This test instructions is disgusting to me Baidu. Look at the small leisurely write *_* understand
The cost of transporting individual goods is calculated independently, so they can be handled separately
#include <cstdio>#include<cstring>#include<algorithm>#include<queue>using namespacestd;Const intinf=0x7f7f7f7f;Const intmaxn=100008;Const intmaxm= -;structfuck{intU,v,cap,next,cost;} EDGE[MAXN];inthead[maxm<<4];intTol;intMPN[MAXM][MAXM],MPM[MAXM][MAXM],MPCOST[MAXM][MAXM][MAXM];voidinit () {tol=0; memset (Head,-1,sizeof(head));}voidAddedge (intUintVintWintc) {edge[tol].u=u; EDGE[TOL].V=v; Edge[tol].cap=W; Edge[tol].next=Head[u]; Edge[tol].cost=B; Head[u]=tol++; EDGE[TOL].U=v; EDGE[TOL].V=T; Edge[tol].cap=0; Edge[tol].next=Head[v]; Edge[tol].cost=-C; HEAD[V]=tol++;}intMicost,max_flow,last;intdis[maxm<<4],pre[maxn<<4];BOOLvis[maxm<<4];BOOLSPFA (intSour,intsink) {Queue<int>Q; Q.push (sour); memset (Vis,false,sizeof(VIS)); memset (Dis,inf,sizeof(DIS)); Dis[sour]=0; vis[sour]=true;p re[sour]=0; intU,v,i; while(!Q.empty ()) {u=Q.front (); Q.pop (); Vis[u]=false; for(i=head[u];i!=-1; i=Edge[i].next) {v=edge[i].v; if(edge[i].cap>0&&dis[v]>dis[u]+edge[i].cost) {Dis[v]=dis[u]+Edge[i].cost; PRE[V]=i; if(!Vis[v]) {Q.push (v); VIS[V]=true; } } } } if(Dis[sink]>=inf)return false; return true;}voidMincost_flow (intSour,intsink) {Max_flow=0; micost=0; inti; while(SPFA (Sour,sink)) {intFl=INF; for(i=sink;i!=sour;i=edge[pre[i]].u)if(fl>Edge[pre[i]].cap) FL=Edge[pre[i]].cap; for(i=sink;i!=sour;i=edge[pre[i]].u) {Edge[pre[i]].cap-=FL; Edge[pre[i]^1].cap+=FL; } Max_flow+=FL; Micost+=dis[sink]*FL; }}intMain () {inti,j,k,jj,n,m,u,v,w; while(SCANF ("%d%d%d", &n,&m,&k) = =3) { if(n==0&&m==0&&k==0) Break; BOOLflag=true; intans=0; for(i=1; i<=n;i++) for(j=1; j<=k;j++) scanf ("%d",&Mpn[i][j]); for(i=1; i<=m;i++) for(j=1; j<=k;j++) scanf ("%d",&Mpm[i][j]); for(jj=1; jj<=k;jj++) for(i=1; i<=n;i++) for(j=1; j<=m;j++) scanf ("%d",&Mpcost[jj][i][j]); for(i=1; i<=k;i++) {init (); intsum=0; for(j=1; j<=n;j++)//Lingshoushang if(mpn[j][i]>0) {Addedge (J,n+m+1, Mpn[j][i],0); Sum+=Mpn[j][i]; } for(j=1; j<=m;j++)//Shangjia if(mpm[j][i]>0) Addedge (0, J+n,mpm[j][i],0); intSour=0, sink=n+m+1; for(j=1; j<=n;j++) { if(mpn[j][i]>0) for(jj=1; jj<=m;jj++) if(mpm[jj][i]>0) Addedge (JJ+N,J,MPM[JJ][I],MPCOST[I][J][JJ]); } Last=sour+1; Mincost_flow (Sour,sink); if(max_flow<sum) flag=false; Ans+=Micost; } if(flag) printf ("%d\n", ans); Elseprintf"-1\n"); } return 0;}
POJ 2516 Minimum cost minimum charge flow