Network flow (maximum flow) Ford_fulkerson algorithm petition

Source: Internet
Author: User

There are two computers in the network s and T, now want to transfer data from S to t the network in a total of n too computer some of the computers between some of the refining of a one-way communication cable is corresponding to the maximum amount of data that can be transmitted in a second, when no data transmission between other computers is, within one second s can transfer how much data

5 7
0 1 10
0 2 2
1 2 6
1 3 6
3 2 3
2 4 5
3 4 8

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<Set>#include<algorithm>#include<map>#defineMAXN 200005typedefLong Longll;#defineINF 1000000009intX,y,num;using namespacestd;structedge{intTo,cap,rev;//end of edge, capacity, reverse edge};vector<edge>mp[maxn];//adjacency DiagramBOOLVIS[MAXN];voidAdd_edge (int  from,intTo,intCap//Building Map{mp[ from].push_back (Edge) {to,cap,mp[to].size ()}); Mp[to].push_back (Edge) { from,0, mp[ from].size ()-1}); Reverse Arc}intDfsintVintTintF//to find the augmented road v,t is the end of the flow with F{   if(v==t)returnF; VIS[V]=true;  for(intI=0; I<mp[v].size (); i++) {Edge&e=Mp[v][i]; if(!vis[e.to]&&e.cap>0)       {           intD=dfs (E.to,t,min (F,e.cap));//traverse all the Paths           if(d>0) {E.cap-=d;//for increased trafficmp[e.to][e.rev].cap+=D; returnD; }       }   }   return 0;}intMax_flow (intSintt) {    intflow=0;  for(;;) {memset (Vis,0,sizeof(VIS));//Initialize        intf=DFS (S,t,inf); if(f==0)returnflow; Flow+=F; }}intMain () {intn,m; CIN>>n>>m;  for(intI=0; i<m;i++) {cin>>x>>y>>num;    Add_edge (X,y,num); }    intAns=max_flow (0,4); cout<<ans<<Endl; return 0;}

Network flow (maximum flow) Ford_fulkerson algorithm petition

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.