POJ-1273 Drainage Ditches(最大流)
2016-07-17 14:32
375 查看
题目链接:http://poj.org/problem?id=1273
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
Sample Output
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 最大流水题,适合入门熟悉算法
#include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<cstdio> #include<queue> #include<stack> using namespace std; const int INF = 0x3f3f3f3f; const int MAXM = 205; int cost[MAXM][MAXM]; int n, m; int start, end; int path[MAXM], flow[MAXM]; int bfs(){ memset( path, -1, sizeof( path ) ); path[start] = 0; flow[start] = INF; queue<int> Q; Q.push( start ); while( !Q.empty() ){ int x = Q.front(); Q.pop(); if( x == end ) break; for( int i = 1; i <= m; i++ ){ if( i != start && path[i] == -1 && cost[x][i] ){ flow[i] = flow[x] < cost[x][i] ? flow[x] : cost[x][i]; Q.push( i ); path[i] = x; } } } if( path[end] == -1 ) return -1; return flow[end]; } int Edmonds_Karp(){ int max_flow = 0, add, now, pre; while( ( add = bfs() ) != -1 ){ max_flow += add; now = end; while( now != start ){ pre = path[now]; cost[pre][now] -= add; cost[now][pre] += add; now = pre; } } return max_flow; } int main(){ ios::sync_with_stdio( false ); while( cin >> n >> m ){ memset( cost, 0, sizeof( cost ) ); int a, b, c; while( n-- ){ cin >> a >> b >> c; cost[a][b] += c; } start = 1; end = m; cout << Edmonds_Karp() << endl; } }
相关文章推荐
- BNU Training 2016.07.11
- An error has occurred,See error log for more details
- Problem B. Rain
- 自学——PaintAndPath
- 修复 apt-get update “the following signatures couldn’t be verified because the public key is not avai
- [区间dp] hdu 2476 string painter
- 19. ubuntu 16.04 LTS 安装mailx
- UVALive 6893 The Big Painting hash
- HDU 2476 String painter (区间DP)
- UVALive 6885 Flowery Trails 最短路
- HighAvailability
- UVA 442 Matrix Chain Multiplication
- Mybaits深入了解(三)----mybatis开发Dao的方法
- Mybaits深入了解(二)—-入门实例
- main中的数组args传值
- LeetCode-336.Palindrome Pairs
- hdu 1789 Doing Homework again 贪心
- 邮件服务器(postfix + dovecot + extmail + extman + MailScanner + spamassassin + ClamAV )
- 微软开源Malmo AI项目平台 利用Minecraft构建复杂机器人任务
- Training2:位操作训练