标签:queue push code == log eof div turn span
Edmonds-Karp算法:
1 //Edmonds-Karp算法 2 3 const int INF=0x3f3f3f3f; 4 int n,m,s,t,Map[N][N],path[N],flow[N]; 5 6 int bfs(){ 7 int tmp; 8 queue <int> Q; 9 while(!Q.empty()) Q.pop(); 10 memset(path,-1,sizeof(path)); 11 path[s]=0;flow[s]=INF; 12 Q.push(s); 13 while(!Q.empty()){ 14 tmp=Q.front();Q.pop(); 15 if(tmp==t) break; 16 for(int i=1;i<=m;i++){ 17 if(i!=s&&path[i]==-1&&Map[tmp][i]){ 18 flow[i]=flow[tmp]<Map[tmp][i]?flow[tmp]:Map[tmp][i]; 19 Q.push(i); 20 path[i]=tmp; 21 } 22 } 23 } 24 if(path[t]==-1) return -1; 25 return flow[m]; 26 } 27 28 int Edmonds_Karp(){ 29 int max_flow=0,step,now,pre; 30 while((step=bfs())!=-1){ 31 max_flow+=step; 32 now=t; 33 while(now!=s){ 34 pre=path[now]; 35 Map[pre][now]-=step; 36 Map[now][pre]+=step; 37 now=pre; 38 } 39 } 40 return max_flow; 41 }
标签:queue push code == log eof div turn span
原文地址:http://www.cnblogs.com/Leonard-/p/7827841.html