码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1273 Drainage Ditches (网络流Dinic模板)

时间:2017-08-29 23:00:16      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:hose   line   eof   poj   tran   names   input   分层图   which   

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

网络流Dinic模板
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <queue>
 6 #include <string>
 7 #include <cstring>
 8 using namespace std;
 9 const int inf = 0x3f3f3f3f;
10 const int maxn = 220;
11 int c[maxn][maxn];
12 int dep[maxn];
13 int n,m;
14 void pt()
15 {
16     for (int i=1;i<=n;++i){
17             for (int j=1;j<=n;++j)
18                 printf("%d ",c[i][j]);
19             printf("\n");
20         }
21     printf("==================\n");
22 }
23 int bfs (int s,int t)
24 {
25     memset(dep,-1,sizeof dep);
26     queue<int> q;
27     while (!q.empty()) q.pop();
28     dep[s] = 0;
29     q.push(s);
30     while (!q.empty()){
31         int u = q.front();
32         q.pop();
33         for (int v=1;v<=n;++v){
34             if (c[u][v]>0&&dep[v]==-1){//能到达该节点的条件是这条边有流量,而且这个点没有被访问
35                 dep[v] = dep[u]+1;
36                 q.push(v);
37             }
38         }
39     }
40     return dep[t]!=-1;
41 }
42 int dfs (int u,int mi,int t)
43 {
44     if (u==t)
45         return mi;
46     int tmp;
47     for (int v=1;v<=n;++v){
48         if (c[u][v]>0&&dep[v]==dep[u]+1&&(tmp=dfs(v,min(mi,c[u][v]),t))){//下一节点的深度是当前节点+1
49             c[u][v]-=tmp;
50             c[v][u]+=tmp;
51             return tmp;
52         }
53     }
54     return 0;//别忘写返回0!!!
55 }
56 int dinic ()
57 {
58     int ans = 0;
59     int tmp;
60     while (bfs(1,n)){//每次按照深度建立分层图,这样每次dfs的时候下一节点的深度是当前节点+1
61         while (1){
62             tmp = dfs(1,inf,n);
63             //printf("%d\n",tmp);
64             if (tmp==0)
65                 break;
66             //pt();
67             ans+=tmp;
68         }
69     }
70     return ans;
71 }
72 int main()
73 {
74     //freopen("de.txt","r",stdin);
75     while (~scanf("%d%d",&m,&n)){
76         memset(c,0,sizeof c);
77         for (int i=0;i<m;++i){
78             int u,v,cap;
79             scanf("%d%d%d",&u,&v,&cap);
80             c[u][v]+=cap;
81         }
82         printf("%d\n",dinic());
83     }
84     return 0;
85 }

 

POJ 1273 Drainage Ditches (网络流Dinic模板)

标签:hose   line   eof   poj   tran   names   input   分层图   which   

原文地址:http://www.cnblogs.com/agenthtb/p/7450555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!