POJ1273(最大流)
Posted Penn000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ1273(最大流)相关的知识,希望对你有一定的参考价值。
Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 70451 | Accepted: 27391 |
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.
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
最大流入门题,裸的Edmonds-Karp。
1 //2016.9.22 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 #define N 205 7 8 using namespace std; 9 10 const int inf = 0x3f3f3f3f; 11 12 struct Edge 13 { 14 int from, to , cap, flow;//分别为边的两个端点、容量、边上的流 15 Edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f){} 16 }; 17 18 struct EdmondsKarp//Edmonds-Karp算法 19 { 20 int n, m;//结点编号0——n-1 21 vector<Edge> edges;//存边与反向弧 22 vector<int> G[N];//邻接表,G[i][j]表示结点i的第j条边在数组edges中的序号 23 int a[N];//当起点到i的可改进量 24 int p[N];//最短路树上p的入弧编号 25 26 void init(int n)//初始化 27 { 28 for(int i = 0; i < n; i++)G[i].clear(); 29 edges.clear(); 30 } 31 32 void addEdge(int from, int to, int cap)//添加边 33 { 34 edges.push_back(Edge(from, to, cap, 0)); 35 edges.push_back(Edge(to, from, 0, 0));//反向弧 36 m = edges.size(); 37 G[from].push_back(m-2);//加入编号 38 G[to].push_back(m-1); 39 } 40 41 int maxFlow(int s, int t)//最大流,s为源点,t为汇点 42 { 43 int flow = 0; 44 while(1) 45 { 46 memset(a, 0, sizeof(a)); 47 queue<int> Q; 48 Q.push(s); 49 a[s] = inf; 50 while(!Q.empty()) 51 { 52 int x = Q.front(); Q.pop(); 53 for(int i = 0; i < G[x].size(); i++) 54 { 55 Edge& e = edges[G[x][i]]; 56 if(!a[e.to] && e.cap>e.flow) 57 { 58 p[e.to] = G[x][i]; 59 a[e.to] = min(a[x], e.cap-e.flow); 60 Q.push(e.to); 61 } 62 } 63 if(a[t])break; 64 } 65 if(!a[t])break;//残余网络中不存在增广路,当前流是最大流 66 for(int u = t; u != s; u = edges[p[u]].from)//找到一条增广路 67 { 68 edges[p[u]].flow += a[t]; 69 edges[p[u]^1].flow -= a[t];//p[u]^1为p[u]的反向边 70 } 71 flow += a[t]; 72 } 73 return flow; 74 } 75 }; 76 77 int main() 78 { 79 int n, m; 80 EdmondsKarp e; 81 while(scanf("%d%d", &m, &n)!=EOF) 82 { 83 int u, v, c; 84 e.init(n); 85 for(int i = 0; i < m; i++) 86 { 87 scanf("%d%d%d", &u, &v, &c); 88 u--, v--; 89 e.addEdge(u, v, c); 90 } 91 printf("%d\n", e.maxFlow(0, n-1)); 92 } 93 94 return 0; 95 }
以上是关于POJ1273(最大流)的主要内容,如果未能解决你的问题,请参考以下文章
POJ 1273 Drainage Ditches (网络最大流)
POJ-1273-Drainage Ditches(网络流之最大流)