Portal:
No previous exposure to the maximum flow problem, just beginning to think is to use the kruscal algorithm to find the minimum spanning tree, and the title is also the topic of showing the smallest tree:
Anyway OK, tinkering for two days, finally a little understanding, and so will come back to tidy up the knowledge points.
1#include <iostream>2#include <queue>3#include <algorithm>4#include <cstring>5#include <cstdio>6 using namespacestd;7 Const intn=201;8 intMaxdata=1<< -;9 intCapacity[n][n];//record the capacity of the remaining network:Ten intFlow[n];//marks the actual amount of traffic available from the source point to the current node One intPre[n];//marks the predecessor of the current node on this path, and also marks whether the node is in the queue A intn,m; -queue<int>Q; - intBFsintSrcintDes//find an augmented path the { - inti,j; - while(!q.empty ())//Empty Queue - Q.pop (); + for(i=1; i<m+1; i++) -pre[i]=-1; +pre[src]=0; Aflow[src]=MaxData; at Q.push (SRC); - while(!q.empty ()) - { - intindex=Q.front (); - Q.pop (); - if(Index==des)//Find the Augmented path in Break; - for(i=1; i<=m;i++) to if(/*i!=src&&*/Capacity[index][i]>0&&pre[i]==-1) + { -Pre[i]=index;//Recording precursor theFlow[i]=min (Capacity[index][i],flow[index]);//key: Finding increments for iterations * Q.push (i); $ }Panax Notoginseng } - if(pre[des]==-1)//there is no longer an augmented path in the residue map the return-1; + Else A returnFlow[des]; the } + - intMaxflow (intSrcintDes//To find the maximum flow $ { $ intIncreasement=0; - intsumflow=0; - while((Increasement=bfs (src,des))!=-1) the { - intK=des;//using the precursor to find the pathWuyi while(k!=src) the { - intLast =Pre[k]; WuCapacity[last][k]-=increasement;//change the capacity of the forward direction -Capacity[k][last]+=increasement;//changing the reverse capacity Aboutk=Last ; $ } -sumflow+=increasement; - } - returnSumflow; A } + the intMain () - { $ inti,j; the intStart,end,ci; the while(~SCANF ("%d%d",&n,&m)) the { thememset (capacity,0,sizeof(capacity)); -memset (Flow,0,sizeof(flow)); in for(i=0; i<n;i++) the { thescanf"%d%d%d",&start,&end,&ci); AboutCapacity[start][end]+=ci;//Note that there may be multiple occurrences of the same start point the } theprintf"%d\n", Maxflow (1, M)); the } + return 0; - } the
Drainage network flow--Maximum stream EK algorithm