A few words of nonsense:
After reading Rujia's book, feel everything is so vacant, so I find tutorials on the Internet, their own little bit of code, about three days.
Network Flow Fundamentals:
Looks like I need to talk about the basics of network streaming.
Network flow problem is to give you a diagram, each graph of Benquan called this side of the traffic, ask you from the starting point, how many values can flow through these edges to the focus
I know you didn't read it, for example:
The maximum value is
From 1 to 2 to 4 shipped 6
From 1 to 2 to 3 to 4
From 1 to 3 to 4 shipped 3
There are 10 total shipments.
For example, I say a few definitions:
Capacity, which is the weight of only one edge, indicates the maximum value that can be transported from this side
Flow, which represents the maximum value that an edge actually flows through
So, it's time to say the algorithm, or the data first.
(The data is shown)
4 5 // Four points, five sides 1 2 8 1 3 3 2 3 1 2 4 6 3 4 5
First, let's assume that the traffic on all sides is 0.
With this data, this can be achieved.
So we tried to increase some of the traffic and make the focus more traffic.
So how to increase? We need to find the way.
What is the Broad road, is a path from the beginning to the end of each side capacity-the actual flow of > Road 0.
Like what
0/8 indicates an edge flow of 0 with a capacity of 8
It is now clear that there is an 1--2--4 road
Then what to do, the traffic on each side of the road plus the minimum value of each edge capacity and flow difference
For example 8-0>6-0 so 1--2, 2--4 These two sides of traffic are added 6, and the answer is added 6
Then we went on to find the way, and we found a 1--2--3--4.
Or just to find the minimum value for 1, so this adds 1 to the traffic on each side of the road.
Then ans plus one becomes 7.
However, we continued to find the 1--3--4 road and found
According to just now, Flow plus 2, becomes
Ans Plus 3 becomes 10
Then found that there is no way out, so the algorithm ended, the answer is 10.
Wait, this is going to be the code? The legendary NOI algorithm network flow is that simple? It doesn't exist.
Careful classmates (funny) will find that this is a magical situation
You may return to the 1--2--3--4, and the answer is 1, but it turns out that the best answer is obviously 2, so what's the problem?
Because we do not give the opportunity to return, which is the equivalent of the first time to find not the optimal solution, then what to do?
So, we want to have a reverse side, to give the program back to the opportunity, each edge creates a reverse edge, the initial capacity of the reverse edge is 0, the traffic is negative.
Suppose 1--2 this edge originally weight is 1, flow is also 1, then his reverse edge capacity is 0, flow is-1, this should be good understanding.
It will become:
Then, with an example just now, when the 1--2--3--4 is determined, the figure is this:
So, we found a new way: 1--3--2--4
(Because 0-(-1) = 1, so this side can also walk)
So the answer is 1+1=2.
So the question is, why is this even one?
Because making the opposite side, such as 2--3, is equivalent to the original flow to 3 of the flow back.
This allows you to find the maximum value.
Mouth Hu content on to here, in fact, I write is not very clear, we see Rujia may be more clear, but the point is!!!
This problem code implementation is very difficult, at least for me, so Rujia code I do not understand at all.
So I wrote myself a code that was easy to understand.
No vector!!!. No pointers!!!
And there are complex annotations!!!
#include <iostream>#include<cmath>#include<cstdio>#include<algorithm>#include<cstdlib>#include<cstring>#include<queue>using namespacestd;intN,m;//n points, M edgeinthead[100010];//This is the flag of the adjacency table, Head[i] denotes the first edge of vertex with Istructedge{intCap,flow;//cap is capacity, flow is traffic int from, to;//the beginning and end of an edge intNext//Next side (adjacency table flag) with the same starting point as this one}map[100010];intindex=-1;//adjacency Table Input Arrayintflag=0;//Sign out ofvoidBuild_edge (intAintBintC//construct adjacency table, insert join table{Index++; Map[index]. from=A; Map[index].to=b; Map[index].cap=B; Map[index].flow=0; Map[index].next=Head[a]; Head[a]=index; Index++; Map[index]. from=b;//insert opposite sidemap[index].to=A; Map[index].cap=0; Map[index].flow=0; Map[index].next=Head[b]; HEAD[B]=index; return ;}intBFS ()//using BFS to find the way to the wider{ intmin_flow[100010];//Min_flow[i] Represents the current variable capacity-the minimum value of the flow.memset (Min_flow,0,sizeof(Min_flow)); Queue<int> Q;//Maintaining with Queuesmin_flow[1]=999999;//Start for Infinity intp[100010];//This is very important, when constructing the add-on road, the record current point is found by that side, in order to find the path can be from the end point to the beginning of the edge of the flow plus the minimum valuep[1]=-1; Q.push (1);//Start the queue while(!Q.empty ()) { intnow=Q.front (); Q.pop (); for(inte=head[now];e!=-1; e=map[e].next)//adjacency table enumerates all the edges of the current point { intV=map[e].cap-map[e].flow;//v is for the current edge capacity-flow if(v>0&&!min_flow[map[e].to])//when v>0 and this point does not have access to outdated{p[map[e].to]=e;//record the ordinal of the edge that reached this pointMin_flow[map[e].to]=min (MIN_FLOW[NOW],V);//Maintain minimum valueQ.push (map[e].to);//Maintenance BFS } } if(min_flow[n]!=0)//If the end point is updated, it means that you have found a way to break directly Break; } if(min_flow[n]==0)//when not updated to the end point, that is, the focus of the minimum increase of 0 o'clock, indicating that there is no increase in the road, directly finishedflag=1; for(intE=n;; E=map[p[e]]. from)//start from the end, and then go backwards to the road, the upper traffic along the way together with the end of the minimum update value { if(p[e]==-1)//if the head is done Break; Map[p[e]].flow+=min_flow[n];//Traffic Plus minimum update tomap[p[e]^1].flow-=min_flow[n];//Reverse Edge minus } returnMin_flow[n];//return minimum update to, add to answer}intMax_flow () {intflow=0;//This is the sacred answer. while(1) {Flow+=bfs ();//Circular Search if(flag==1)//without the broad road, just jump out Break; } returnFlow//Back to answers}intMain () {CIN>>n>>m; for(intI=0; i<=m;i++)//Initialize, it's important, the point of winning{Map[i].next=-1; Head[i]=-1; } for(intI=1; i<=m;i++)//read in, construct adjacency table { inta,b,c,d; CIN>>a>>b>>C; Build_edge (A,B,C); } cout<<max_flow ();//Output Answer}/*test Data 4 2 4 3 4 1*/
Learning information is not easy, homework has not moved, ask attention!!!
Maximum network flow--augmented path algorithm