hdu 1532Drainage Ditches

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4652    Accepted Submission(s): 2171


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
View Code
 1 #include<stdio.h>
 2 #include<queue>
 3 #include<string.h>
 4 #define min(x,y) x>y?y:x
 5 #define N 205
 6 using namespace std;
 7 int flow[N];
 8 int c[N][N];
 9 int p[N];
10 int n,m;
11 int bfs(int s,int e)
12 {
13     queue<int> q;
14     for(int i=1;i<=n;i++)
15     p[i]=-1;
16     p[s]=0;
17     q.push(s);
18     while(!q.empty())
19     {
20         
21         int temp=q.front();
22         
23         q.pop();
24         if(temp==e) break;
25         flow[s]=INT_MAX;
26         for(int i=1;i<=n;i++)
27         {
28             if(i!=temp&&c[temp][i]>0&&p[i]==-1)
29             {
30                 flow[i]=min(flow[temp],c[temp][i]);
31                 p[i]=temp;
32                 q.push(i);
33             }
34         }
35     }
36     if(p[e]==-1) return -1;
37     else     return flow[e];
38     
39 }
40 
41 int max_flow(int s,int e)
42 {
43     
44     int ans=0;
45     int sum=0;
46     while((ans=bfs(s,e))!=-1)
47     {
48         sum+=ans;
49         int last=e;
50         while(last!=s)
51         {
52             
53             int pre=p[last];
54             c[pre][last]-=ans;
55             c[last][pre]+=ans;
56             last=pre;
57         }
58     }
59     return sum;
60 }
61 int main()
62 {
63     
64     int aa,bb,cc;
65     while(~scanf("%d%d",&n,&m))
66     {
67         
68         memset(c,0,sizeof(c));
69         memset(flow,0,sizeof(flow));
70         for(int i=1;i<=n;i++)
71         {
72             scanf("%d%d%d",&aa,&bb,&cc);
73             if(aa==bb) continue;
74             
75             c[aa][bb]+=cc;
76         }
77         printf("%d\n",max_flow(1,m));
78     }
79 }

 

posted on 2012-09-01 20:21  仁者无敌8勇者无惧  阅读(532)  评论(0编辑  收藏  举报

导航