Drainage Ditches 網路流 dinic 模板題
題目:
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
輸入:
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
輸出:
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
樣例輸入:
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
樣例輸出:
50
有N個溝渠,有M個溝渠交叉點,源點為1,匯點為M,問從1到M的最大速率。
AC程式碼:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <algorithm> using namespace std; const int INF=0x7fffffff; const int maxn=300; int dis[maxn],head[maxn]; int cnt; int n,m; struct edge { int u,v,c,f,next; }node[2*maxn]; void add(int u,int v,int c,int f) { node[cnt].u=u; node[cnt].v=v; node[cnt].c=c; node[cnt].f=f; node[cnt].next=head[u]; head[u]=cnt++; } int bfs() { queue<int>q; memset(dis,0,sizeof(dis)); q.push(1); dis[1]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=node[i].next) { edge e=node[i]; if(!dis[e.v]&&e.c-e.f>0) { dis[e.v]=dis[e.u]+1; q.push(e.v); } } } return dis[m]!=0; } int dfs(int u,int mx) { if(u==m)return mx; for(int i=head[u];i!=-1;i=node[i].next) { edge e=node[i]; if(dis[e.v]==dis[e.u]+1 && e.c-e.f>0) { int v=dfs(e.v,min(mx,e.c-e.f)); if(v>0) { node[i].c-=v; node[i^1].c+=v; return v; } } } return 0; } int dinic() { int maxflow=0; while(bfs()) { while(int res=dfs(1,INF)) maxflow+=res; } return maxflow; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(head,-1,sizeof(head)); cnt=0; for(int i=0;i<n;i++) { int s,e,c; scanf("%d%d%d",&s,&e,&c); add(s,e,c,0); add(e,s,0,0); } cout<<dinic()<<endl; } return 0; }