Luogu 3376 网络最大流

栏目: 数据库 · 发布时间: 7年前

内容简介:题目链接:思路:这道题数据范围较大,按照邻接矩阵的方法存边只能得70分。

题目链接: https://www.luogu.org/problemnew/show/P3376

思路:

这道题数据范围较大,按照邻接矩阵的方法存边只能得70分。

还是先考虑求解,只是更换存边方法。

由于本人对掌握不是很好,于是就没敢用存边,听说很方便?

其实邻接矩阵比较好写,在存反向边和增广方面。

用邻接表其实也不难。

对于反向边,我们边号需要从0开始,也就是与平常不同,。

然后就可以用异或来做,因为0^1=1,1^1=0,2^1=3,3^1=2,......

然后增光的时候循环稍有不同,看代码吧。

#include <cstdio>
#include <cctype>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
static const int MAXN=20050;
static const int MAXM=200050;
static const int INF=1<<30;
using namespace std;
struct Edge{
    int from,to,val,nextt;
}edge[MAXM];
int n,m,s,t,u,v,w,num,maxflow,head[MAXN],res[MAXN],father[MAXN];
queue<int> q;
inline int read(){
    int x=0;bool sign=false;
    char alpha=0;
    while(!isdigit(alpha)) sign|=alpha=='-',alpha=getchar();
    while(isdigit(alpha)) x=(x<<1)+(x<<3)+(alpha^48),alpha=getchar();
    return sign?-x:x;
}
inline void addedge(int u,int v,int w){
    edge[num].from=u;
    edge[num].to=v;
    edge[num].val=w;
    edge[num].nextt=head[u];
    head[u]=num++;
}
inline bool bfs(int s,int t){
    while(!q.empty()) q.pop();
    memset(res,0,sizeof(res));
    q.push(s);
    res[s]=INF;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=head[x];i;i=edge[i].nextt){
            int to=edge[i].to;
            if(res[to]==0&&edge[i].val){
                father[to]=i;
                res[to]=min(res[x],edge[i].val);
                q.push(to); 
            }
        }
        if(res[t]) return true;
    }
    return false;
}
inline int EK(int s,int t){
    while(1){
        if(!bfs(s,t)) break;
        for(int i=t;i!=s;i=edge[father[i]].from){
            edge[father[i]].val-=res[t];
            edge[father[i]^1].val+=res[t];
        }
        maxflow+=res[t];
    }
    return maxflow;
}
int main(){
    n=read();m=read();s=read();t=read();
    for(int i=1;i<=m;i++){
        u=read();v=read();w=read();
        addedge(u,v,w);
        addedge(v,u,0);
    }
    printf("%d\n",EK(s,t));
    return 0;
}

之后会更新算法。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Maven实战

Maven实战

许晓斌 / 机械工业出版社 / 2010年12月 / 65.00元

你是否早已厌倦了日复一日的手工构建工作?你是否对各个项目风格迥异的构建系统感到恐惧?Maven——这一Java社区事实标准的项目管理工具,能帮你从琐碎的手工劳动中解脱出来,帮你规范整个组织的构建系统。不仅如此,它还有依赖管理、自动生成项目站点等超酷的特性,已经有无数的开源项目使用它来构建项目并促进团队交流,每天都有数以万计的开发者在访问中央仓库以获取他们需要的依赖。 本书内容全面而系统,Ma......一起来看看 《Maven实战》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换