Drainage Ditches
来源:互联网 发布:淘宝直通车在哪开通 编辑:程序博客网 时间:2024/06/14 07:29
题目描述
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.
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.
输入描述:
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.
输出描述:
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
示例1
输入
5 41 2 401 4 202 4 202 3 303 4 10
输出
50
#include<iostream>#include<cstring>#include<queue>using namespace std;const int MAXM=203;int capacity[MAXM][MAXM];int flow[MAXM];int pre[MAXM];int n,m;int bfs(){ memset(pre,0,sizeof(pre)); flow[1]=11000000; queue<int> q; q.push(1); while(!q.empty()) { int s=q.front(); q.pop(); for(int i=2;i<=m;++i) if(capacity[s][i]>0&&pre[i]==0) { flow[i]=min(flow[s],capacity[s][i]); pre[i]=s; if(i==m) return flow[m]; q.push(i); } } return 0;}int maxFlow(){ int increase; int sumflow=0; while(increase=bfs()) { sumflow+=increase; for(int i=m;i;i=pre[i]) { capacity[pre[i]][i]-=increase; capacity[i][pre[i]]+=increase; } } return sumflow;}void show(){ for(int i=1;i<=m;++i) { for(int j=1;j<=m;++j) cout<<capacity[i][j]<<" "; cout<<endl; }}int main(){ int a,b,c; while(cin>>n>>m) { memset(capacity,0,sizeof(capacity)); for(int i=1;i<=n;++i) { cin>>a>>b>>c; capacity[a][b]+=c; } cout<<maxFlow()<<endl; } return 0;}
阅读全文
0 0
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- Drainage Ditches
- POJ1273 Drainage Ditches
- PKU 1273 Drainage Ditches
- Drainage Ditches 最大流
- InnoDB与MyISAM数据引擎对比选择
- 2017年科技界十大新闻,你都知道吗?
- 73. Set Matrix Zeroes
- ASP.NET请求处理过程
- 解决svn: Can't convert string from 'UTF-8' to native encoding问题
- Drainage Ditches
- Android 8.0系统源码分析--Message发送、处理过程源码分析
- php运行出现Call to undefined function curl_init()的解决方法
- 第十三周作业第一题
- curl_init不支持的一个可行方案,亲测问题解决。
- 32. Longest Valid Parentheses
- CodeForces 45 G.Prime Problem(构造+数论)
- C++ operator关键字(重载操作符)
- Going Home