首页 > 代码库 > hdu1532Drainage Ditches【最大流】
hdu1532Drainage Ditches【最大流】
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8431 Accepted Submission(s): 3921
Problem 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
Source
USACO 93
题意:
最大流
分析:
最大流
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <vector> 6 using namespace std; 7 8 const int maxn = 205 << 1; 9 const int INF = 1000000000; 10 11 struct Edge 12 { 13 int from, to, cap, flow; 14 }; 15 16 struct Dinic 17 { 18 int n, m, s, t; 19 vector<Edge> edges; 20 vector<int>G[maxn]; 21 bool vis[maxn]; 22 int d[maxn]; 23 int cur[maxn]; 24 25 void ClearAll(int n) { 26 for(int i = 0; i <= n; i++) { 27 G[i].clear(); 28 } 29 edges.clear(); 30 } 31 32 void AddEdge(int from, int to, int cap) { 33 edges.push_back((Edge){from, to, cap, 0} ); 34 edges.push_back((Edge){to, from, 0, 0} ); 35 m = edges.size(); 36 G[from].push_back(m - 2); 37 G[to].push_back(m - 1); 38 //printf("%din end\n",m); 39 } 40 41 bool BFS() 42 { 43 memset(vis, 0, sizeof(vis) ); 44 queue<int> Q; 45 Q.push(s); 46 vis[s] = 1; 47 d[s] = 0; 48 while(!Q.empty() ){ 49 int x = Q.front(); Q.pop(); 50 for(int i = 0; i < G[x].size(); i++) { 51 Edge& e = edges[G[x][i]]; 52 if(!vis[e.to] && e.cap > e.flow) { 53 vis[e.to] = 1; 54 d[e.to] = d[x] + 1; 55 Q.push(e.to); 56 } 57 } 58 } 59 return vis[t]; 60 } 61 62 int DFS(int x, int a) { 63 if(x == t || a == 0) return a; 64 int flow = 0, f; 65 for(int& i = cur[x]; i < G[x].size(); i++) { 66 Edge& e = edges[G[x][i]]; 67 if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) { 68 e.flow += f; 69 edges[G[x][i]^1].flow -= f; 70 flow += f; 71 a -= f; 72 if(a == 0) break; 73 } 74 } 75 return flow; 76 } 77 78 int Maxflow(int s, int t) { 79 this -> s = s; this -> t = t; 80 int flow = 0; 81 while(BFS()) { 82 memset(cur, 0, sizeof(cur) ); 83 flow += DFS(s, INF); 84 } 85 return flow; 86 } 87 }; 88 89 int main() { 90 int n, m; 91 int a, b, c; 92 while(EOF != scanf("%d %d",&n, &m)) { 93 Dinic g; 94 for(int i = 0; i < n; i++) { 95 scanf("%d %d %d",&a, &b, &c); 96 g.AddEdge(a, b, c); 97 } 98 printf("%d\n",g.Maxflow(1, m) ); 99 } 100 return 0; 101 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。