[Network Stream]hdu3046 (dinic)

Source: Internet
Author: User

First I want to talk about the basics of network streaming.
First, we must understand some basic concepts, otherwise the understanding of the latter will have a certain impact. This list is only commonly used, and too many columns can affect the efficiency of reading.

Basemap: If you remove the direction of each edge of a directed graph, the resulting non-directed graph is called the basemap of the original diagram.
Pathway: A sequential alternating sequence of dots at the midpoint of Figure G is called a pathway of G.
Trace: The path in Figure g where the edges do not recur is called traces.
Road: A trace in Figure g where the vertices do not recur is called a road.

Basic concepts of the network:
Definition: A network n= (v,a) refers to an undirected graph that is connected without loops and satisfies the following conditions:
1. There is a fixed-point subset X, with each vertex having a degree of 0;
2. There is a subset of vertices with X that does not intersect Y, with each vertex having a degree of 0;
3. Each arc has a non-negative weight value, called the capacity of the arc.
The above network n is recorded as n (v,x,y,a,c), where X is the set of source points for the network, Y is called the network sink set, and V and A are respectively called vertex sets and arc sets. c is the capacity function of the network.

Feasible flow of the network:
A feasible flow in network n= (V,X,Y,A,C) refers to an integer function f defined on a, which makes:
1. For any a belongs to A,0<=f (a) <=c (a) (capacity constraint);
2. To any v belongs to V (Xuy), F (v) =f ' (v) (flow conservation);
where F (v) represents the sum of the flow of the arc of the Point V, and F ' (v) represents the sum of the flow of the arc of the Point v.

Maximum flow minimum cut theorem: In either network, the maximum flow flow is equal to the minimum cut capacity.
This theorem is not useful for how to find the maximum flow, but it can be used to prove the correctness of the maximum flow.

Solution for maximum flow:
The basic idea of the maximum flow in the network is to continuously increase the flow rate until it can no longer be increased. This introduces the concept of an augmented path.
Road : Set U,V is any two points in the network N, p is a way of connecting Uvs in the basemap of N, if the direction of P is from U to V, it is said that the direction of the road p for the network n a road from U to V, referred to as U-v Road. In particular, a path from source x to meeting point y is called X-y path.
The forward arc is whether the side of the road is a forward arc in the original image. Not just the reverse arc.
Extensible Path : Suppose F is a feasible flow of the network, U is any point in N, P is a x-u road in network N, if any arc a on the path p, there are:
1. If arc A is a positive arc of P, then C (a)-f (a) >0;
2. If arc A is a reverse arc of P, then f (a) >0;
Then P is called an x-u path of N. In particular, an F can increase the X-y path called n an F-path.

For any one of the N in the F can be added p and p on any one arc a, assuming that
F (a) = C (a)-F (a) (forward arc)
F (a) (reverse arc)
The increased flow of the available increments is F (P) =min{f (A)}, which is called incremental.

Maximum Flow dinic algorithm:

The dinic algorithm makes use of the layered idea to do some processing to the network, simplifying some operation.
1. Incremental network (residual network)

For the feasible flow F on the network N and N, construct a new network n (f) = (V,x,y,a (f), C ') with a (f) and A capacity function C ' defined as follows:

(Why this is so strange, this sentence to knock out the blog page will not show the following two pictures and some text: Helpless, had to ... Pop the pop. )

2. if (u,v) belongs to A and F (u,v) >0, then (v,u) belongs to A (f) and C ' (u,v) =f (U,V);
The stratification is the BFS scan the network once and find the shortest distance from each point to the source point.
The next step in the layered network is to get a secondary network.
Secondary network: After the incremental network N (f) is layered, delete the vertices of the number of layers not below Y, and then delete the arcs from the upper layer to the lower layer and the arcs between the layers.

In fact, the code does not clearly see the figure of the auxiliary network, directly the BFS layered, DFS calculation f (a), until the BFS can not reach the meeting point.

Well, let's talk about the test instructions of the subject.

Test instructions: Is in a grid some is sheep, there are two gray too wolf and Red too wolf, ask how to use the least railing interception make the lamb safe.

Analysis:
Each point has a connection to the adjacent point, the capacity is 1, the wolves are connected to the meeting point, the sheep are connected to the source point, the capacity of the INF. To find the maximum flow.

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace STD;#define Read Freopen ("Q.in", "R", stdin)#define MAXN 40004#define INF 0x7fffffffintEdgenum,n,m;intD[MAXN],HEAD[MAXN];structedge{intTo,flow,next;} edge[maxn*4];intFrom,to;voidAddintUintVintFlow) {edge[edgenum].to=v;edge[edgenum].flow=flow;edge[edgenum].next=head[u];head[u]=edgenum++; edge[edgenum].to=u;edge[edgenum].flow=0; edge[edgenum].next=head[v];head[v]=edgenum++;}BOOLBFS () {memset(d,0,sizeof(d));intI,k,j; Queue<int>Q d[from]=1; Q.push (from); while(!q.empty ()) {intU=q.front (); Q.pop (); for(i=head[u];i!=-1; i=edge[i].next) {intv=edge[i].to;if(!d[v] && edge[i].flow>0) {d[v]=d[u]+1; Q.push (v);if(v==to)return true; }        }    }return false;}intDfsintUintFlow) {if(U==to | | flow==0)returnFlowintI,j,k;intCap=flow; for(i=head[u];i!=-1; i=edge[i].next) {intv=edge[i].to;if(d[v]==d[u]+1&& edge[i].flow>0)        {intX=dfs (V,min (Cap,edge[i].flow));            Cap-=x;            Edge[i].flow-=x; edge[i^1].flow+=x;if(cap==0)returnFlow }    }returnFlow-cap;}intDinic () {intsum=0; while(BFS ())) Sum+=dfs (From,inf);returnsum;}intMain () {//Read;    inti,j,x,cas=1; while(~scanf("%d%d", &n,&m)) {from=n*m+1; to=n*m+2; edgenum=0;memset(head,-1,sizeof(head)); for(i=1; i<=n;i++) { for(j=1; j<=m;j++) {scanf("%d", &x);if(x==1) Add (from, (I-1) *m+j,inf);if(x==2) Add ((I-1) *m+j,to,inf);if(I-1>=1) Add ((I-1) *m+j, (I-2) *m+j,1);if(J-1>=1) Add ((I-1) *m+j, (I-1) *m+j-1,1);if(i+1<=n) Add ((I-1) *m+j,i*m+j,1);if(j+1&LT;=M) Add ((I-1) *m+j, (I-1) *m+j+1,1); }        }intRes=dinic ();cout<<"Case"<<cas++<<": \ n"<<res<<endl; }}

PS: I think it is important to say the algorithm is attached to the code.

[Network Stream]hdu3046 (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.