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

XTU 二分图和网络流 练习题 J. Drainage Ditches

时间:2014-07-26 00:21:56      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   os   io   

J. Drainage Ditches

Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d      Java class name: Main
 
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

解题:哈哈 直接求最大流就是了!模板一刷,AC到手。。。。。。。。^_^

bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #include <queue>
10 #define LL long long
11 #define INF 0x3f3f3f3f
12 using namespace std;
13 const int maxn = 500;
14 int cap[maxn][maxn],flow[maxn][maxn],a[maxn],link[maxn];
15 queue<int>q;
16 int main(){
17     int n,m,i,j,u,v,w,ans;
18     while(~scanf("%d%d",&n,&m)){
19         memset(cap,0,sizeof(cap));
20         memset(flow,0,sizeof(flow));
21         for(i = 0; i < n; i++){
22             scanf("%d%d%d",&u,&v,&w);
23             cap[u][v] += w;
24         }
25         while(!q.empty()) q.pop();
26         ans = 0;
27         while(true){
28             memset(a,0,sizeof(a));
29             a[1] = INF;
30             q.push(1);
31             while(!q.empty()){
32                 u = q.front();
33                 q.pop();
34                 for(v = 1; v <= m; v++){
35                     if(!a[v] && cap[u][v] > flow[u][v]){
36                         link[v] = u;
37                         q.push(v);
38                         a[v] = min(a[u],cap[u][v]-flow[u][v]);
39                     }
40                 }
41             }
42             if(a[m] == 0) break;
43             for(u = m; u != 1; u = link[u]){
44                 flow[link[u]][u] += a[m];
45                 flow[u][link[u]] -= a[m];
46             }
47             ans += a[m];
48         }
49         printf("%d\n",ans);
50     }
51     return 0;
52 }
View Code

 

XTU 二分图和网络流 练习题 J. Drainage Ditches,布布扣,bubuko.com

XTU 二分图和网络流 练习题 J. Drainage Ditches

标签:des   style   blog   http   java   color   os   io   

原文地址:http://www.cnblogs.com/crackpotisback/p/3868384.html

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