Recently learned the next maximum flow algorithm, probably thought is Mengmengdongdong, now want to record the template, in case of deep learning later.
1#include <cstdio>2#include <cstring>3 using namespacestd;4 5 #define_CLR (x, y) memset (x, y, sizeof (x))6 #defineMin (x, y) (x < y x:y)7 #defineINF 0x3f3f3f3f8 #defineN 2109 Ten intMap[n][n]; One intPre[n], sta[n]; A BOOLUsed[n]; - intN, M; - the //BFS Determines whether there is an augmented path between S--t - BOOLBFsintSintt) - { -_CLR (Used,0); +_CLR (Pre,-1); - inttop=1; +sta[top++] =s; AUsed[s] =true; at while(top) - { - intv = sta[--top]; - for(intI=1; i<=n; i++) - { - if(Map[v][i] &&!Used[i]) in { -Used[i] =true; toPre[i] =v; + if(i==t) - return true; thesta[top++] =i; * } $ }Panax Notoginseng } - return false; the } + A intEK (intSintt) the { + //Initializes a maximum of 0 streams - intmaxflow=0, D; $ $ //If there is an augmented path between the s--t - while(BFS (S, t)) - { theD =INF; - //find the smallest possible incrementWuyi for(inti=t; I!=s; I=Pre[i]) theD =Min (d, Map[pre[i]][i]); - Wu //add reverse edge to update residual network - for(inti=t; I!=s; I=Pre[i]) About { $Map[pre[i]][i]-=D; -Map[i][pre[i]] + =D; - } -Maxflow + =D; A } + returnMaxflow; the } - $ the intMain () the { the intx, y, Z; the while(~SCANF ("%d%d", &m, &N)) - { in_CLR (Map,0); the for(intI=0; i<m; i++) the { Aboutscanf"%d%d%d", &x, &y, &z); theMap[x][y] + =Z; the } theprintf"%d\n", EK (1, N)); + } - return 0; the}
Max stream EK algorithm template