Drainage Ditches
2015-08-17 20:50
483 查看
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
还是裸题,改一下输入输出就好了。
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<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> #include<algorithm> #include<string> #include<cmath> #include<set> #include<map> using namespace std; typedef long long ll; const int inf = 0x3f3f3f3f; const int maxn = 2005; struct Edge{ int form, to, cap, flow; Edge(int u, int v, int c, int f) :form(u), to(v), cap(c), flow(f){} }; struct EdmondKarp{ int n, m; vector<Edge>edges; vector<int>G[maxn]; int a[maxn]; int p[maxn]; void init(int n) { for (int i = 0; i < n; i++)G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int cap) { edges.push_back(Edge(from, to, cap, 0)); edges.push_back(Edge(to, from, 0, 0)); m = edges.size(); G[from].push_back(m - 2); G[to].push_back(m - 1); } int Maxflow(int s, int t) { int flow = 0; for (;;) { memset(a, 0, sizeof(a)); queue < int >Q; Q.push(s); a[s] = inf; while (Q.size()) { int x = Q.front(); Q.pop(); for (int i = 0; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if (!a[e.to] && e.cap>e.flow) { p[e.to] = G[x][i]; a[e.to] = min(a[x], e.cap - e.flow); Q.push(e.to); } } if (a[t])break; } if (!a[t])break; for (int u = t; u != s; u = edges[p[u]].form) { edges[p[u]].flow += a[t]; edges[p[u] ^ 1].flow -= a[t]; } flow += a[t]; } return flow; } }ans; int main() { int i, j, m, n, t, p = 1, c, s; while (scanf("%d",&n)!=EOF) { if (!n)break; ans.init(n); cin >> s >> t >> c; for (i = 1; i <= c; i++) { int x, y, z; cin >> x >> y >> z; ans.AddEdge(x - 1, y - 1, z); ans.AddEdge(y - 1, x - 1, z); } cout << "Network " << p++ << endl; cout << "The bandwidth is "; cout << ans.Maxflow(s - 1, t - 1) <<"."<< endl; cout << endl; } return 0; }
相关文章推荐
- UVa 514 - Rails
- [优先队列] POJ2010 Moo University - Financial Aid
- HDU 1848 Fibonacci again and again(SG函数)
- underscore chain
- Slave failed to initialize relay log info structure from the repository
- 游戏人工智能 状态驱动智能体设计——消息功能
- hdu 5381 The sum of gcd 原来有个算法叫莫队 2015 Multi-University Training Contest 8
- 1079. Total Sales of Supply Chain (25)
- PAT 1086. Tree Traversals Again (25)
- Hive的Explain命令
- How To run OAI eNB (No S1) with USRP X310(1)
- pywin32 ImportError: DLL load failed: 找不到指定的模块
- 关于百度地图开发中可能遇到Conversion to Dalvik format failed with error 1
- 2015 Multi-University Training Contest 8 hdu 5381 The sum of gcd
- atomic和nonatomic assign retain
- HDOJ 4460 Friend Chains 图的最长路
- 2015 Multi-University Training Contest 8(HDOJ5384、5389)
- 2015 Multi-University Training Contest 8
- Testng 运行报错:"Total tests run: 0, Failures: 0, Skips: 0"以及找不到class文件的问题
- [观察]HDOJ1021Fibonacci Again