[HihoCoder1369]网络流一·Ford-Fulkerson算法
2017-07-28 14:39
513 查看
思路:
最大流模板。
#include<queue> #include<cstdio> #include<cctype> #include<vector> #include<cstring> inline int getint() { char ch; while(!isdigit(ch=getchar())); int x=ch^'0'; while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0'); return x; } const int N=501,M=20000,inf=0x7fffffff; struct Edge { int from,to,remain; }; Edge e[M<<1]; std::vector<int> g ; int sz=0; inline void add_edge(const int u,const int v,const int w) { e[sz]=(Edge){u,v,w}; g[u].push_back(sz); sz++; } int s,t; int a ,p ; inline int Augment() { memset(a,0,sizeof a); a[s]=inf; std::queue<int> q; q.push(s); while(!q.empty()&&!a[t]) { int x=q.front(); q.pop(); for(unsigned i=0;i<g[x].size();i++) { Edge &y=e[g[x][i]]; if(!a[y.to]&&y.remain) { p[y.to]=g[x][i]; a[y.to]=std::min(a[x],y.remain); q.push(y.to); } } } return a[t]; } inline int EdmondsKarp() { int maxflow=0; while(int flow=Augment()) { for(int i=t;i!=s;i=e[p[i]].from) { e[p[i]].remain-=flow; e[p[i]^1].remain+=flow; } maxflow+=flow; } return maxflow; } int main() { int n=getint(),m=getint(); s=1,t=n; while(m--) { int u=getint(),v=getint(),w=getint(); add_edge(u,v,w); add_edge(v,u,0); } printf("%d\n",EdmondsKarp()); return 0; }
相关文章推荐
- hihocoder 1369: 网络流一·Ford-Fulkerson算法
- 【hihocoder 1369】网络流一·Ford-Fulkerson算法
- HihoCoder #1369 : 网络流一·Ford-Fulkerson算法
- hihoCoder 1369 网络流一·Ford-Fulkerson算法 (网络流学习#1 记录)
- hihocoder #1369 : 网络流一·Ford-Fulkerson算法
- #1369 : 网络流一·Ford-Fulkerson算法
- hihocode#1369 : 网络流一·Ford-Fulkerson算法(EK)
- hihoCoder 题目1 : 网络流一·Ford-Fulkerson算法
- hihoCoder-第115周-网络流一·Ford-Fulkerson算法
- hihocoder#1369 : 网络流一·Ford-Fulkerson算法
- HihoCoder #1369 : 网络流一·Ford-Fulkerson算法
- #1369 : 网络流一·Ford-Fulkerson算法 模板题
- hihocoder1369 网络流一·Ford-Fulkerson算法
- 【网络流】最大流最简单的Ford-Fulkerson算法
- 最大网络流之Ford-Fulkerson算法和ScalingFord-Fulkerson算法
- 网络流的征程——Ford-Fulkerson算法
- 网络流最大流之Ford-Fulkerson算法
- HDU3549 - Flow Problem(模板网络流 + Ford-Fulkerson算法)
- 网络流(一)----最大流Ford-Fulkerson算法
- hdu3549(网络流入门题-最大流的Ford-Fulkerson算法)