CODEVS——T 1993 草地排水 USACO
题目描述 Description
在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水。这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间。因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没的烦恼(不用担心,雨水会流向附近的一条小溪)。作为一名一流的技师,农夫约翰已经在每条排水沟的一端安上了控制器,这样他可以控制流入排水沟的水流量。
农夫约翰知道每一条排水沟每分钟可以流过的水量,和排水系统的准确布局(起点为水潭而终点为小溪的一张网)。需要注意的是,有些时候从一处到另一处不只有一条排水沟。
根据这些信息,计算从水潭排水到小溪的最大流量。对于给出的每条排水沟,雨水只能沿着一个方向流动,注意可能会出现雨水环形流动的情形。
输入描述 Input Description
第1行: 两个用空格分开的整数N (0 <= N <= 200) 和 M (2 <= M <= 200)。N是农夫John已经挖好的排水沟的数量,M是排水沟交叉点的数量。交点1是水潭,交点M是小溪。
第二行到第N+1行: 每行有三个整数,Si, Ei, 和 Ci。Si 和 Ei (1 <= Si, Ei <= M) 指明排水沟两端的交点,雨水从Si 流向Ei。Ci (0 <= Ci <= 10,000,000)是这条排水沟的最大容量。
输出描述 Output Description
输出一个整数,即排水的最大流量。
样例输入 Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
样例输出 Sample Output
50
数据范围及提示 Data Size & Hint
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 #define LL long long 8 const LL INF(0x7fffffff); 9 const int N(233); 10 int n,m,u,v,w; 11 12 int head[N],sumedge; 13 struct Edge 14 { 15 int to,next; 16 LL val; 17 Edge(int to=0,int next=0,LL val=0): 18 to(to),next(next),val(val){} 19 }edge[N<<1]; 20 inline void ins(int u,int v,int w) 21 { 22 edge[++sumedge]=Edge(v,head[u],w); 23 head[u]=sumedge; 24 } 25 struct Type_AC 26 { 27 int s,t; 28 LL ret; 29 int deep[N]; 30 bool BFS() 31 { 32 memset(deep,-1,sizeof(deep)); 33 int tail=0,hd=0,que[N]; 34 deep[s]=0; que[tail++]=s; 35 for(;hd<tail;) 36 { 37 int fro=que[hd++]; 38 for(int i=head[fro];i;i=edge[i].next) 39 { 40 int v=edge[i].to; 41 if(deep[v]==-1&&edge[i].val) 42 { 43 deep[v]=deep[fro]+1; 44 que[tail++]=v; 45 } 46 } 47 } 48 if(deep[t]!=-1) return true; 49 return false; 50 } 51 LL DFS(int now,LL flow) 52 { 53 if(now==t||!flow) return flow; 54 LL go,flux=0; 55 for(int i=head[now];i;i=edge[i].next) 56 { 57 int v=edge[i].to; 58 if(deep[v]!=deep[now]+1||edge[i].val<=0) continue; 59 go=DFS(v,min(edge[i].val,flow-flux)); 60 edge[i].val-=go; 61 edge[i^1].val+=go; 62 flux+=go; 63 if(flux==flow) return flow; 64 } 65 if(!flux) deep[now]=-1; 66 return flux; 67 } 68 void Dinic() 69 { 70 for(;BFS();) ret+=(LL)DFS(s,INF); 71 } 72 }I_want_AC; 73 74 75 int main() 76 { 77 scanf("%d%d",&n,&m); 78 for(int i=1;i<=n;i++) 79 { 80 scanf("%d%d%d",&u,&v,&w); 81 ins(u,v,(LL)w); ins(v,u,(LL)0); 82 } 83 I_want_AC.s=1; 84 I_want_AC.t=m; 85 I_want_AC.Dinic(); 86 printf("%lld\n",I_want_AC.ret); 87 return 0; 88 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。