Test instructions: Gives a direction graph, and the upper limit of capacity on the edge, to find the maximum flow. (with heavy edges, to overlay the capacity cap)
Idea: Solve with the simplest ek+bfs. Each time you search for a path to the finish line, exit immediately, update ans, and then look back at the current flow status in the diagram (this depends on the record path). When the current diagram does not reach the end of the path map, the flow is already saturated, you can end the program.
1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn= $+5;7 8vector<int>Vect[n];9 intC[n][n];//capacityTen intFlow[n][n];//Flow Rate One A intA[n];//Temporary Traffic - intPath[n];//which side of the record is used to update flow and cap . - the - intBFS (intm) - { -deque<int>que; +Que.push_back (1); -a[1]=inf;//first set to infinity + A while(!que.empty ()) at { - intx=Que.front (); - Que.pop_front (); - for(intI=0; I<vect[x].size (); i++) - { - intt=Vect[x][i]; in if(!a[t] && c[x][t]>flow[x][t])//no traversal, and capacity > Flow - { toPath[t]=x;//just remember which point you reached T. +A[t]=min (A[x], c[x][t]-flow[x][t]);//either all of them flow to you, or you can get the upper limit. - Que.push_back (t); the } * } $ if(A[m])returnA[M];//as long as a path can be updated to the end of M, exit immediately. Panax Notoginseng } - return 0; the } + A intCalintm) the { + intans_flow=0; - while(true)//To find the maximum flow $ { $Memset (A,0,sizeof(a)); -memset (Path,0,sizeof(path)); - the inttmp=BFS (m); - if(!tmp)returnAns_flow;//I can't find the augmented path.Wuyians_flow+=tmp; the - intEd=m; Wu while(ed!=1)//adjust the flow and upper limit according to the path - { About int from=path[ed]; $flow[ from][ed]+=tmp;//forward Edge Plus flow -flow[ed][ from]-=tmp;//reverse edge minus flow, equivalent to Cap-flow is greater than 0. -Ed= from; - } A } + } the - intMain () $ { theFreopen ("Input.txt","R", stdin); the intN, M, St, ed, CA; the while(~SCANF ("%d%d",&n,&m)) the { - in for(intI=0; i<=m; i++) vect[i].clear (); theMemset (c,0,sizeof(c)); thememset (Flow,0,sizeof(flow)); About the for(intI=0; i<n; i++) the { thescanf" %d%d%d", &st, &ed, &CA); +Vect[st].push_back (ed);//adjacency Table -Vect[ed].push_back (ST);//The reverse side, the capacity is 0. theC[st][ed]+=ca;//The pit is here.Bayi } theCout<<cal (m) <<Endl; the - } - return 0; the}AC Code
HDU 1532 Drainage Ditches drain (network flow, maximum flow, getting started)