poj1273 Drainage Ditches(最大流)
2015-08-06 09:49
573 查看
Drainage Ditches
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
题意:n条水管,m个结点,给出每条水管的最大流量,求从第一个结点流向最后一个结点,最多的水流量。
典型的最大流模板题,套用Dinic算法的模板。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 62364 | Accepted: 23992 |
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
题意:n条水管,m个结点,给出每条水管的最大流量,求从第一个结点流向最后一个结点,最多的水流量。
典型的最大流模板题,套用Dinic算法的模板。
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define maxn 205 using namespace std; int m, n, s, e, c; int maps[maxn][maxn], q[maxn * maxn]; int d[maxn];//记录每个点所在的层 int BFS()//BFS分层,判断是否存在增广路 { memset(d, -1, sizeof(d)); d[1] = 0; int f, r; f = 1; r = 1; q[r++] = 1; while(f < r) { int x = q[f++]; for(int i = 1; i <= n; i++) { if(d[i] < 0 && maps[x][i] > 0) { d[i] = d[x] + 1; q[r++] = i; } } } if(d > 0) return 1; else return 0; } int Find(int x, int v)//找出某一条增广路的最大流 { int a; if(x == n) return v; for(int i = 1; i <= n; i++) { if(maps[x][i] > 0 && d[i] == d[x] + 1 && (a = Find(i, min(v, maps[x][i]))))//x到i有流量 且 i是x的下一层 且 i到汇点存在最大流 { maps[x][i] -= a;//增广路 maps[i][x] += a;//回退边 return a; } } return 0; } int main() { int ans, t; while (scanf("%d%d", &m, &n) != EOF) { ans = 0; memset(maps, 0, sizeof(maps)); for(int i = 0; i < m; i++) { scanf("%d%d%d", &s, &e, &c); maps[s][e] += c; } while(BFS())//BFS搜索判断是否有从源点到汇点的通路 { while(t = Find(1, 2000000005))//查找增广路,求其最大流 ans += t; } printf("%d\n", ans); } }
相关文章推荐
- POJ ACM 1001
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- 用单调栈解决最大连续矩形面积问题
- 2632 Crashing Robots的解决方法
- 1573 Robot Motion (简单题)
- 【高手回避】poj3268,一道很水的dijkstra算法题
- POJ 1088 滑雪
- poj2387 Til the Cows Come Home—Dijkstra模板
- poj 2485 Highways
- poj 1511 Invitation Cards
- POJ水题1083区间重叠问题
- 【网络流-最大流-Dinic-建模】POJ3281 Dining:Pascal 解法