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

Drainage Ditches

时间:2015-08-12 19:09:18      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

Drainage Ditches
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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到m点有若干通道,通道流量有限制,求最大流量。
EK:
 1 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 2 //int main(int argc, char** argv) 
 3 
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<queue>
 7 #include<algorithm>
 8 
 9 using namespace std;
10 
11 #define maxn 220
12 #define INF 0xfffffff
13 #define min(a, b)(a < b ? a : b)
14 
15 int maps[maxn][maxn], pre[maxn];
16 
17 bool bfs(int s, int e)   // 找路
18 {
19     memset(pre, 0, sizeof(pre));
20     queue<int> Q;
21     Q.push(s);
22     
23     while(Q.size())
24     {
25         int i = Q.front();
26         Q.pop();
27         if(i == e)
28         return true;
29         
30         for(int j = 1; j <= e; j++)
31         {
32             if(maps[i][j] && pre[j] == 0)  // 此通道还有流量可以用,并且当前点在当前路上没有被用到
33             {
34                 pre[j] = i;  // 表示从i->j流向
35                 Q.push(j);
36             }
37         }
38     }
39     return false;
40 }
41 
42 int EK(int s, int e)
43 {
44     int i, Minflow;
45     int ans = 0;
46     
47     while(bfs(s, e))  //找到一条路径
48     {
49         Minflow = INF;
50         for(i = e; i != s; i = pre[i])
51             Minflow = min(Minflow, maps[pre[i]][i]);   // 从后往前找,找当前路上最小的流量限制
52         for(i = e; i != s; i = pre[i])
53         {
54             maps[pre[i]][i] -= Minflow; // 当前管道的流量减去已经用的就是剩下可以用的
55             maps[i][pre[i]] += Minflow;   //反方向加~why
56         }
57         ans += Minflow;  // 总流量相加
58     }
59     return ans;
60 }
61 int main()
62 {
63     int n, m, x, y, q;
64 
65     while(scanf("%d%d", &n, &m) != EOF)
66     {
67         memset(maps, 0, sizeof(maps));
68         
69         while(n--)
70         {
71             scanf("%d%d%d", &x, &y, &q);
72             maps[x][y] += q;  // 管道流量也是相加
73         }
74         printf("%d\n", EK(1, m));  //
75     }
76     return 0;
77 }

 

Drainage Ditches

标签:

原文地址:http://www.cnblogs.com/Tinamei/p/4724988.html

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