Drainage ditches
Topic Abstract: Give you M edge u,v,c. n fixed point, source point 1, Meeting point N. The maximum flow is obtained. The best entry questions, various algorithms can be taken to practice
(1): General augmented Path algorithm Ford ()
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <Set>9#include <map>Ten#include <stack> One#include <queue> A#include <sstream> -#include <iomanip> - using namespacestd; thetypedefLong LongLL; - Const intINF =0x4fffffff; - Const DoubleEXP = 1e-5; - Const intMS =10005; + Const intSIZE =10005; - + structEdge A { at intc, F; - }edges[ms][ms]; - - intN, M; - - intFlag[ms]; in intPre[ms]; - intAlpha[ms]; to + intQue[size]; - the intu; * intQs, QE; $ intI, J;Panax Notoginseng - voidFord () the { + while(1) A { the //Label Method +memset (Flag,0xFF,sizeof(flag)); -memset (PRE,0xFF,sizeof(pre)); $memset (Alpha,0xFF,sizeof(Alpha)); $flag[1] =0; -pre[1] =0; -alpha[1] =INF; theQS = QE =0; -que[qe++] =1;Wuyi the while(Qs < Qe&&flag[n] = =-1) - { WuU = que[qs++]; - for(inti =1; I <= N; i++) About { $ if(Flag[i] = =-1) - { - if(Edges[u][i].c >0&&edges[u][i].f <edges[u][i].c) - { AFlag[i] =0; Pre[i] =u; +Alpha[i] = min (Alpha[u], edges[u][i].c-edges[u][i].f); theque[qe++] =i; - } $ Else if(edges[i][u].c>0&&edges[i][u].f>0) the { theFlag[i] =0; Pre[i] =-u; theAlpha[i] =min (alpha[u], edges[i][u].f); theque[qe++] =i; - } in } the } theFlag[u] =1; About}//END of while the if(Flag[n] = =-1|| Alpha[n] = =0) the Break;//END of while the + intK1 = N, k2 =ABS (PRE[K1]); - intA =Alpha[n]; the while(1)Bayi { the if(edges[k2][k1].c>0)//use F ==inf to judge the positive theEDGES[K2][K1].F + =A; - Else -EDGES[K1][K2].F-=A; the if(K2 = =1) the Break; theK1 =K2; theK2 =ABS (PRE[K1]); -}//END of while the}//END of while the the intMax_flow =0;94 for(inti =1; I <=n; i++) the { the for(intj =1; J <=n; J + +) the {98 if(i = =1&& edges[i][j].f >0) AboutMax_flow + =edges[i][j].f; - //if (edges[i][j].f > 0)101 //printf ("%d-->%d:%d\n", I, J, EDGES[I][J].F);102 }103 }104printf"%d\n", Max_flow); the }106 107 intMain ()108 {109 intu, V, C, F; the while(SCANF ("%d%d", &m,&n)! =EOF)111 { the for(inti =0; I <= N; i++)113 for(intj =0; J <= N; J + +) the //edges[i][j].c = EDGES[I][J].F = INF; theedges[i][j].c=edges[i][j].f=0; the for(inti =0; I < m; i++)117 {118 //scanf ("%d%d%d%d", &u, &v, &c, &f);119scanf"%d%d%d", &u,&v,&c);//here is the 0 stream -EDGES[U][V].C +=c;//there may be heavy edges121 //edges[u][v].f = f;122 }123 Ford ();124 } the return 0;126}
Network flow The most classic entry question various network algorithms can be AC.