您的位置:首页 > 理论基础 > 计算机网络

poj 1273 第1道网络流 Edmonds-Karp算法

2012-03-18 19:30 375 查看
#include<cstdio>
#include<cstdlib>
#include<memory>
#include<cstring>
#define maxn 202
#define MAXINT 0x5f5f5f5f
using namespace std;
int c[maxn][maxn];
int pre[maxn];
int queue[maxn];

int N,M;

int EK(int,int);

int main(){
while(scanf("%d %d",&N,&M)!=EOF){
int S,E,C;
memset(c,0,sizeof(c));
for(int i=0;i<N;i++){
scanf("%d %d %d",&S,&E,&C);
c[S][E]+=C;
}
printf("%d\n",EK(1,M));
}
//while(1);
return 0;
}

int EK(int s,int t){
int p,q,u,v,flow=0,aug;
while(true){
memset(pre,-1,sizeof(pre));
for(queue[p=q=0]=s;p<=q;p++){
u=queue[p];
for(v=1;v<=M && pre[t]<0 ;v++){
if(c[u][v]>0 && pre[v]<0){
pre[v]=u;
queue[++q]=v;
}
}
if(pre[t]>0)
break;
}
if(pre[t]<0)
break;
aug=MAXINT;
for(u=pre[v=t];v!=s;v=u,u=pre[u]){
if(c[u][v]<aug)
aug=c[u][v];
}
for(u=pre[v=t];v!=s;v=u,u=pre[u]){
c[u][v]-=aug;
c[v][u]+=aug;
}
flow+=aug;
}
return flow;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: