码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1532 Drainage Ditches

时间:2015-08-11 16:00:06      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

Drainage Ditches



 

 

 

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<vector>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 
 8 struct edge
 9 {
10     int from,to,cap,flow;
11     edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
12 };
13 
14 const int inf=999999;
15 int m,n;
16 int a[205];
17 int p[205];
18 vector<int>G[405];
19 vector<edge>edges;
20 
21 void init()
22 {
23     for(int i=0;i<n;i++)
24     G[i].clear();
25     edges.clear();
26 }
27 
28 void addedge(int from,int to,int cap)
29 {
30     edges.push_back(edge(from,to,cap,0));
31     edges.push_back(edge(to,from,0,0));
32     int m=edges.size();
33     G[to].push_back(m-1);
34     G[from].push_back(m-2);
35 }
36 
37 int maxflow(int s,int t)
38 {
39     int flow=0;
40     while(1)
41     {
42         memset(a,0,sizeof(a));
43         queue<int>q;
44         q.push(s);
45         a[s]=inf;
46         while(!q.empty())
47         {
48             int x=q.front();
49             q.pop();
50             for(int i=0;i<G[x].size();i++)
51             {
52                 edge& e=edges[G[x][i]];
53                 if(!a[e.to]&&e.cap>e.flow)
54                 {
55                     p[e.to]=G[x][i];
56                     a[e.to]=min(a[x],e.cap-e.flow);
57                     q.push(e.to);
58                 }
59             }
60             if(a[t])break;
61         }
62         if(!a[t])break;
63         for(int u=t;u!=s;u=edges[p[u]].from)
64         {
65             edges[p[u]].flow+=a[t];
66             edges[p[u]^1].flow-=a[t];
67         }
68         flow+=a[t];
69     }
70     return flow;
71 }
72 
73 int main()
74 {
75     int a,b,c;
76     while(scanf("%d%d",&m,&n)!=EOF)
77     {
78         init();
79         for(int i=0;i<m;i++)
80         {
81             scanf("%d%d%d",&a,&b,&c);
82             addedge(a,b,c);
83         }
84         int ans=maxflow(1,n);
85         printf("%d\n",ans);
86     }
87     return 0;
88 }

 

HDU 1532 Drainage Ditches

标签:

原文地址:http://www.cnblogs.com/homura/p/4721031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!