POJ 1273 Drainage Ditches(最大流 SAP 模板)
Drainage Ditches
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.
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
模板题 参考了大佬的模板 http://blog.csdn.net/lianai911/article/details/44962653
代码如下:
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int MAXN = 220; const int MAXM = MAXN*MAXN; const int INF = 0xffffff0; struct EdgeNode { int to; int w; int next; }Edges[MAXM]; int Head[MAXN],tol; void init() { tol = 0; memset(Head,-1,sizeof(Head)); } void add_edge(int u,int v,int w) { Edges[tol].to = v; Edges[tol].w = w; Edges[tol].next = Head[u]; Head[u] = tol++; Edges[tol].to = u; Edges[tol].w = 0; Edges[tol].next = Head[v]; Head[v] = tol++; } int Numh[MAXN],h[MAXN],curedges[MAXN],pre[MAXN]; void BFS(int end,int N) { memset(Numh,0,sizeof(Numh)); for(int i = 1; i <= N; ++i) Numh[h[i]=N]++; h[end] = 0; Numh[N]--; Numh[0]++; queue<int> Q; Q.push(end); while(!Q.empty()) { int v = Q.front(); Q.pop(); int i = Head[v]; while(i != -1) { int u = Edges[i].to; if(h[u] < N) { i = Edges[i].next; continue; } h[u] = h[v] + 1; Numh[N]--; Numh[h[u]]++; Q.push(u); i = Edges[i].next; } } } int SAP(int start,int end,int N) { int Curflow,FlowAns = 0,temp,neck; memset(h,0,sizeof(h)); memset(pre,-1,sizeof(pre)); for(int i = 1; i <= N; ++i) curedges[i] = Head[i]; BFS(end,N); int u = start; while(h[start] < N) { if(u == end) { Curflow = INF; for(int i = start; i != end; i = Edges[curedges[i]].to) { if(Curflow > Edges[curedges[i]].w) { neck = i; Curflow = Edges[curedges[i]].w; } } for(int i = start; i != end; i = Edges[curedges[i]].to) { temp = curedges[i]; Edges[temp].w -= Curflow; Edges[temp^1].w += Curflow; } FlowAns += Curflow; u = neck; } int i; for(i = curedges[u]; i != -1; i = Edges[i].next) if(Edges[i].w && h[u]==h[Edges[i].to]+1) break; if(i != -1) { curedges[u] = i; pre[Edges[i].to] = u; u = Edges[i].to; } else { if(0 == --Numh[h[u]]) break; curedges[u] = Head[u]; for(temp = N,i = Head[u]; i != -1; i = Edges[i].next) if(Edges[i].w) temp = min(temp,h[Edges[i].to]); h[u] = temp + 1; ++Numh[h[u]]; if(u != start) u = pre[u]; } } return FlowAns; } int main() { int m,n,x,y,v; while(scanf("%d%d",&m,&n)!=EOF) { init(); for(int i=0;i<m;i++) { scanf("%d%d%d",&x,&y,&v); add_edge(x,y,v); } printf("%d\n",SAP(1,n,n)); } return 0; }