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