Algorithm
1. Maximum Flow
(1) capacity limit: for ∀u,v∈v, F (u,v) ≤c (u,v) is Required.
(2) anti-symmetry: for ∀u,v∈v, F (u,v) =−f (v,u) is Required.
(3) flow balance: for ∀u∈v−{s,t}, ∑f (u,v) = 0 is Required.
Dinic
- Calculates the hierarchy graph based on the residue Network.
- Use DFS in a hierarchy to augment along a blocking stream, regardless of the maximum flow level graph in reverse arcs, until there is no augmented path
- Repeat the above steps until you are unable to augment
intcur[n];intvis[n],d[n],q[n],head,tail;BOOLBFS () {memset (vis,0,sizeof(vis)); Memset (d,0,sizeof(d)); Head=tail=1; Q[tail++]=s;d[s]=0; vis[s]=1; while(head!=Tail) { intu=q[head++]; for(intI=h[u];i;i=e[i].ne) { intv=e[i].v; if(!vis[v]&&e[i].c>E[i].f) {vis[v]=1;d [v]=d[u]+1; Q[tail++]=v; if(v==t)return 1; } } } return 0;}intDfsintUintA) { if(u==t| | a==0)returna; intflow=0, f; for(int&i=cur[u];i;i=e[i].ne) { intv=e[i].v; if(d[v]==d[u]+1&& (f=dfs (v,min (a,e[i].c-e[i].f)) >0) {flow+=f; E[I].F+=f; E[((i-1)^1)+1].f-=f; A-=f; if(a==0) break; } } returnflow;}intdinic () {intflow=0; while(bfs ()) { for(intI=s;i<=t;i++) cur[i]=h[i]; Flow+=DFS (s,inf); } returnflow;}
View Code
2. Minimum cut
Dual problem of maximum flow
Stream network G = (v,e) cut (cut) [s,t] divides the point set V into two parts of S and T (t =v−s),
Makes the source s∈s and sinks T∈t. The symbol [s,t] represents an edge collection {u,v | u,v∈e,u∈s,v∈t}.
The net flow through the cut [s,t] is defined as F (s,t), and the capacity (capacity) of the cut [s,t] is defined as C (s,t), which is generally recorded as c[s,t].
The smallest cut of a network (minimum Cut) is the smallest cut in the Network.
At the end of the augmented path algorithm, all the points that have flow (from S) are composed of s, and no traffic points make up t
The maximum flow flow is the minimum cut capacity
3. Minimum cost maximum flow
(1) SPFA Fee Flow
Use SPFA to find the shortest path to augment
Save pre[i] and pos[i] are the parent and incoming edges in the shortest path, respectively
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespaceStd;typedefLong Longll;Const intn=5005, m=5e4+5, inf=1e9;intRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*f;}intn,m,s,t,u,v,w,c;structedge{intv,ne,c,f,w;} E[m<<1];intCnt,h[n];inlinevoidInsintUintVintCintW) {cnt++; E[CNT].V=v;e[cnt].c=c;e[cnt].f=0; e[cnt].w=w; E[cnt].ne=h[u];h[u]=cnt; CNT++; E[CNT].V=u;e[cnt].c=0; e[cnt].f=0; e[cnt].w=-w; E[cnt].ne=h[v];h[v]=cnt;}intD[n],pre[n],pos[n],q[n],head=1, tail=1, Inq[n];inlinevoidLopint&x) {if(x==n) x=1;Else if(x==0) x=n-1;}BOOLSPFA () {memset (d,127,sizeof(d)); d[s]=0;p re[t]=-1; Head=tail=1; Memset (inq,0,sizeof(inq)); Q[tail++]=s;inq[s]=1; while(head!=Tail) { intU=q[head++];lop (head); inq[u]=0; for(intI=h[u];i;i=e[i].ne) { intv=e[i].v,w=e[i].w; if(d[v]>d[u]+w&&e[i].c>E[i].f) {d[v]=d[u]+w; pre[v]=u; pos[v]=i; if(!Inq[v]) { if(d[v]<d[q[head]]) head--, Lop (head), q[head]=v; Elseq[tail++]=V,lop (tail); inq[v]=1; } } } } returnpre[t]==-1?0:1;}voidMCMF () {ll Flow=0, cost=0; while(SPFA ()) {intf=INF; for(intI=t;i!=s;i=pre[i]) F=min (f,e[pos[i]].c-e[pos[i]].f); Flow+=f; cost+=f*d[t]; for(intI=t;i!=s;i=pre[i]) {E[POS[I]].F+=f; E[((pos[i)-1)^1)+1].f-=f; }} printf ("%lld%lld", flow,cost);}intMainintargcConst Char*Argv[]) {n=read (); m=read (); s=read (); t=Read (); for(intI=1; i<=m;i++) {u=read (); v=read (); c=read (); w=Read (); INS (u,v,c,w); } MCMF (); return 0;}
View Code
(2) zkw Fee Flow
http://www.artofproblemsolving.com/community/c1368h1020435
"modeling"
1. The issue of equitable distribution
assigning x to y, an x has two Y optional, giving the fewest
Binary graph model, x and y constitute Two-part graph
Two min. min. Max mid
S--1-->x--1-->y--mid-->t
See maxflow==| X|
2. Maximum closed sub-graph
A closed graph (closure) that defines a directed graph G = (v,e) 10 is a set of points for the directed graph, and all the out edges of that point set also point to that point Set. The arbitrary successor of any point within the closed graph must also be in the closed Diagram.
Adding source S and sinks T on the basis of the original point set;
Replace each forward Edge u,v∈e of the original image with a forward edge u,v∈en with a capacity of C (u,v) =∞;
Increase the connection source s to the original image each positive point v (wv >0) of the forward edge s,v∈en, the capacity of C (s,v) =wv;
Increase the connection to the original of each negative weight point v (wv <0) to the sink T of the forward edge v,t∈en, the capacity is C (v,t) =−WV
(this way down two sides right are positive)
S--point right--positive weight point----INF----negative Right point--| point right |-->t
There is a detailed proof in the hu-waves paper
We can also simply think about the minimum cut, either (1) cut the s--> positive u, or (2) cut the negative v-->t
(1) the equivalent of not choosing this u, his successor will not need to choose, while the loss of Wu
(2) the equivalent of selecting u, while selecting his successor v, so loss WV
3. Two maximum matching of graphs
s--1-->x--1-->y--1-->t
4. Minimum path coverage problem
The minimum path coverage for G is the path coverage with the least number of path bars in G.
A bit of a reverse-thinking feeling.
Worst case, All points are a path.
Two dots, one less path, one point.
Split into points x and out point y, constitute a binary graph, ans=n-maximum number of matches
5.
Network flow algorithm and modeling summary