739. [Network Flow 24 questions] transport issues
★ Import File: tran.in output file: tran.out Simple comparison
Time limit: 1 s memory limit: MB
«Problem Description:
«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:
«Result Output:
At the end of the program run, the calculated minimum shipping cost and the maximum shipping cost are output to the file tran.out.
Example output file for input file sample
Tran.in
2 3
220 280
170 120 210
77 39 105
150 186 122
Tran.out
48500
69140
For all data: 1<=n,m<=100
The following:
It's easier to think about the cost stream.
Build diagram:
1> set up virtual sources s and sinks T.
2> from S to M warehouses with a flow rate of a[i], with a cost of 0 sides.
3> from n stores to T for B[i], with a cost of 0 sides.
4> from M warehouses to n stores, with an infinity, cost of c[i][j] edge.
Then run for the minimum cost, and then run one side of the maximum cost is good.
AC Code:
#include <cstdio>#include<cstring>#include<iostream>#defineM (s) memset (s,0,sizeof s)#defineINF 0X7FFFFFF#defineIn inline#defineR Registerusing namespacestd;Const intn=305; inintRead () {Rintx=0; RBOOLf=1; RCharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=0; ch=GetChar ();} while(ch>='0'&&ch<='9') {x= (x<<3) + (x<<1) +ch-'0'; ch=GetChar ();} returnf?x:-x;}structnode{intV,cap,cost,next;} E[n* -];intN,m,s,t,tot,ans,head[n],prep[n],prev[n],flow[n],dis[n];intA[n],b[n],c[n][n];BOOLVis[n];invoidAddintXintYintCapintCost ) {e[++tot].v=y; E[tot].cap=cap; E[tot].cost=Cost ; E[tot].next=Head[x]; HEAD[X]=tot;} Inchvoidbuild () {S=0, t=m+n+1; for(intI=1; i<=m;i++) Add (S,i,a[i],0), add (I,s,0,0); for(intI=1; i<=n;i++) Add (I+m,t,b[i],0), add (T,i+m,0,0); for(intI=1; i<=m;i++){ for(intj=1; j<=n;j++) {Add (I,j+M,INF,C[I][J]); Add (J+m,i,0,-C[i][j]); }}}invoidCl () {ans=0; M (head); m (Prep); M (prev); m (flow);}Const intqlen=n* -;intq[qlen+5];inBOOLSPFA (intk) { intH=0, t=1; M (VIS); if(k>0) memset (DIS,127,sizeofdis); Elsememset (DIS, -,sizeofdis); Q[t]=s;dis[s]=0; vis[s]=1; Flow[s]=inf;prep[s]=-1; while(h!=t) { if(++h>qlen) h=1; intx=Q[h]; VIS[X]=0; for(intI=head[x];i;i=E[i].next) { intv=e[i].v,cap=e[i].cap,cost=E[i].cost; if(cap>0&& (k>0&&dis[v]>dis[x]+cost) | | (k<0&&dis[v]<dis[x]+Cost)) ) {Dis[v]=dis[x]+Cost ; PREP[V]=x;prev[v]=i; FLOW[V]=min (flow[x],cap); if(!Vis[v]) {Vis[v]=1; if(++t>qlen) t=1; Q[t]=v; } } } } if(k>0)returndis[t]<inf; Else returnDis[t]>0;} InchvoidWork () { for(intI=t;i!=s;i=Prep[i]) {E[prev[i]].cap-=Flow[t]; E[prev[i]^1].cap+=Flow[t]; } ans+=flow[t]*dis[t];}intMain () {Freopen ("tran.in","R", stdin); Freopen ("Tran.out","W", stdout); M=read (); n=read (); for(intI=1; i<=m;i++) a[i]=read (); for(intI=1; i<=n;i++) b[i]=read (); for(intI=1; i<=m;i++){ for(intj=1; j<=n;j++) {C[i][j]=read (); }} tot=1; build (); while(SPFA (1) ) work (); printf ("%d\n", ans); Tot=1; Cl (); build (); while(SPFA (-1) ) work (); printf ("%d\n", ans); return 0;}
739. [Network Flow 24 questions] transport issues