[USACO4.2]Drainage Ditches

练习网络流的好题目,这里给出的是Dinic

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=250;
 4 int n,m;
 5 struct Dinic{
 6     int G[N][N],dis[N];
 7     void build(){
 8         memset(G,0,sizeof G);
 9         for(int i=1;i<=m;++i){
10             int u,v,f;
11             scanf("%d%d%d",&u,&v,&f);
12             G[u][v]+=f;
13         }
14     }
15     bool BFS(){
16         queue<int> Q;
17         memset(dis,-1,sizeof dis);
18         dis[1]=0;
19         Q.push(1);
20         while(!Q.empty()){
21             int u=Q.front();Q.pop();
22             if(u==n)return 1;
23             for(int i=1;i<=n;++i){
24                 if(~dis[i]||G[u][i]<=0)continue;
25                 dis[i]=dis[u]+1;
26                 Q.push(i);
27             }
28         }
29         return 0;
30     }
31     int dfs(int u,int low){
32         int a=0;
33         if(u==n)return low;
34         for(int i=1;i<=n;++i)
35             if(G[u][i]>0&&dis[i]==dis[u]+1){
36                 a=dfs(i,min(low,G[u][i]));
37                 G[u][i]-=a;
38                 G[i][u]+=a;
39                 if(a)return a;
40             }
41         return 0;
42     }
43     int dinic(){
44         int ans=0,t;
45         while(BFS())
46             while(t=dfs(1,0x3f3f3f3f))ans+=t;
47         return ans;
48     }
49 }D;
50 int main(){
51     while(~scanf("%d%d",&m,&n)){
52         D.build();
53         printf("%d\n",D.dinic());
54     }
55     return 0;
56 }
时间: 2024-07-29 13:24:34

[USACO4.2]Drainage Ditches的相关文章

luogu P2740 [USACO4.2]草地排水Drainage Ditches

P2740 [USACO4.2]草地排水Drainage Ditches 2017-09-17 题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪).作为一名一流的技师,农夫约翰已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量. 题目描述 农夫约翰知道每一条排水沟每分钟可以流过的水量,和

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

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

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 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

Drainage Ditches

题目链接 题意: n个点,m条边,求1点到n点的最大流 N (0 <= N <= 200) and M (2 <= M <= 200). struct Edge { int from, to, cap, flow; bool operator< (const Edge& rhs) const { return from < rhs.from || (from == rhs.from && to < rhs.to); } }; const i

hdu 1532 Drainage Ditches(最大流)

Drainage Ditches 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 drai

USACO 4.2 Drainage Ditches(网络流模板题)

Drainage DitchesHal Burch 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 se

poj-1273 Drainage Ditches(最大流基础题)

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 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 cover

POJ 1273 -Drainage Ditches

Time Limit:1000MS    Memory Limit:10000K 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,

USACO Section 4.2 Drainage Ditches

最大流问题.ISAP算法.注意可能会有重边,不过我用的数据结构支持重边.距离d我直接初始化为0,也可以用BFS逆向找一次. #include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<queue> #define rep(i,l,r) for(int i=l;i<r;i++) #define