POJ 1273 Drainage Ditches题解——S.B.S.

Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 67823   Accepted: 26209

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. 

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.

Output

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

Sample Input

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

Sample Output

50

Source

——————————我是分割线——————————————————————————————————————————————————
水题,模板题。
网络流,最大流。
增广路算法求解。
我竟然调了两个钟头......
正解是DINIC增广,但我直接BFS增广竟然也过了……
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<cassert>
  7 #include<climits>
  8 #define maxn 210
  9 using namespace std;
 10 void find();
 11 void flow();
 12 void update();
 13 struct Edge
 14 {
 15     int c;
 16     int f;
 17 }edge[maxn][maxn];
 18 int n,m;
 19 int s,t;
 20 int residual[maxn][maxn];
 21 int que[maxn*maxn],head,tail;
 22 int pre[maxn];
 23 bool vis[maxn];
 24 int max_flow,min_flow;
 25 void find()
 26 {
 27     int i,cu;
 28     memset(vis,false,sizeof(vis));
 29     memset(residual,0,sizeof(residual));
 30     memset(pre,0,sizeof(pre));
 31     head=0;que[head]=s;pre[s]=s;vis[s]=true;tail=1;
 32     while(head<tail&&pre[t]==0)
 33     {
 34         cu=que[head];
 35         for(i=1;i<=n;i++)
 36         {
 37             if(vis[i]==false)
 38             {
 39                 if(edge[cu][i].c-edge[cu][i].f>0)
 40                 {
 41                     residual[cu][i]=edge[cu][i].c-edge[cu][i].f;
 42                     pre[i]=cu;que[tail++]=i;vis[i]=true;
 43                 }
 44                 else if(edge[i][cu].f>0)
 45                 {
 46                     residual[cu][i]=edge[i][cu].f;
 47                     pre[i]=cu;que[tail++]=i;vis[i]=true;
 48                 }
 49             }
 50         }
 51         head++;
 52     }
 53 }
 54 void flow()
 55 {
 56     int i=t,j;
 57     if(pre[i]==0)
 58     {
 59         min_flow=0;return;
 60     }
 61     j=0x7fffffff;
 62     while(i!=s)
 63     {
 64         if(residual[pre[i]][i]<j) j=residual[pre[i]][i];
 65         i=pre[i];
 66     }
 67     min_flow=j;
 68 }
 69 void update()
 70 {
 71     int i=t;
 72     if(pre[i]==0) return;
 73     while(i!=s)
 74     {
 75         if(edge[pre[i]][i].c-edge[pre[i]][i].f>0)
 76             edge[pre[i]][i].f+=min_flow;
 77         else if(edge[i][pre[i]].f>0)
 78             edge[pre[i]][i].f+=min_flow;
 79         i=pre[i];    
 80     }
 81 }
 82 void solve()
 83 {
 84     s=1;t=n;
 85     max_flow=0;
 86     while(true)
 87     {
 88         find();flow();
 89         max_flow+=min_flow;
 90         if(min_flow>0) update();
 91         else return;
 92     }
 93 }
 94 int main()
 95 {
 96     std::ios::sync_with_stdio(false);
 97     int i,u,v,c;
 98     while(scanf("%d %d",&m,&n)!=EOF)
 99     {
100         memset(edge,0,sizeof(edge));
101         for(i=0;i<m;i++)
102         {
103             scanf("%d %d %d",&u,&v,&c);
104             edge[u][v].c+=c;
105         }
106         solve();
107         printf("%d\n",max_flow);
108     }
109     return 0;
110 }
View Code

 

posted @ 2016-05-28 08:23  Orion_7  阅读(362)  评论(0编辑  收藏  举报