Drainage Ditches HDU 1532 (网络流 最大流 EK模板)

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

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#define LL long long 
#define mem(a, b) memset(a, b, sizeof(a))
#define N 210 
#define MOD
using namespace std;
const int inf=1<<29;
int n, m, T;
int head[N], ver[410], edge[410], Next[410], v[N], incf[N], pre[N];
int s, t, tot, maxflow;

void add(int x, int y, int z) {
	ver[++tot]=y, edge[tot]=z, Next[tot]=head[x], head[x]=tot;
	ver[++tot]=x, edge[tot]=0, Next[tot]=head[y], head[y]=tot;
}

bool bfs() {
	mem(v, 0);
	queue<int> q;
	q.push(s); v[s]=1;
	incf[s]=inf;
	while(q.size()) {
		int x=q.front(); q.pop();
		for(int i=head[x]; i; i=Next[i])
			if(edge[i]) {
				int y=ver[i];
				if(v[y]) continue;
				incf[y]=min(incf[x], edge[i]);
				pre[y]=i;
				q.push(y), v[y]=1;
				if(y==t) return 1;
			}
	}
	return 0;
}

void update() {
	int x=t;
	while(x!=s) {
		int i=pre[x];
		edge[i]-=incf[t];
		edge[i^1]+=incf[t];
		x=ver[i^1]; 
	}
	maxflow+=incf[t];
}

int main() {
	while(scanf("%d%d",&m, &n)!=EOF) {	 
		maxflow=0;
		mem(head, 0);
		s=1, t=n;
		tot=1;
		for(int i=1; i<=m; i++) {
			int x, y, z;
			scanf("%d%d%d",&x, &y, &z);
			add(x, y, z);
		}
		while(bfs()) update();
		cout<<maxflow<<endl;
	}
} 

上一篇:[USACO4.2]草地排水Drainage Ditches--最大流--EK


下一篇:POJ1149_PIGS(网络流/EK)