Data entry format: First enter the number of vertices n and the number of arcs M, and then enter the data for each arc. Specifies that the source point is vertex 0 and that the meeting point is a vertex n-1. The data format for each arc is: U,v,w, which represents the starting point, end point, and capacity of the arc respectively. The vertex number starts at 0.
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <string>7#include <map>8#include <stack>9#include <vector>Ten#include <Set> One#include <queue> A #pragmaComment (linker, "/stack:102400000,102400000") - #defineMAXN 1005 - #defineMAXN 2005 the #defineMoD 1000000009 - #defineINF 0x3f3f3f3f - #definePi ACOs (-1.0) - #defineEPS 1e-6 +typedefLong Longll; - using namespacestd; + A intD[MAXN];//Marking at intMP[MAXN][MAXN];//residual network, initially as original - intNUM[MAXN];//Num[i] Indicates the number of vertices with a label of I - intPRE[MAXN];//Recording precursor - intN,m,s,t;//n vertices, M-edge, source point s, meeting point T - - voidInit ()//BFS calculation designator, the label of meeting Point T is zero in { - intK; toqueue<int>Q; +memset (D,inf,sizeof(d));//initializes the D array to infinity -memset (NUM,0,sizeof(num));//Initialize num array num theQ.push (t);//meeting point t into the queue *d[t]=0;//meeting Point designator is zero $num[0]=1;//the number of vertices with a label of 0 is 1Panax Notoginseng while(!q.empty ()) - { thek=Q.front (); + Q.pop (); A for(intI=0; i<n;i++) the { + if(d[i]>=n&&mp[i][k]>0) - { $d[i]=d[k]+1; $ Q.push (i); -num[d[i]]++; - } the } - }Wuyi } the - intFindalowarc (intI//Starting from I look for permissible arcs Wu { - intJ; About for(j=0; j<n;j++) $ if(mp[i][j]>0&&d[i]==d[j]+1) - returnJ; - return-1; - } A + intRelable (intI//re-label the { - intmm=INF; $ for(intj=0; j<n;j++) the if(mp[i][j]>0) theMm=min (mm,d[j]+1); the returnMm==inf?m:mm; the } - in intMaxflow (intSintT//to find the maximum flow from the source point s the { the intflow=0, i=s,j; About intDelta//Incremental thememset (pre,-1,sizeof(pre)); the while(d[s]<N) the { +j=Findalowarc (i); - if(j>=0) the {Bayipre[j]=i; theI=J; the if(i==t)//Updating residual networks - { -Delta=INF; the for(i=t;i!=s;i=Pre[i]) theDelta=min (delta,mp[pre[i]][i]); the for(i=t;i!=s;i=Pre[i]) themp[pre[i]][i]-=delta,mp[i][pre[i]]+=Delta; -flow+=Delta; the } the } the Else94 { the intX=relable (i);//re-label thenum[x]++; thenum[d[i]]--;98 if(num[d[i]]==0)//Gap Optimization About returnflow; -d[i]=x;101 if(i!=s)102I=Pre[i];103 }104 } the returnflow;106 }107 108 intMain ()109 { the intU,v,c;//The starting point, end point, capacity of the arc111 while(SCANF ("%d%d", &n,&m)!=eof)//reads the number of vertices n and number of arcs M the {113Memset (MP,0,sizeof(MP)); the for(intI=0; i<m;i++) the { thescanf"%d%d%d",&u,&v,&c);117mp[u][v]=C;118 }119s=0, t=n-1; - init ();121printf"%d\n", Maxflow (0, N-1));122 }123 return 0;124 } the /*126 6 Ten127 0 1 8 - 0 2 4129 1 3 2 the 1 4 2131 2 1 4 the 2 3 1133 2 4 4134 3 4 6135 3 5 9136 4 5 7137 */
View Code
Shortest augmented path algorithm template for network flow (SAP)