Drainage Ditches
2015-08-12 17:24
357 查看
Drainage Ditches
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 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
邻接表
View Code
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 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
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50 题意:从1到m点有若干通道,通道流量有限制,求最大流量。
Edmonds-Karp
/* run this program using the console pauser or add your own getch, system("pause") or input loop */ //int main(int argc, char** argv) #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; #define maxn 220 #define INF 0xfffffff #define min(a, b)(a < b ? a : b) int maps[maxn][maxn], pre[maxn]; bool bfs(int s, int e) // 找路 { memset(pre, 0, sizeof(pre)); queue<int> Q; Q.push(s); while(Q.size()) { int i = Q.front(); Q.pop(); if(i == e) return true; for(int j = 1; j <= e; j++) { if(maps[i][j] && pre[j] == 0) // 此通道还有流量可以用,并且当前点在当前路上没有被用到 { pre[j] = i; // 表示从i->j流向 Q.push(j); } } } return false; } int EK(int s, int e) { int i, Minflow; int ans = 0; while(bfs(s, e)) //找到一条路径 { Minflow = INF; for(i = e; i != s; i = pre[i]) Minflow = min(Minflow, maps[pre[i]][i]); // 从后往前找,找当前路上最小的流量限制 for(i = e; i != s; i = pre[i]) { maps[pre[i]][i] -= Minflow; // 当前管道的流量减去已经用的就是剩下可以用的 maps[i][pre[i]] += Minflow; //反方向加~why } ans += Minflow; // 总流量相加 } return ans; } int main() { int n, m, x, y, q; while(scanf("%d%d", &n, &m) != EOF) { memset(maps, 0, sizeof(maps)); while(n--) { scanf("%d%d%d", &x, &y, &q); maps[x][y] += q; // 管道流量也是相加 } printf("%d\n", EK(1, m)); // } return 0; }
邻接表
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define maxn 220 #define INF 0xfffffff int used[maxn], layer[maxn], head[maxn]; int End, cnt; struct node { int u, v, flow, next; }e[maxn*2]; void addedge(int u, int v, int flow) { e[cnt].u = u; e[cnt].v = v; e[cnt].flow = flow; e[cnt].next = head[u]; head[u] = cnt++; } int bfs(int s, int E) { memset(used, false, sizeof(used)); memset(layer, 0, sizeof(layer)); queue<int> Q; Q.push(s); layer[s] = 0; used[s] = true; while(Q.size()) { s = Q.front(); Q.pop(); if(s == E) return true; for(int i = head[s]; i != -1; i = e[i].next) { int v = e[i].v; if(e[i].flow && !used[v]) { used[v] = true; layer[v] = layer[s] + 1; Q.push(v); } } } return false; } int dfs(int s, int Maxflow) { if(s == End) return Maxflow; int uflow = 0; for(int i = head[s]; i != -1; i = e[i].next) { int v = e[i].v, flow = e[i].flow; if(flow && layer[v] == layer[s]+1) { flow = min(flow, Maxflow-uflow); flow = dfs(v, flow); uflow += flow; e[i].flow -= flow; e[i^1].flow += flow; if(uflow == Maxflow) break; } } return uflow; } int EK(int s, int E) { int ans = 0; while(bfs(s, E)) ans += dfs(s, INF); return ans; } int main() { int n, m, u, v, flow; while(scanf("%d%d", &n, &m) != EOF) { End = m; cnt = 0; memset(head, -1, sizeof(head)); while(n--) { scanf("%d%d%d", &u, &v, &flow); addedge(u, v, flow); addedge(v, u, 0); } printf("%d\n", EK(1, m)); } return 0; }
View Code
相关文章推荐
- 220 Contains Duplicate III
- Contains Duplicate III
- stl 静态数组初始化vector及pair,vector混合使用
- HDU 5372 Segment Game(2015 Multi-University Training Contest 7 1004)
- container_of()详解
- TIME_WAIT 与 CLOSE_WAIT
- 2015 Multi-University Training Contest 7(HDOJ5379)
- 2015 Multi-University Training Contest 6(hdu5362 - Just A String)dp求期望
- 219 Contains Duplicate II
- OC_ self , 属性特性readwrite readonly assign retain copy nonatomic
- [BZOJ1660] [Usaco2006 Nov]Bad Hair Day 乱发节
- TEXT CLASSIFICATION FOR SENTIMENT ANALYSIS – NAIVE BAYES CLASSIFIER
- 2015 Multi-University Training Contest 7 1011
- HDU 4750 Count The Pairs
- 1086. Tree Traversals Again (25)
- 1086. Tree Traversals Again (25)
- hdu 3571 Hotaru's problem || 2015 Multi-University Training Contest 7 (manacher算法)
- 217 Contains Duplicate
- hdu 1532 Drainage Ditches 最大流 dinic算法
- hdu 1532 Drainage Ditches 最大流 dinic算法