POJ1273 Drainage Ditches

Drainage Ditches
 

Description

 

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.

一个有向图, M条有向边和N个点,求点1 到点N的最大流。

Input

 

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.

第1行:2个整数M (2 <= M <= 200) 和N (0 <= N <= 200)。(注意给出的格式M N)

下来M行: 每行有三个整数:x,y,c。表示一条从点x到点y的有向边,流量为c (0<= c <= 10,000,000)。

Output

 

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

输出一个整数,即最大流量。

Sample Input 1 

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output 1

50

Source

最大流裸题,直接上板子。

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 
  4 const int maxn=100010;
  5 const int maxm=400010;
  6 const int inf=0x3f3f3f3f;
  7 struct Edge{
  8     int to,next,cap,flow,cost;
  9 }edge[maxm];
 10 
 11 int tol;
 12 int head[maxn];
 13 int gap[maxn],dep[maxn],cur[maxn];
 14 void init() {
 15     tol=0;
 16     memset(head,-1,sizeof(head));
 17 }
 18 void addedge(int u,int v,int w,int rw=0) {
 19     edge[tol].to=v;edge[tol].cap=w;edge[tol].flow=0;
 20     edge[tol].next=head[u];head[u]=tol++;
 21     edge[tol].to=u;edge[tol].cap=rw;edge[tol].flow=0;
 22     edge[tol].next=head[v];head[v]=tol++;
 23 }
 24 
 25 int Q[maxn];
 26 void bfs(int start,int end) {
 27     memset(dep,-1,sizeof(dep));
 28     memset(gap,0,sizeof(gap));
 29     gap[0]=1;
 30     int front=0,rear=0;
 31     dep[end]=0;
 32     Q[rear++]=end;
 33     while(front!=rear) {
 34         int u=Q[front++];
 35         for(int i=head[u];i!=-1;i=edge[i].next) {
 36             int v=edge[i].to;
 37             if(dep[v]!=-1) continue;
 38             Q[rear++]=v;
 39             dep[v]=dep[u]+1;
 40             gap[dep[v]]++;
 41         }
 42     }
 43 }
 44 
 45 int S[maxn];
 46 int sap(int start,int end,int n) {
 47     bfs(start,end);
 48     memcpy(cur,head,sizeof(head));
 49     int top=0;
 50     int u=start;
 51     int ans=0;
 52     while(dep[start]<n) {
 53         if(u==end) {
 54             int minn=inf;
 55             int inser;
 56             for(int i=0;i<top;i++) {
 57                 if(minn>edge[S[i]].cap-edge[S[i]].flow) {
 58                     minn=edge[S[i]].cap-edge[S[i]].flow;
 59                     inser=i;
 60                 }
 61             }
 62             for(int i=0;i<top;i++) {
 63                 edge[S[i]].flow+=minn;
 64                 edge[S[i]^1].flow-=minn;
 65             }
 66             ans+=minn;
 67             top=inser;
 68             u=edge[S[top]^1].to;
 69             continue;
 70         }
 71         bool flag=false;
 72         int v;
 73         for(int i=cur[u];i!=-1;i=edge[i].next) {
 74             v=edge[i].to;
 75             if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]) {
 76                 flag=true;
 77                 cur[u]=i;
 78                 break;
 79             }
 80         }
 81         if(flag) {
 82             S[top++]=cur[u];
 83             u=v;
 84             continue;
 85         }
 86         int minn=n;
 87         for(int i=head[u];i!=-1;i=edge[i].next) {
 88             if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<minn) {
 89                 minn=dep[edge[i].to];
 90                 cur[u]=i;
 91             }
 92         }
 93         gap[dep[u]]--;
 94         if(!gap[dep[u]]) return ans;
 95         dep[u]=minn+1;
 96         gap[dep[u]]++;
 97         if(u!=start) u=edge[S[--top]^1].to;
 98     }
 99     return ans;
100 }
101 
102 int main() {
103     int n,m;
104     while(~scanf("%d%d",&m,&n)) {
105         init();
106         for(int i=1;i<=m;i++) {
107             int u,v,w;
108             scanf("%d%d%d",&u,&v,&w);
109             addedge(u,v,w);
110         }
111         printf("%d
",sap(1,n,n));
112     }
113 }
原文地址:https://www.cnblogs.com/ACMerszl/p/10786258.html