Problem description
W Company has m warehouses and N retail stores. The first warehouse has an AI unit of goods, and the first J retail stores need BJ units of goods. Balance of goods supply and demand . The cost of transporting each unit of goods from the I warehouse to the J retail store is c[i,j]. Try to design a transport plan that transports all the goods in the warehouse to the retail store, so that the total transportation costs are minimized.
Programming tasks
for the cost of transporting goods between a given m warehouse and N retail stores, calculate the optimal transport plan and the worst and the transmission scheme.
Data entry
the 1th line of the input file has 2 positive integers m and n, respectively, indicating the number of warehouses and number of retail stores. The next line has m positive integer ai, 1≤i≤m, which indicates that the first warehouse has an AI unit of goods . In the next line, there are n positive integer bj,1≤j≤n, which indicates that the first J retail store needs BJ units of goods . The next M-line, with n integers per line, represents the cost of shipping each unit of goods from the I warehouse to the J retail store C[i,j].
Result output
At the end of the program run, the output calculates the minimum shipping cost and the maximum shipping cost.
Input File Example
Input.txt
2 3
220 280
170 120 210
77 39 105
150 186 122
Output File Example
Output.txt
48500 69140
Consider all warehouses as Vertex XI in a binary graph, and all retail stores as Vertex Yi in a binary map to create an additional source S-sink T.
1, from S to each XI a capacity for the warehouse in the quantity of goods AI, the cost of 0 of the forward edge.
2, from each Yi to t even a capacity for the store to the number of goods required bi, the cost of 0 of the forward edge.
3, from each XI to each YJ to connect a capacity for infinity, the cost of CIJ of the forward edge.
The problem is to ask for a network of the minimum cost maximum flow and maximum cost of the maximum flow, the minimum cost of the maximum flow is skipped, the maximum cost maximum flow has 2 methods:
1. Turn all expenses into opposite number to do the maximum flow of the minimum cost, output the inverse number of the answer;
2. When initializing SPFA, the DIS array is changed from Max to-1, and the condition of relaxation is changed from dis[i]>dis[j]+cap[i,j] to dis[i]<dis[j]+cap[i,j];
I used the first method here.
Code:
1 Const2maxn=100000000;3 4 var5Ot,ot1,ne1,cap1,ne,cap,h:Array[0..30000] ofLongint;6Cost,cost1:Array[0..30000,1..2] ofLongint;7G,g1,pre,dis:Array[0..1010] ofLongint;8InqArray[0..1010] ofBoolean;9 E,s,t,c,i,n,m,ans,j:longint;Ten One procedureAddedge (x,y,z,w:longint); A begin -Ot[e]:=y; NE[E]:=G[X]; Cap[e]:=z; Cost[e,1]:=w; Cost[e,2]:=-w; g[x]:=E; Inc (E); -Ot[e]:=x; Ne[e]:=g[y]; cap[e]:=0; Cost[e,1]:=-w; Cost[e,2]:=w; g[y]:=E; Inc (E); the End; - - functionmin (a,b:longint): Longint; - begin + ifA<b ThenExit (a)Elseexit (b); - End; + A functionSPFA (C:longint): boolean; at var - X,y,l,r,p:longint; - begin - forI:=s toT Do - beginDIS[I]:=MAXN; Inq[i]:=false;End; -l:=0; r:=1; dis[s]:=0; Inq[s]:=true; h[1]:=s; pre[s]:=-1; in whileL<r Do - begin to Inc (L); +x:=H[l]; -p:=G[x]; the whilep>-1 Do * begin $y:=Ot[p];Panax Notoginseng if(cap[p]>0) and(dis[y]>dis[x]+Cost[p,c]) - Then begin theDIS[Y]:=DIS[X]+COST[P,C]; pre[y]:=p; + ifinq[y]=false A Then beginInq[y]:=true; Inc (R); H[r]:=y;End; the End; +p:=Ne[p]; - End; $inq[x]:=false; $ End; -Exit (dis[t]<>MAXN); - End; the - functionFind_path (c:longint): Longint;Wuyi var the X,p,tmp,path:longint; - begin Wux:=t; PATH:=MAXN; tmp:=0; - whileX>s Do About begin $p:=Pre[x]; -path:=min (path,cap[p]); -X:=ot[p XOR1]; - End; Ax:=T; + whileX>s Do the begin -p:=Pre[x]; $Inc (tmp,path*cost[p,c]); theInc (Cap[p XOR1],path); the Dec (cap[p],path); theX:=ot[p XOR1]; the End; - exit (TMP); in End; the the begin Aboute:=0; theFillchar (G,sizeof (g),255); the readln (n,m); thes:=0; t:=n+m+1; ans:=0; + fori:=1 toN Do - beginRead (c); Addedge (S,i,c,0);End; the fori:=1 toM DoBayi beginRead (c); Addedge (N+i,t,c,0);End; the fori:=1 toN Do the forj:=1 toM Do - begin - read (c); theAddedge (i,n+j,maxn,c); the End; theg1:=g; Ot1:=ot; Cap1:=cap; Ne1:=ne; cost1:=Cost ; the whileSPFA (1) Do -Inc (Ans,find_path (1)); the writeln (ans); theans:=0; theG:=G1; OT:=OT1; CAP:=CAP1; Ne:=ne1; cost:=Cost1;94 whileSPFA (2) Do theInc (Ans,find_path (2)); theWriteln (-ans); the End.
Network Flow 24 Questions-no.17 transportation problem