The Ford-fulkerson algorithm is a very well understood algorithm. This is probably the case:
① constantly starting from the starting point Dfs find a way to the end. If no one is found, the current value is the maximum flow
② If there is still a road to the end, then add it to the shortest section, and then make a residual diagram of the diagram. Go on.
The following is the Ford-fulkerson maximum flow algorithm based on the adjacency matrix. Easy to understand, suitable for all ages.
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace STD;int Map[ -][ -];intused[ -];intN,m;Const intINF =1000000000;intDfsintSintTintf) {if(s = = t)returnF for(inti =1; I <= N; i + +) {if(Map[S] [I] >0&&!used[i]) {Used[i] =true;intD = DFS (I,t,min (F,Map[S] [i]));if(D >0) {Map[S] [i]-= D;MapI [s] + = D;returnD } } }}intMaxflow (intSintT) {intFlow =0; while(true) {memset(Used,0,sizeof(used));intf = DFS (S,t,inf);//Keep looking for an augmented path from S to T if(f = =0)returnFlow//Go back if you can't find it.Flow + + F;//Find a way to flow F}}intMain () { while(scanf("%d%d", &m,&n)! = EOF) {memset(Map,0,sizeof(Map)); for(inti =0; I < m; i + +) {intFrom,to,cap;scanf("%d%d%d", &from,&to,&cap);Map[From] [to] + = cap; }cout<< Maxflow (1, n) << Endl; }return 0;}
The simplest Ford-fulkerson algorithm for "network flow" maximum flow