hdu1532 board maximum flow problem

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=1532

Title given point source and drain, and some edges, requires the maximum flow between a source and a drain, I used Edmonds Karp algorithm is a Ford-Fulkerson algorithm is implemented, the algorithm is the key technology residual on the reverse side of the network and the remaining network, which is equivalent to the search strategy a chance to "go back", the implementation process of the algorithm is always looking for a source to augmenting path of the leak, size of the stream is calculated every time to find a path to accumulate until it can not find an augmenting path. General practice is looking for augmenting paths bfs, the number of iterations with dfs words can be very large, very consumption rate. In Edmonds Karp algorithm augmenting path once lookup consumes O (| E |) time in O (| V || E |} after augmentation maximum flow can be found, so the time Edmonds Karp algorithm complexity is about O (| V || E | ^ 2), the complexity of the number of cases in the side is very high, there are others such as the maximum flow algorithm Dinic, ISAP algorithm, this time I will write from a EdmondsKarp algorithm.

code show as below:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned int ui;
 4 typedef long long ll;
 5 typedef unsigned long long ull;
 6 #define pf printf
 7 #define mem(a,b) memset(a,b,sizeof(a))
 8 #define prime1 1e9+7
 9 #define prime2 1e9+9
10 #define pi 3.14159265
11 #define lson l,mid,rt<<1
12 #define rson mid+1,r,rt<<1|1
13 #define scand(x) scanf("%llf",&x) 
14 #define f(i,a,b) for(int i=a;i<=b;i++)
15 #define scan(a) scanf("%d",&a)
16 #define mp(a,b) make_pair((a),(b))
17 #define P pair<int,int>
18 #define dbg(args) cout<<#args<<":"<<args<<endl;
19 #define inf 0x3f3f3f3f
20 const int maxn=1005;
21 int n,m,t;
22 inline int read(){
23     int ans=0,a = ;
124      char CH = getchar ();
 25      the while (! Isdigit (CH)) { IF (CH == ' - ' ) = W - . 1 ; CH = getchar ();}
 26 is      the while (isdigit (CH)) ANS = ( << ANS . 3 ) + (ANS << . 1 ) + CH-, ' 0 ' , CH = getchar ();
 27      return ANS * W;
 28  }
 29  int G [MAXN] [MAXN], pre [MAXN]; // g array here not only holds the forward edge of the remaining information further holds network information 
30  int BFS ( int the src, int sink)
 31 is {
 32      int Flow [MAXN];
 33 is      MEM (pre, - . 1 );
 34 is      Flow [the src] = INF; pre [the src] = 0 ; // every time an infinite stream of water issuing from the source 
35       Queue < int > Q; q.push (the src);
 36       the while (! q.empty ())
 37 [       {
 38 is           int U = q.front ();
 39           q.pop ();
 40           IF (U == sink) BREAK ; // reaches the leak 
41 is          F (I, . 1 , n-)
 42 is          {
 43 is              IF(I = the src && pre [I] == -! . 1 && G [U] [I]> 0 ) // find a not a source but not visited and there node side of the current point 
44 is              {
 45                  pre [I] = U;
 46 is                  q.push (i);
 47                  Flow [i] = min (Flow [U], G [U] [i]); // size updated after the point i be the size of a source side or a precursor the smaller the value of the node 
48              }
 49          }
 50      }     
 51 is      IF (pre [sink] == - . 1 ) return - . 1 ; // leak is not searched 
52 is      return Flow [sink]; 
 53 is  } 
 54 is int MaxFlow ( int the src, int sink)
 55  {
 56 is      int Maxflow = 0 ;
 57 is      the while ( . 1 )
 58      {
 59          int Flow = BFS (the src, sink);
 60          IF (Flow == - . 1 ) BREAK ;
 61 is          int CUR = sink; // start point from the drain, source rollback step by step, the network updates the residue 
62 is          the while (CUR =! the src)
 63 is          {
 64              int Father = pre [CUR];
 65             G [Father] [CUR] - = Flow; // from the parent node to the flow path to reduce the current node Flow 
66              G [CUR] [Father] + = Flow; // from the current node to the parent node a residual flow increases between 
67              CUR = Father; // back update participate in the network 
68          }
 69          Maxflow + = flow;
 70      }
 71 is      return Maxflow;
 72  }
 73 is  int main ()
 74  {
 75      // the freopen ( "input.txt" , "R & lt", stdin);
 76      // The freopen ( "output.txt", "W", stdout); 
77      STD :: :: iOS sync_with_stdio ( to false );
78     while(scanf("%d%d",&m,&n)!=EOF)
79     {
80         mem(g,0);
81         int u,v,w;
82         f(i,1,m)
83         {
84             u=read(),v=read(),w=read();
85             g[u][v]+=w;
86         }
87         pf("%d\n",maxflow(1,n));
88     }
89 
90 } 

 

Guess you like

Origin www.cnblogs.com/randy-lo/p/12574881.html