XTU 二分图和网络流 练习题 J. Drainage Ditches

J. Drainage Ditches

Time Limit: 1000ms
Memory Limit: 32768KB
64-bit integer IO format: %I64d      Java class name: Main
 
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

解题:哈哈 直接求最大流就是了!模板一刷,AC到手。。。。。。。。^_^

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #include <queue>
10 #define LL long long
11 #define INF 0x3f3f3f3f
12 using namespace std;
13 const int maxn = 500;
14 int cap[maxn][maxn],flow[maxn][maxn],a[maxn],link[maxn];
15 queue<int>q;
16 int main(){
17     int n,m,i,j,u,v,w,ans;
18     while(~scanf("%d%d",&n,&m)){
19         memset(cap,0,sizeof(cap));
20         memset(flow,0,sizeof(flow));
21         for(i = 0; i < n; i++){
22             scanf("%d%d%d",&u,&v,&w);
23             cap[u][v] += w;
24         }
25         while(!q.empty()) q.pop();
26         ans = 0;
27         while(true){
28             memset(a,0,sizeof(a));
29             a[1] = INF;
30             q.push(1);
31             while(!q.empty()){
32                 u = q.front();
33                 q.pop();
34                 for(v = 1; v <= m; v++){
35                     if(!a[v] && cap[u][v] > flow[u][v]){
36                         link[v] = u;
37                         q.push(v);
38                         a[v] = min(a[u],cap[u][v]-flow[u][v]);
39                     }
40                 }
41             }
42             if(a[m] == 0) break;
43             for(u = m; u != 1; u = link[u]){
44                 flow[link[u]][u] += a[m];
45                 flow[u][link[u]] -= a[m];
46             }
47             ans += a[m];
48         }
49         printf("%d\n",ans);
50     }
51     return 0;
52 }
View Code

 Dinic大法好啊

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 210;
18 int e[maxn][maxn],d[maxn],S,T,N;
19 queue<int>q;
20 bool bfs() {
21     memset(d,-1,sizeof(d));
22     q.push(1);
23     d[1] = 0;
24     while(!q.empty()) {
25         int u = q.front();
26         q.pop();
27         for(int i = 1; i <= T; i++) {
28             if(d[i] < 0 && e[u][i] > 0) {
29                 d[i] = d[u]+1;
30                 q.push(i);
31             }
32         }
33     }
34     return d[T] > 0;
35 }
36 int dfs(int u,int low) {
37     int a = 0;
38     if(u == T) return low;
39     for(int i = 1; i <= T; i++) {
40         if(e[u][i] > 0 && d[i] == d[u]+1 && (a = dfs(i,min(low,e[u][i])))) {
41             e[u][i] -= a;
42             e[i][u] += a;
43             return a;
44         }
45     }
46     return 0;
47 }
48 int main() {
49     int u,v,w,ans,flow;
50     while(~scanf("%d %d",&N,&T)) {
51         memset(e,0,sizeof(e));
52         ans = 0;
53         for(int i = 0; i < N; i++) {
54             scanf("%d %d %d",&u,&v,&w);
55             e[u][v] += w;
56         }
57         while(bfs()) while(flow = dfs(1,INF)) ans += flow;
58         printf("%d\n",ans);
59     }
60     return 0;
61 }
View Code

 ISAP大法

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 250;
18 struct arc{
19     int to,flow,next;
20     arc(int x = 0,int y = 0,int z = -1){
21         to = x;
22         flow = y;
23         next = z;
24     }
25 };
26 arc e[100000];
27 int head[maxn],p[maxn],d[maxn],gap[maxn],cur[maxn];
28 int tot,S = 1,T,q[10000],hd,tl;
29 void add(int u,int v,int flow){
30     e[tot] = arc(v,flow,head[u]);
31     head[u] = tot++;
32     e[tot] = arc(u,0,head[v]);
33     head[v] = tot++;
34 }
35 void bfs(){
36     memset(gap,0,sizeof(gap));
37     memset(d,-1,sizeof(d));
38     d[T] = 0;
39     q[tl++] = T;
40     while(hd < tl){
41         int u = q[hd++];
42         ++gap[d[u]];
43         for(int i = head[u]; ~i; i = e[i].next){
44             if(d[e[i].to] == -1){
45                 d[e[i].to] = d[u] + 1;
46                 q[tl++] = e[i].to;
47             }
48         }
49     }
50 }
51 int isap(){
52     int maxFlow = 0,flow = INF,u = S;
53     memcpy(cur,head,sizeof(head));
54     bfs();
55     while(d[S] < T){
56         int &i = cur[u];
57         for( ;~i; i = e[i].next)
58             if(e[i].flow && d[u] == d[e[i].to] + 1) break;
59         if(i > -1){
60             flow = min(flow,e[i].flow);
61             p[u = e[i].to] = i;
62             if(u == T){
63                 do{
64                     int v = p[u];
65                     e[v].flow -= flow;
66                     e[v^1].flow += flow;
67                     u = e[v^1].to;
68                 }while(u != S);
69                 maxFlow += flow;
70                 flow = INF;
71             }
72         }else{
73             if(--gap[d[u]] == 0) break;
74             d[u] = T;
75             cur[u] = head[u];
76             for(int k = head[u]; ~k; k = e[k].next)
77                 if(e[k].flow && d[e[k].to] + 1 < d[u])
78                     d[u] = d[e[k].to] + 1;
79             ++gap[d[u]];
80             if(u != S) u = e[p[u]^1].to;
81         }
82     }
83     return maxFlow;
84 }
85 int main(){
86     int x,y,z,n;
87     while(~scanf("%d %d",&n,&T)){
88         memset(head,-1,sizeof(head));
89         tot = 0;
90         while(n--){
91             scanf("%d %d %d",&x,&y,&z);
92             add(x,y,z);
93         }
94         printf("%d\n",isap());
95     }
96 }
View Code

 

