Hiho, 115th Week, Ff,ek,dinic.

Source: Internet
Author: User
Tags dashed line

Topic 1: Network Flow one · Ford-fulkerson algorithm time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description

Small hi and Small ho live in P city, p City is a big big city, so also faced with a big city will encounter problems: traffic congestion.

Little ho: Every weekend comes home feeling traffic jam is a kind of suffering ah.

Small hi: Usually the traffic is also good, just one to the rush hour will be more crowded.

Little ho: If you can limit the number of cars, I do not know if there is a way to know the maximum traffic flow system, so that can be limited to a can always be a smooth quantity.

Little hi: Theoretically, there are algorithms. As early as 1955, T.E Harris raised the question of seeking maximum traffic between two points on a given network. and thus a new graph theory model is created: network flow.

Little ho: What exactly is that?

Little hi: The mathematical description of the language is given a graph g= (v,e), where each edge (U,V) has a non-negative capacity value, recorded as C (U,v) ≥0. At the same time there are two special vertices in the graph, the source point S and the meeting point T.

As an example:

Where node 1 is the source point S and Node 6 is the meeting point T.

We ask for the maximum feasible flow from the source point S to the meeting point T, which is also known as the maximum flow problem.

In this example, the maximum flow is 5, respectively: 1→2→4→6, Flow is 1;1→3→4→6, flow is 2;1→3→5→6, flow is 2.

Little ho: It looks like fun, you let me think about it first.

Input

Line 1th: 2 positive integer n,m. 2≤n≤500,1≤m≤20,000.

2nd.. M+1 line: 3 integers per line u,v,c (U,V), representing an edge (U,V) and its capacity C (u,v). 1≤u,v≤n,0≤c (u,v) ≤100.

The default source point in the given diagram is 1, and the meeting point is N. There may be duplicate edges.

Output

Line 1th: An integer representing the maximum stream for the given figure G.

Hint: Ford-fulkerson algorithm

Little hi: Before you think about it, I'll tell you something about the nature of the network stream.

For any given moment, the actual flow of F (u,v), the entire stream network of graph G satisfies 3 properties:

1. Capacity limit: To any u,v∈v,f (u,v) ≤c (u,v).

2. Anti-symmetry: to any u,v∈v,f (u,v) =-F (v,u). The flow from U to V must be the opposite value of the flow from V to U.

3. Flow conservation: to any u, if u is not s or T, there must be ∑f (u,v) =0, (u,v) ∈e. That is, the sum of the flows from the U to the neighboring nodes is 0, because the flow into U is equal to the flow of the U point, and the U point itself does not "make" and "consume" traffic.

For the diagram in the example above, the corresponding F Network Diagram is (where the dashed line represents an edge (v,u) that does not actually exist):

On this basis, assuming that we use CF (U,V) to represent C (u,v)-F (u,v), we can indicate how much traffic is left on each side, which we call residual capacity.

Assuming an edge (U,V) with a capacity of 3 and using Flow f (u,v) = 2, it can be represented as: CF (U,V) =1, cf (v,u) = 2.

A graph made up of CF (U,V) is what we call a residue network.

For example, the residual Network diagram is:

Little ho, you can use the residue network as a starting point, it will be relatively simple.

Small ho: Residual network, which is the amount of traffic that can be used ... I know!

Since the residual network represents traffic that can still be used, I can find a path p from s to T, so that the CF (U,V) on all sides of the path P is greater than 0.

Assuming that the smallest CF (u,v) on the path p is equal to K, then I can make the S-t increase the flow of K.

Little hi: Yes, the maximum flow of graph G is increased by this path p, so this path p is called an augmented path.

Little ho: I probably have a simple algorithm!

First, based on the information I read, I can get the original figure G and then turn it into a residual network.

Next I look for an augmented path on the residue network, and if there is no augmented path, then the graph can no longer increase traffic.

If there is an augmented path, I will increase the maximum traffic while modifying the Edge CF (U,V) on the augmented path, and then finding the augmented path again.

The whole process is probably:

while (Findaugmentpath ())//Determine if there is an augmented path Maxflow = maxflow + Delta//MAX Stream increase modifygraph ()//Modify the augmentation path end while

Little hi: So how do you plan to find the augmented path and modify the paths?

Small ho: If you look for augmented roads, use BFS to search directly from the source point S, recording the path to each point and the minimum residual capacity on the path:

