hdu 1532 Drainage Ditches (最大流EK算法)

直接套用模板

#include<stdio.h>
#include<string.h>
#define N 210
#define inf 10000010
int min(int a,int b){
	return a<b?a:b;
} 
int r[N][N],n,flow;
int vis[N],pre[N],Q[N];
int bfs(int s,int t){
	memset(pre,-1,sizeof(pre));
	memset(vis,0,sizeof(vis));
	memset(Q,0,sizeof(Q));
	int front=0,rear=1;
	pre[s]=s;
	vis[s]=1;
	Q[front]=s;
	while(front<rear){
		int p=Q[front++];
		for(int i=1;i<=n;i++){
			if(r[p][i]&&!vis[i]){
				pre[i]=p;
				vis[i]=1;
				if(i==t) return 1;
				Q[rear++]=i;
			}
		}
	} 
	return 0;
}
int E_k(int s,int t){
	int d,i;
	flow=0;
	while(bfs(s,t)){
		d=inf;
		for(i=t;i!=s;i=pre[i]){
			d=min(d,r[pre[i]][i]);
		}
		for(i=t;i!=s;i=pre[i]){
			r[pre[i]][i]-=d;
			r[i][pre[i]]+=d;
		}
		flow+=d;
	}
	return flow;
}
int main(){
	int m,i,j,a,b,val;
	while(~scanf("%d%d",&m,&n)){
		memset(r,0,sizeof(r));
		for(i=0;i<m;i++){
			scanf("%d%d%d",&a,&b,&val);
			r[a][b]+=val;
		}
		printf("%d\n",E_k(1,n));
	}
	return 0;
}
 


你可能感兴趣的:(hdu 1532 Drainage Ditches (最大流EK算法))