首页 > 代码库 > XTU 二分图和网络流 练习题 J. Drainage Ditches
XTU 二分图和网络流 练习题 J. Drainage Ditches
J. Drainage Ditches
Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d Java class name: MainEvery 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.
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 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50
解题:哈哈 直接求最大流就是了!模板一刷,AC到手。。。。。。。。^_^
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 long11 #define INF 0x3f3f3f3f12 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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。