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

 1 #include<cstdio>
2 #include<cstdlib>
3 #include<memory>
4 #include<cstring>
5 #define maxn 202
6 #define MAXINT 0x5f5f5f5f
7 using namespace std;
8 int c[maxn][maxn];
9 int pre[maxn];
10 int queue[maxn];
11
12 int N,M;
13
14 int EK(int,int);
15
16 int main(){
17 while(scanf("%d %d",&N,&M)!=EOF){
18 int S,E,C;
19 memset(c,0,sizeof(c));
20 for(int i=0;i<N;i++){
21 scanf("%d %d %d",&S,&E,&C);
22 c[S][E]+=C;
23 }
24 printf("%d\n",EK(1,M));
25 }
26 //while(1);
27 return 0;
28 }
29
30 int EK(int s,int t){
31 int p,q,u,v,flow=0,aug;
32 while(true){
33 memset(pre,-1,sizeof(pre));
34 for(queue[p=q=0]=s;p<=q;p++){
35 u=queue[p];
36 for(v=1;v<=M && pre[t]<0 ;v++){
37 if(c[u][v]>0 && pre[v]<0){
38 pre[v]=u;
39 queue[++q]=v;
40 }
41 }
42 if(pre[t]>0)
43 break;
44 }
45 if(pre[t]<0)
46 break;
47 aug=MAXINT;
48 for(u=pre[v=t];v!=s;v=u,u=pre[u]){
49 if(c[u][v]<aug)
50 aug=c[u][v];
51 }
52 for(u=pre[v=t];v!=s;v=u,u=pre[u]){
53 c[u][v]-=aug;
54 c[v][u]+=aug;
55 }
56 flow+=aug;
57 }
58 return flow;
59 }

转载于:https://www.cnblogs.com/qmath/archive/2012/03/18/2404977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值