The simplest Ford-fulkerson algorithm for "network flow" maximum flow

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.