POJ 1273 USACO 93 Drainage Ditches 最大流
2016-04-04 00:19
761 查看
先拉低一波文章质量(文章质量早已不行了。。)
Total Submissions: 66877 Accepted: 25841
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.
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int inf = 2147483647, N = 210, M = 40005; int level , n, cnt = 0, v[M], w[M], p[M], h , q[M]; void add(int a, int b, int c) { p[++cnt] = h[a]; v[cnt] = b; w[cnt] = c; h[a] = cnt; p[++cnt] = h[b]; v[cnt] = a; w[cnt] = 0; h[b] = cnt; } bool bfs() { int f = 0, r = 0, u, i; memset(level, -1, sizeof level); q[r++] = 1; level[1] = 1; while (f < r) { u = q[f++]; for (i = h[u]; i; i = p[i]) { if (w[i] && level[v[i]] == -1) { level[v[i]] = level[u] + 1; q[r++] = v[i]; if (v[i] == n) return 1; } } } return false; } ll dfs(int u, ll low) { int i, tmp = 0; ll res = 0; if (u == n) return low; for (i = h[u]; i && res < low; i = p[i]) { if (w[i] && level[v[i]] == level[u] + 1) { tmp = dfs(v[i], min((ll) w[i], low - res)); w[i] -= tmp; w[i ^ 1] += tmp; res += tmp; } } if (!res) level[u] = -1; return res; } ll dinic() { ll ans = 0; while (bfs()) ans += dfs(1, inf); return ans; } int main() { int m, u, v; ll w; while(scanf("%d%d", &m, &n) != EOF) { cnt = 1; memset(h, 0, sizeof h); while (m--) { scanf("%d%d%lld", &u, &v, &w); add(u, v, w); } printf("%lld\n", dinic()); } return 0; }
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 66877 Accepted: 25841
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 41 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50Source
USACO 93相关文章推荐
- 使用jacob将word转为pdf时报com.jacob.com.ComFailException: Invoke of: SaveAs ...
- 【BZOJ1660】【Usaco2006 Nov】Bad Hair Day 乱发节
- 造人论坛上关于人工智能的一些科幻想法
- http://blog.csdn.net/i_bruce/article/details/39555417
- 写Detail的接口
- 03-树3 Tree Traversals Again (25分)
- opencv笔记02-main函数使用
- 笔记---main函数及其他问题
- 2.3-saltstack grains
- LeetCode OJ刷题历程——Contains Duplicate
- HDU1702 ACboy needs your help again! 队列&&栈的应用
- 用软件开发的方式生成小说是否可能?
- poj-1273-Drainage Ditches(最大流问题)
- resume suspend wait sleep
- 多线程sleep,wait,yield辨析
- LeetCode:计算相同数组元素的下标差(Contains Duplicate II)
- Never _______ again
- FZU 2216 The Longest Straight (二分)
- 11. Container With Most Water
- LightOJ 1138 - Trailing Zeroes (III) (求末尾0为x的最小N---二分)