#include <iostream>#include<stdio.h>#include<vector>#include<string.h>#include<queue>using namespacestd;Const intmaxn=1024x768;Const intinf=0x3f3f3f3f;structedge{int from, To,cap,flow,cost; Edge (intUintVintCintFintW): from(U), to (v), Cap (c), Flow (f), Cost (W) {}};structmcmf{intn,m; Vector<Edge>edges; Vector<int>G[MAXN]; intINQ[MAXN];//is in the queue intD[MAXN];//Bellman-ford intP[MAXN];//on an arc intA[MAXN];//can improve the amount voidInitintN) { This->n=N; for(intI=0; i<n;i++) g[i].clear (); Edges.clear (); } voidAddedge (int from,intTo,intCapintCost ) {Edges.push_back (Edge ( from, To,cap,0, cost)); Edges.push_back (Edge to, from,0,0,-Cost )); M=edges.size (); g[ from].push_back (M-2); G[to].push_back (M-1); } BOOLBellmanford (intSintTint&flow,Long Long&Cost ) { for(intI=0; i<n;i++) d[i]=INF; memset (INQ,0,sizeof(INQ)); D[s]=0; inq[s]=1;p [s]=0; a[s]=INF; Queue<int>p; Q.push (s); while(!Q.empty ()) { intu=Q.front (); Q.pop (); Inq[u]=0; for(intI=0; I<g[u].size (); i++) {Edge&e=Edges[g[u][i]]; if(e.cap>e.flow&&d[e.to]>d[u]+e.cost) {D[e.to]=d[u]+E.cost; P[e.to]=G[u][i]; A[e.to]=min (a[u],e.cap-E.flow); if(!inq[e.to]) {Q.push (e.to); inq[e.to]=1;} } } } if(D[t]==inf)return false; Flow+=A[t]; Cost+=(Long Long) d[t]* (Long Long) a[t]; for(intU=t;u!=s;u=edges[p[u]]. from) {Edges[p[u]].flow+=A[t]; Edges[p[u]^1].flow-=A[t]; } return true; } //need to ensure that there is no negative power ring in the initial network intMincostmaxflow (intSintTLong Long&Cost ) { intflow=0; cost=0; while(Bellmanford (s,t,flow,cost)); returnflow; }}mm;intMain () {intN,m,k;//number of meeting points, number of source points, types of items intneed[ -][ -];//Need[i][j] indicates that the first store owner needs the quantity of the article J Commodity inttotalneed[ -];// intstorage[ -][ -];//Storage[i][j] Indicates the number of j commodities stored in the warehouse of the first I inttotalstorage[ -];// intcost[ -][ -];//Cost[i][j] Indicates the unit price at which the item is currently shipped from the J warehouse to the first owner BOOLFlag; intMincost; intMaxflow; Long LongCost ; while(~SCANF ("%d%d%d",&n,&m,&K)) { if(n==0&&m==0&&k==0) Break; memset (Totalneed,0,sizeof(totalneed)); memset (Totalstorage,0,sizeof(Totalstorage)); Flag=true; Mincost=0; for(intI=0; i<n;++i) { for(intj=0; j<k;++j) {scanf ("%d",&Need[i][j]); TOTALNEED[J]+=Need[i][j]; } } for(intI=0; i<m;++i) { for(intj=0; j<k;++j) {scanf ("%d",&Storage[i][j]); TOTALSTORAGE[J]+=Storage[i][j]; } } for(intI=0; i<k;++i) { if(totalneed[i]>Totalstorage[i]) {Flag=false; Break; } } for(intk=0; k<k;++k) { for(intI=0; i<n;++i) { for(intj=0; j<m;++j) {scanf ("%d",&Cost[i][j]); } } if(flag==false)Continue; Build (cost);// from hereMaxflow=MM. Mincostmaxflow (S,t,cost); if(maxflow<Totalneed[k]) flag=false; } if(flag==false) printf ("-1\n"); Elseprintf"%lld\n", Mincost); } return 0;}
H-minimum Cost