hdu 1532 Drainage Ditche

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

 

Total Submission(s): 6601    Accepted Submission(s): 3131
Problem 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
 

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #define INF 2110000000
 5 using namespace std;
 6 int n,m;
 7 int cap[202][202],flow[202][202];
 8 int c[212],p[212];
 9 int EK(int s,int t)
10 {
11     int f=0;
12     queue<int> q;             
13     memset(flow,0,sizeof(flow));
14     for(;;)                  
15     {
16         memset(c,0,sizeof(c));
17         memset(p,0,sizeof(p));
18         c[s]=INF;
19         q.push(s);
20         while(!q.empty())              //bfs找到增广路
21         {
22             int u=q.front();
23             q.pop();
24             for(int v=1;v<=n;v++)
25             {
26                 if(!c[v]&&cap[u][v]>flow[u][v])//找到新结点v
27                 {
28                     p[v]=u;                            //记录v的父结点,
29                     q.push(v);                         //加入队列
30                     c[v]=c[u]<cap[u][v]-flow[u][v]?c[u]:cap[u][v]-flow[u][v];//s-v路径上的最小残值
31                 }
32             }
33         }
34         if(c[t]==0) break;                 //没有增广路了,就跳出
35         for(int u=t;u!=s;u=p[u])           //从汇点往回走
36         {
37             flow[p[u]][u]+=c[t];           //更新正向流量
38             flow[u][p[u]]-=c[t];           //更新反向流量
39         }
40         f+=c[t];                           //更新从s流出的总流量
41     }
42     return f;
43 }
44 int main()
45 {
46     int i,a,b,cost;
47     while(scanf("%d%d",&m,&n)!=EOF)
48     {
49         memset(cap,0,sizeof(cap));
50 
51         for(i=0;i<m;i++)
52         {
53             scanf("%d%d%d",&a,&b,&cost);
54             cap[a][b]+=cost;
55         }
56         printf("%d\n",EK(1,n));
57     }
58     return 0;
59 }

 

posted @ 2013-08-10 21:08  ゐ星落★孤晨ね  阅读(234)  评论(0编辑  收藏  举报