网络流EK算法
来源:互联网 发布:网络电视怎么看直播啊 编辑:程序博客网 时间:2024/06/05 18:18
题目描述
有n个点,m条有向边,对于每条边u->v,可以从u到v流w容量,问,1到n最大可以流多少容量。
输入
第一行输入n,m
接下来m行每行输入u, v, w
输出
输出最大流
样例输入
4 5
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
样例输出
50
提示
【数据规模和约定】
1<=n<=2000
1<=m<=10000
0<=w<=10000
开始填网络流的坑,此题是最大流EK算法(找增广路)的模版题,网络流中的反向边真是精妙!
EK算法的复杂度为
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;int n,m,x,y,z,ans,tot,src,tar;int head[2005],Next[20005],to[20005],cap[20005];int pre[2005],path[2005],vis[2005],g[200005]; void add(int x,int y,int z){ Next[tot]=head[x]; to[tot]=y; cap[tot]=z; head[x]=tot; tot++;}int bfs(){ int t=0,w=1; for(int i=1;i<=n;i++) vis[i]=0; g[1]=src; pre[src]=-1;path[src]=-1;vis[src]=1; while(t<w) { t++; int x=g[t]; for(int i=head[x];i!=-1;i=Next[i]) { int y=to[i],z=cap[i]; if(vis[y]) continue; if(z<=0) continue; pre[y]=x;path[y]=i;vis[y]=1; if(y==tar) return 1; w++; g[w]=y; } } return 0;}int ek(){ int ret=0; while(bfs()) { int x=tar,flow=1e9; while(x!=src) { flow=min(flow,cap[path[x]]); x=pre[x]; } x=tar; while(x!=src) { cap[path[x]]-=flow; cap[path[x]^1]+=flow; x=pre[x]; } ret+=flow; } return ret;}int main(){ cin>>n>>m; for(int i=1;i<=n;i++) head[i]=-1; for(int i=1;i<=m;i++) { scanf("%d%d%d",&x,&y,&z); add(x,y,z); add(y,x,0); } src=1;tar=n; ans=ek(); cout<<ans; return 0;}
0 0
- 网络流EK算法
- 网络流EK算法
- 网络流EK算法
- 网络流EK算法
- 网络流EK算法
- 网络流--EK算法
- 网络流EK算法理解
- 网络流EK算法详解
- 网络流之EK 算法
- 最大网络流 EK 算法
- 网络最大流 EK算法
- EK算法(网络流,最大流)
- 网络流--求最大流:EK算法
- POJ 1459 网络流算法(EK)
- poj1698-网络流,(Ek)和(Dinic)算法。
- 网络流的EK算法模板
- hdu 1565 网络流(EK算法)
- 网络流算法总汇(ek,dinic,isap)
- Big data and its developer fallout
- EXTERN C 关键字
- 验证码有几种?
- Spring事务配置的五种方式(转载)
- spring配置: Annotation vs XML
- 网络流EK算法
- 二、Android情景分析之详解init进程(以启动zygote为例)
- 对org.springframework.beans.CachedIntrospectionResults的再次解读
- 程序设计原则
- 分贝(dB)的概念辨析
- mysql两种递归查询效率比较
- DATEDIF函数
- Very Deep Convolutional Networks for Large-Scale Image Recognition
- Android中使用DownloadManager类来管理数据下载