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
题意:从1到m点有若干通道,通道流量有限制,求最大流量。
Edmonds-Karp
1 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 2 //int main(int argc, char** argv) 3 4 #include<stdio.h> 5 #include<string.h> 6 #include<queue> 7 #include<algorithm> 8 9 using namespace std; 10 11 #define maxn 220 12 #define INF 0xfffffff 13 #define min(a, b)(a < b ? a : b) 14 15 int maps[maxn][maxn], pre[maxn]; 16 17 bool bfs(int s, int e) // 找路 18 { 19 memset(pre, 0, sizeof(pre)); 20 queue<int> Q; 21 Q.push(s); 22 23 while(Q.size()) 24 { 25 int i = Q.front(); 26 Q.pop(); 27 if(i == e) 28 return true; 29 30 for(int j = 1; j <= e; j++) 31 { 32 if(maps[i][j] && pre[j] == 0) // 此通道还有流量可以用,并且当前点在当前路上没有被用到 33 { 34 pre[j] = i; // 表示从i->j流向 35 Q.push(j); 36 } 37 } 38 } 39 return false; 40 } 41 42 int EK(int s, int e) 43 { 44 int i, Minflow; 45 int ans = 0; 46 47 while(bfs(s, e)) //找到一条路径 48 { 49 Minflow = INF; 50 for(i = e; i != s; i = pre[i]) 51 Minflow = min(Minflow, maps[pre[i]][i]); // 从后往前找,找当前路上最小的流量限制 52 for(i = e; i != s; i = pre[i]) 53 { 54 maps[pre[i]][i] -= Minflow; // 当前管道的流量减去已经用的就是剩下可以用的 55 maps[i][pre[i]] += Minflow; //反方向加~why 56 } 57 ans += Minflow; // 总流量相加 58 } 59 return ans; 60 } 61 int main() 62 { 63 int n, m, x, y, q; 64 65 while(scanf("%d%d", &n, &m) != EOF) 66 { 67 memset(maps, 0, sizeof(maps)); 68 69 while(n--) 70 { 71 scanf("%d%d%d", &x, &y, &q); 72 maps[x][y] += q; // 管道流量也是相加 73 } 74 printf("%d ", EK(1, m)); // 75 } 76 return 0; 77 }
邻接表
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<queue> 6 7 using namespace std; 8 9 #define maxn 220 10 #define INF 0xfffffff 11 #define min(a, b)(a < b ? a : b) 12 13 int cnt; 14 int pre[maxn], used[maxn], head[maxn]; 15 16 struct node 17 { 18 int u, v, flow, next; 19 }e[maxn*2]; 20 21 void addedge(int u, int v, int flow) 22 { 23 e[cnt].u = u; 24 e[cnt].v = v; 25 e[cnt].flow = flow; 26 e[cnt].next = head[u]; 27 head[u] = cnt++; 28 } 29 30 bool bfs(int s, int E) 31 { 32 memset(used, false, sizeof(used)); 33 memset(pre, -1, sizeof(pre)); 34 queue<int> Q; 35 Q.push(s); 36 used[s] = true; 37 38 while(Q.size()) 39 { 40 s = Q.front(); 41 Q.pop(); 42 43 if(s == E) 44 return true; 45 for(int i = head[s]; i != -1; i = e[i].next) 46 { 47 if(e[i].flow && !used[e[i].v]) 48 { 49 used[e[i].v] = true; 50 pre[e[i].v] = i; 51 Q.push(e[i].v); 52 } 53 } 54 } 55 return false; 56 } 57 58 int Karp(int s, int E) 59 { 60 int ans = 0; 61 62 while(bfs(s, E)) 63 { 64 int MInflow = INF, v; 65 v = pre[E]; 66 67 while(v != -1) 68 { 69 MInflow = min(MInflow, e[v].flow); 70 v = pre[e[v].u]; 71 } 72 v = pre[E]; 73 while(v != -1) 74 { 75 e[v].flow -= MInflow; 76 e[v^1].flow += MInflow; 77 v = pre[e[v].u]; 78 } 79 ans += MInflow; 80 } 81 return ans; 82 } 83 84 int main() 85 { 86 int n, m, u, v, flow; 87 88 while(scanf("%d%d", &n, &m) != EOF) 89 { 90 cnt = 0; 91 memset(head, -1, sizeof(head)); 92 93 while(n--) 94 { 95 scanf("%d%d%d", &u, &v, &flow); 96 addedge(u, v, flow); 97 addedge(v, u, 0); 98 } 99 printf("%d ", Karp(1, m)); 100 } 101 return 0; 102 }
dinic 算法(bfs,dfs
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 6 using namespace std; 7 8 #define maxn 220 9 #define INF 0xfffffff 10 11 int used[maxn], layer[maxn], head[maxn]; 12 int End, cnt; 13 14 struct node 15 { 16 int u, v, flow, next; 17 }e[maxn*2]; 18 19 void addedge(int u, int v, int flow) 20 { 21 e[cnt].u = u; 22 e[cnt].v = v; 23 e[cnt].flow = flow; 24 e[cnt].next = head[u]; 25 head[u] = cnt++; 26 } 27 28 int bfs(int s, int E) 29 { 30 memset(used, false, sizeof(used)); 31 memset(layer, 0, sizeof(layer)); 32 queue<int> Q; 33 Q.push(s); 34 layer[s] = 0; 35 36 used[s] = true; 37 38 while(Q.size()) 39 { 40 s = Q.front(); 41 Q.pop(); 42 43 if(s == E) 44 return true; 45 46 for(int i = head[s]; i != -1; i = e[i].next) 47 { 48 int v = e[i].v; 49 50 if(e[i].flow && !used[v]) 51 { 52 used[v] = true; 53 layer[v] = layer[s] + 1; 54 Q.push(v); 55 } 56 } 57 } 58 return false; 59 } 60 61 int dfs(int s, int Maxflow) 62 { 63 if(s == End) 64 return Maxflow; 65 66 int uflow = 0; 67 68 for(int i = head[s]; i != -1; i = e[i].next) 69 { 70 int v = e[i].v, flow = e[i].flow; 71 72 if(flow && layer[v] == layer[s]+1) 73 { 74 flow = min(flow, Maxflow-uflow); 75 flow = dfs(v, flow); 76 77 uflow += flow; 78 e[i].flow -= flow; 79 e[i^1].flow += flow; 80 if(uflow == Maxflow) 81 break; 82 } 83 } 84 return uflow; 85 } 86 87 int EK(int s, int E) 88 { 89 int ans = 0; 90 91 while(bfs(s, E)) 92 ans += dfs(s, INF); 93 94 return ans; 95 } 96 97 int main() 98 { 99 int n, m, u, v, flow; 100 101 while(scanf("%d%d", &n, &m) != EOF) 102 { 103 End = m; 104 cnt = 0; 105 memset(head, -1, sizeof(head)); 106 107 while(n--) 108 { 109 scanf("%d%d%d", &u, &v, &flow); 110 addedge(u, v, flow); 111 addedge(v, u, 0); 112 } 113 printf("%d ", EK(1, m)); 114 } 115 return 0; 116 }