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,汇点为n,源点处是水池,通过水沟排水到汇点河流,问最大排水量。
算法分析:最大流的模型,每条水沟有最大的排水量,通过建模dinic一遍就ok了。
参考代码为:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 #define INF 0x3f3f3f3f 10 const int N = 210; 11 //Dinic 12 int n,m,s,t,u,v,w; 13 struct Edge { //cap为容量, flow为实际流量 14 int from, to, cap, flow; 15 }; 16 vector<Edge> edges; 17 vector<int> G[N]; 18 bool vis[N]; 19 int d[N], cur[N]; 20 21 void Init() 22 { 23 memset(d,0,sizeof d); 24 for(int i=0;i<=n;i++) G[i].clear(); 25 } 26 27 void AddEdge(int from, int to, int cap) 28 { 29 edges.push_back((Edge){from, to, cap, 0}); 30 edges.push_back((Edge){to, from, 0, 0}); 31 int m = edges.size(); 32 G[from].push_back(m-2); G[to].push_back(m-1); 33 } 34 35 bool bfs() 36 { 37 memset(vis,0,sizeof vis); 38 queue<int> q; 39 q.push(s); 40 d[s] = 0; vis[s] = 1; 41 while (!q.empty()) 42 { 43 int x = q.front(); q.pop(); 44 for(int i = 0; i < G[x].size(); ++i) 45 { 46 Edge &e = edges[G[x][i]]; 47 if (!vis[e.to] && e.cap > e.flow) 48 { 49 vis[e.to] = 1; 50 d[e.to] = d[x] + 1; 51 q.push(e.to); 52 } 53 } 54 } 55 return vis[t]; 56 } 57 58 int dfs(int x,int a) 59 { 60 if(x == t || a == 0) return a; 61 int flow = 0, f; 62 for(int &i = cur[x]; i < G[x].size(); ++i) 63 { 64 Edge &e = edges[G[x][i]]; 65 if (d[e.to] == d[x] + 1 && (f=dfs(e.to, min(a, e.cap-e.flow))) > 0) 66 { 67 e.flow += f; 68 edges[G[x][i]^1].flow -= f; 69 flow += f; a -= f; 70 if (a == 0) break; 71 } 72 } 73 return flow; 74 } 75 76 int Maxflow(int s, int t) 77 { 78 int flow = 0; 79 while (bfs()) 80 { 81 memset(cur,0,sizeof cur); 82 flow += dfs(s, INF); 83 } 84 return flow; 85 } 86 87 int main() 88 { 89 while(scanf("%d%d",&n,&m)!=EOF) 90 { 91 Init(); 92 for(int i=1;i<=n;i++) 93 { 94 scanf("%d%d%d",&u,&v,&w); 95 AddEdge(u,v,w); 96 } 97 s=1,t=m; 98 printf("%d ",Maxflow(1,m)); 99 } 100 101 return 0; 102 }