Findaugmentpath (): queue = []/    /Reset Search queue path = []     //Initialize path array to 0capacity = []//Initialize traffic array to 0visited = []  //Initialize access array as  Falsetail = 0queue[Tail] = S//Add source point to queue Capacity[s] =∞//to source point traffic is infinity visited[s] = Truei = 0While (i≤tail) u = queue[i]if (U = = T) then//has found an augmented path return capacity[t]end iffor (u, v) ∈ Residual network and CF (U,V) >0 and not visited[v]//u to V has residual capacity and V has not been accessed path[v] = u Record path Capacity[v] = min (cf (U,V), capacity[u])//record path minimum residual capacity visited[v] = Truetail = tail + 1queue[Tail] = vend Fori = i + 1End while

In the case of path modification, the use of iteration or backtracking can be done with an array of paths:

Modifygraph (): flow = Capacity[t]now = Twhile (now was not S) FA = path[now]CF (FA, now) = CF (FA, now)-FLOWCF (now, FA) = CF (now, FA) + flow//reverse residual capacity is increased now = Faend while

Small ho: In terms of time complexity, each time the search for an augmented road is O (n+m), the time complexity of each modification path is O (n). Assuming that the maximum flow of the graph is maxflow, then my algorithm time complexity is O ((n+m) *maxflow).

Little hi: Well, the algorithm you use is the simplest maximum flow solution, originally published by L.r.ford and D.r.fulkerson in 1956, and therefore also known as the Ford-fulkerson algorithm. For the first contact with the network flow, you can first try to implement this algorithm, for you to understand the network flow will be a great help.

Little ho: But little hi, I have a little doubt, although I intuitively feel that I can not find the new augmented road is already the biggest stream, but this really no problem?

Little hi: Finding the augmented path is really equivalent to finding the maximum flow, but the concrete proof is, please listen to tell.

Look at this ford-fulkerson algorithm, feeling and EK very similar, are the BFS constantly augmented.

Then, I had a number of groups did not open enough, unexpectedly is tle, later learned the dinic algorithm.

Here's a summary of the 3 algorithms:

Ford-fulkerson: is also the initial maximum flow algorithm, simply speaking, constantly DFS augmentation until the augmented path is not found.

Edmonds-karp: Is the FF deformation, constantly bfs augmentation, until the augmentation path can not be found.

Dinic:bfs tiering, DFS augmentation.

#include <bits/stdc++.h>using namespacestd;#defineMAXN 505#defineINF 0x3f3f3f3fstructedge{int  from, To,cap,flow;};structdinic{intn,m,s,t; Vector<Edge>Edge; Vector<int>G[MAXN]; BOOLVIS[MAXN]; intD[MAXN]; intCUR[MAXN]; voidAddedge (int  from,intTo,intcap) {Edge.push_back (edge) { from, To,cap,0}); Edge.push_back (Edge) {to, from,0,0}); M=edge.size (); g[ from].push_back (M-2); G[to].push_back (M-1); }    BOOLBFS () {memset (Vis,0,sizeof(VIS)); Queue<int>Q;        Q.push (s); D[s]=0; Vis[s]=1;  while(!Q.empty ()) {            intx =Q.front ();            Q.pop ();  for(intI=0; I<g[x].size (); i++) {Edge& e =Edge[g[x][i]]; if(!vis[e.to]&&e.cap>e.flow) {vis[e.to]=1; D[e.to]= D[x] +1;                Q.push (e.to); }            }        }        returnVis[t]; }    intDFS (intXinta) {if(x==t| | a==0)returnA; intFlow =0, F;  for(int& i = cur[x]; I<g[x].size (); i++) {Edge& e =Edge[g[x][i]]; if(D[x] +1==d[e.to]&& (F=dfs (E.to,min (a,e.cap-e.flow)) >0) {E.flow+=F; Edge[g[x][i]^1].flow-=F; Flow+=F; A-=F; if(a==0) Break; }        }        returnflow; }    intMaxflow (intSintt) { This->s = s; This->t =T; intFlow =0;  while(BFS ()) {memset (cur,0,sizeof(cur)); Flow+=DFS (S,inf); }        returnflow; }}sol;intMain () {intn,m; scanf ("%d%d",&n,&m);  for(intI=0; i<m;i++) {        intU,v,cap; scanf ("%d%d%d",&u,&v,&cap);    Sol.addedge (U,V,CAP); } printf ("%d\n", Sol. Maxflow (1, N)); return 0;}
View Code

Hiho, 115th week, Ff,ek,dinic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.