Tag: str code BRE i++ node cannot find CLU void ret
#include <queue>#include<cstdio>#include<iostream>#include<cstring>using namespacestd;Const intMAXN = -;Const intINF = (1<< -);intCAP[MAXN][MAXN],FLOW[MAXN][MAXN];//cap record capacity, flow record trafficintM//Number of ArcsintEdmondskarp (intSintt) { intP[MAXN],A[MAXN]; Queue<int>Q; memset (Flow,0,sizeof(flow));//initializing an array of traffic intf=0; while(true) {memset (A,0,sizeof(a)); A[s]=INF; Q.push (s); while(!q.empty ())//BFS find augmented Road { intu=Q.front (); Q.pop (); for(intv=1; v<=n;v++) if(!a[v] && cap[u][v]>flow[u][v])//because A[i] is always positive, use a[i] instead of the VIS array. { //Find a new node vp[v]=u; Q.push (v); A[V]=min (a[u],cap[u][v]-Flow[u][v]); } } if(A[t] = =0) Break;//not found, indicating that the current stream is already the maximum stream for(intu=t;u!=s;u=P[u]) {Flow[p[u]][u]+ = A[t];//Update forward TrafficFlow[u][p[u]]-= a[t];//Update Reverse Traffic} f+ = A[t];//update traffic flowing from S } returnF; }intMainvoid) {cin>>m; intu,v; for(intI=1; i<=m;i++) {cin>> u >>v; CIN>> cap[u+1][v+1]; Cap[v+1][u+1]=0; } intf = Edmondskarp (1,6); cout<<F; return 0; }/*0 1 8 0 2 4 1 3 2 1 4 2 2 1 4 2 3 1 2 4 4 3 4 6 3 5 9 4 5 7*/
Result is 8
//re-wrote a practice.#include <cstdio>#include<queue>#include<cstring>#include<iostream>#include<algorithm>Const intMAXN = -;Const intINF =0xFFFFFFF;using namespacestd;intCAP[MAXN][MAXN],FLOW[MAXN][MAXN];intM,n;intEdmondskarp (intSintt) { intp[maxn],a[maxn],f=0; //use the P array to randomly record a path that can increase trafficmemset (Flow,0,sizeof(flow)); while(true) {memset (A,0,sizeof(a)); A[s]=INF; Queue<int>Q; Q.push (s); while(!Q.empty ()) { intU =Q.front (); Q.pop (); for(intv=0; v<n;v++) if(a[v]==0&& cap[u][v]>Flow[u][v]) {P[v]=u; Q.push (v); A[V]=min (a[u],cap[u][v]-Flow[u][v]); } } for(intu=t;u!=s;u=P[u]) {Flow[p[u]][u]+=A[t]; Flow[u][p[u]]-=A[t]; } if(a[t]==0) Break; F+=A[t]; } returnF;}intMainvoid) {cin>> N >>m; for(intI=1; i<=m;i++) { intu,v; CIN>> u >>v; CIN>>Cap[u][v]; Cap[v][u]=0; } intMaxflow = Edmondskarp (0, N-1); cout<<Maxflow; return 0; }
Augmented Path Algorithm---network flow