dinic链式前向星版

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #define INF 0x3f3f3f3f
 6 using namespace std;
 7 const int maxn = 1000;
 8 struct arc {
 9     int to,flow,next;
10     arc(int x = 0,int y = 0,int z = -1) {
11         to = x;
12         flow = y;
13         next = z;
14     }
15 };
16 arc e[maxn];
17 int head[maxn],d[maxn],cur[maxn],tot,n,m;
18 void add(int u,int v,int flow) {
19     e[tot] = arc(v,flow,head[u]);
20     head[u] = tot++;
21     e[tot] = arc(u,0,head[v]);
22     head[v] = tot++;
23 }
24 bool bfs() {
25     queue<int>q;
26     memset(d,-1,sizeof(d));
27     d[1] = 1;
28     q.push(1);
29     while(!q.empty()) {
30         int u = q.front();
31         q.pop();
32         for(int i = head[u]; ~i; i = e[i].next) {
33             if(e[i].flow && d[e[i].to] == -1) {
34                 d[e[i].to] = d[u] + 1;
35                 q.push(e[i].to);
36             }
37         }
38     }
39     return d[n] > 0;
40 }
41 int dfs(int u,int low) {
42     if(u == n) return low;
43     int tmp = 0,a = 0;
44     for(int &i = cur[u]; ~i; i = e[i].next) {
45         if(e[i].flow > 0 && d[e[i].to] == d[u] + 1 &&(a = dfs(e[i].to,min(low,e[i].flow)))) {
46             e[i].flow -= a;
47             e[i^1].flow += a;
48             tmp += a;
49             low -= a;
50             if(!low) break;
51         }
52     }
53     if(!tmp) d[u] = -1;
54     return tmp;
55 }
56 int dinic() {
57     int ans = 0;
58     while(bfs()) {
59         memcpy(cur,head,sizeof(head));
60         ans += dfs(1,INF);
61     }
62     return ans;
63 }
64 int main() {
65     while(~scanf("%d %d",&m,&n)) {
66         memset(head,-1,sizeof(head));
67         int u,v,w;
68         for(int i = tot = 0; i < m; ++i) {
69             scanf("%d %d %d",&u,&v,&w);
70             add(u,v,w);
71         }
72         printf("%d\n",dinic());
73     }
74     return 0;
75 }
View Code

 

递归sap

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = ~0U>>2;
 4 const int maxn = 210;
 5 struct arc {
 6     int to,flow,next;
 7     arc(int x = 0,int y = 0,int z = -1) {
 8         to = x;
 9         flow = y;
10         next = z;
11     }
12 } e[maxn*maxn];
13 int head[maxn],d[maxn],gap[maxn],cur[maxn],tot,S,T,n;
14 void add(int u,int v,int flow) {
15     e[tot] = arc(v,flow,head[u]);
16     head[u] = tot++;
17     e[tot] = arc(u,0,head[v]);
18     head[v] = tot++;
19 }
20 int sap(int u,int low) {
21     if(u == T) return low;
22     int tmp = 0,a,minh = n - 1;
23     for(int &i = cur[u]; ~i; i = e[i].next) {
24         if(e[i].flow) {
25             if(d[u] == d[e[i].to] + 1 &&(a = sap(e[i].to,min(low,e[i].flow)))) {
26                 e[i].flow -= a;
27                 e[i^1].flow += a;
28                 tmp += a;
29                 low -= a;
30                 if(!low) break;
31             }
32             minh = min(minh,d[e[i].to]);
33             if(d[S] >= n) return tmp;
34         }
35     }
36     if(!tmp) {
37         if(--gap[d[u]] == 0) d[S] = n;
38         d[u] = minh + 1;
39         ++gap[d[u]];
40     }
41     cur[u] = head[u];
42     return tmp;
43 }
44 int main() {
45     int u,v,w;
46     while(~scanf("%d%d",&n,&T)) {
47         memset(head,-1,sizeof head);
48         memset(gap,0,sizeof gap);
49         memset(d,0,sizeof d);
50         tot = 0;
51         for(int i = 0; i < n; ++i) {
52             scanf("%d%d%d",&u,&v,&w);
53             add(u,v,w);
54         }
55         gap[S] = n;
56         memcpy(cur,head,sizeof cur);
57         int ret = 0;
58         S = 1;
59         while(d[S] < n) ret += sap(S,INF);
60         printf("%d\n",ret);
61     }
62     return 0;
63 }
View Code

 

posted @ 2014-07-25 16:34  狂徒归来  阅读(239)  评论(0编辑  收藏  举报