Introduced:
The problem with "flow" may not only be traffic, but also the "cost" factor. Each edge of the network (VI,VJ), in addition to a given capacity CIJ, gives a unit traffic cost bij>=0. The mathematical model of the problem is to calculate the maximum flow F, so that the total transport cost B (F) =∑bij Fij (i,j∈a) takes a minimum. This is the so-called minimum cost maximum flow problem.
Shown is a road network, S is the location of the warehouse, T is the end of the material. Each edge has two numbers, the first number indicates the maximum tonnage of material that passes through the road at a certain time, and the second number indicates the cost of passing the road for each piece of material. Ask how to arrange to be transported from S to t the most goods, and make the total transport costs the least?
Algorithm idea:
Starting with f=0, it is known that f is the minimum cost flow of Flow V (F), and the remaining question is how to seek a minimum cost-scalable path for F.
Constructs a weighted directed graph W (F), whose node is the node of the original network D, turning each edge in D (VI,VJ) into two opposite sides <Vi,Vj> and <Vj,Vi>. Define the Benquan Wij in W (F) as
Therefore, in the network, the least cost-scalable path for F is sought, which is equivalent to seeking the shortest path from VS to VT in weighted graph W (f).
Template:
Program Mincost;
Const
maxn=1000;
maxm=1000*1000*2;
Type
Edge=record
U,v,r,c,next,op:longint;
End
Var
G:ARRAY[1..MAXM] of Edge;
H:ARRAY[1..MAXN] of Longint;
S,t,flow,cost,a,b,c,d,tot,n,m,i:longint;
Procedure Add (U,v,r,c:longint);
Begin
Inc (TOT);
G[tot].u:=u;
G[tot].v:=v;
G[tot].r:=r;
G[tot].c:=c;
G[tot].next:=h[u];
H[u]:=tot;
g[tot].op:=tot+1;
Inc (TOT);
G[tot].u:=v;
G[tot].v:=u;
g[tot].r:=0;
G[tot].c:=-c;
G[TOT].NEXT:=H[V];
H[v]:=tot;
g[tot].op:=tot-1;
End
function SPFA (S,t:longint;var flow,cost:longint): boolean;
Var
D,P,A:ARRAY[1..MAXN] of Longint;
INQ:ARRAY[1..MAXN] of Boolean;
Q:ARRAY[1..MAXM] of Longint;
I,u,v,tmp,f,r:longint;
Begin
Fillchar (d,sizeof (d), $7f);
Fillchar (Inq,sizeof (INQ), false);
d[s]:=0; Inq[s]:=true; p[s]:=0;
A[s]:=maxlongint;
F:=1; R:=1; Q[f]:=1;
Repeat
U:=Q[F]; Tmp:=h[u];
While Tmp<>-1 do
Begin
V:=G[TMP].V;
if (g[tmp].r>0) and (D[V]>D[U]+G[TMP].C) then
Begin
D[V]:=D[U]+G[TMP].C;
p[v]:=tmp;
A[v]:=min (A[U],G[TMP].R);
If not inq[u] Then
Begin
Inc (R); Q[r]:=u; Inq[u]:=true;
End
End
Tmp:=g[tmp].next;
End
Inc (F);
Inq[u]:=false;
Until f>r;
If D[t]=maxlongint then exit (false);
FLOW:=FLOW+A[T];
COST:=COST+D[T]*A[T];
u:=t;
while (U<>s) do
Begin
G[P[U]].R:=G[P[U]].R-A[T];
G[G[P[U]].OP].R:=G[G[P[U]].OP].R+A[T];
u:=g[p[u]].u;
End
Exit (TRUE);
End
Begin
Fillchar (H,sizeof (h), $FF);
READLN (N,M);
For I:=1 to M do
Begin
READLN (A,B,C,D);
Add (a,b,c,d);
End
flow:=0;
cost:=0;
S:=1;
T:=n;
While SPFA (S,t,flow,cost) does;
Writeln (flow, ', cost);
End.
Exercises:
POJ 2135 Farm Tour
POJ 2159
POJ 2175 Evacuation Plan
POJ 3686 The windy ' s
POJ 3068 "Shortest" pair of paths
POJ 2195 going Home
POJ 3422 Kaka ' s Matrix travels
POJ 2516
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Minimum cost maximum flow