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 }
posted @ 2012-03-18 19:30  quasi-mathematician  阅读(144)  评论(0编辑  收藏  举报