POJ 2516 minimum cost maximum flow

Source: Internet
Author: User

Language:DefaultMinimum Cost
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 14334 Accepted: 4908

Description

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In He sale area there is N shopkeepers (marked from 1 to N) which stocks goods from him. Dearboy have M supply places (marked from 1 to M), each provides K different kinds of goods (marked from 1 to K). Once Shopkeepers order goods, Dearboy should arrange which supply place provide what much amount of goods to shopkeepers to Cut down the total cost of transport.

It's known that's the cost of transport one unit goods for different kinds from different supply places to different Shopkee Pers may different. Given each supply places ' storage of k kinds of goods, N shopkeepers ' order of K kinds of goods and the cost to transport Goods for different kinds from different supply places to different shopkeepers, and you should tell how to arrange the goods Supply to minimize the total cost of transport.

Input

The input consists of multiple test cases. The first line of all test case contains three integers n, m, K (0 < N, m, K <), which is described above. The next N lines give the shopkeepers ' orders, with each line containing K integers (there integers is belong to [0, 3]), which represents the amount of goods each shopkeeper needs. The next M lines give the supply places ' storage, with all line containing K integers (there integers is also belong to [0, 3]), which represents the amount of goods stored in that supply place.

Then come K integer matrices (each with the size N * M) and the integer (this integer was belong to (0)) at the i-th row, j-th column in the k-th matrix represents the cost to transport one unit of k-th goods from the j-th supply place to the I-th shopkeeper.

The input is terminated with three "0" s. This test case is should not being processed.

Output

For each test case, if Dearboy can satisfy all the needs of the the shopkeepers, print in one line a integer, which is th e minimum cost; Otherwise just output "-1".

Sample Input

1 3 3   1 1 10 1 11 2 21 0 11 2 31 1 12 1 11 1 132200 0 0

Sample Output

4-1

A problem with the maximum flow of minimum cost, difficulties in the building, not to build a map difficult, but the pit, not a one-time build diagram, this will be timed out not to say there will be negative ring, but I have not understood why there will be negative ring, the great God convenient can give a comment, feel this problem I write code above the comments are good, suitable for small white to see.

#include <iostream>#include<cstring>#include<cstdio>#include<climits>#include<queue>#defineMAXN 110using namespacestd;structedge{intS,t,f,c,next;} edge[12000];//Constructing adjacency Tablesintn,m,k;intHEAD[MAXN];//Constructing adjacency TablesintPRE[MAXN];//Record PathBOOLISQ[MAXN];//record whether a point is in the queueintDIST[MAXN];//record the minimum cost to that pointintstore[ -][ -];//Demand for storesintmarket[ -][ -];//supply of the marketintgood[ -][ -][ -];//costs from the market to the storeintsum[ -];//The sum of all the stores ' demand for the first itemintall[ -];//The sum of all markets ' supply of the first J-piece goodsintEnt//number that represents the EdgeintS,t;//represents a super-source point super-Meeting pointintCost//represents the minimum costvoidAddintSintTintFintc) {EDGE[ENT].S=s; EDGE[ENT].T=T; EDGE[ENT].F=F; EDGE[ENT].C=C; Edge[ent].next=Head[s]; Head[s]=ent++; Edge[ent].s=T; EDGE[ENT].T=s; EDGE[ENT].F=0; EDGE[ENT].C=-C; Edge[ent].next=Head[t]; Head[t]=ent++;}intMIN (intAintb) {    returnA<b?a:b;}BOOLSPFA () {memset (PRE,-1,sizeof(pre));//initialization path is-1     for(inti=s;i<=t;i++) {Isq[i]=false;//at the beginning of each point is not in the queueDist[i]=int_max;//the minimum cost to initialize to each point is Int_max} queue<int>Q;    Q.push (s); Isq[s]=true;//The source point S has been put into the queue.dist[s]=0;//The distance from the source point to the source point is 0     while(!q.empty ())//The optimization process ends when the queue is empty, exiting the loop    {        inttemp1=Q.front ();        Q.pop (); ISQ[TEMP1]=false;//the point has exited the queue         for(inti=head[temp1];i!=-1; i=edge[i].next)//from this point, find all the edges that start with that point from the adjacency table and find the points that can be optimized.        {            intTemp2=edge[i].t; if(edge[i].f&&dist[temp2]>dist[temp1]+edge[i].c) {DIST[TEMP2]=dist[temp1]+edge[i].c; PRE[TEMP2]=i; if(!ISQ[TEMP2])//If the point is not in the queue, the point is placed in the queue{Q.push (TEMP2); ISQ[TEMP2]=true; }            }        }    }    returnpre[t]!=-1;//If the pre[t]==-1 words indicate that no path from S to T is found, that all paths have been found, end loop}intMCMF () {intcost=0;  while(SPFA ()) {intMinn=int_max;//to find the variables that are added to the maximum flows that can flow through this process         for(inti=pre[t];i!=-1; i=Pre[i]) {Minn=MIN (MINN,EDGE[I].F); I=Edge[i].s; }         for(inti=pre[t];i!=-1; i=Pre[i]) {EDGE[I].F-=minn;//The flow of the edges passing through the path is reduced by the maximum flow that the process can passedge[i^1].f+=minn;//The reverse edge is added to the maximum flow, which guaranteesI=Edge[i].s; } Cost+=MINN*DIST[T];//add up the cost of each finding    }    returnCost//return minimum fee}intMain () {intTemp,ok;  while(cin>>n>>m>>k,n+m+K) { cost=0; BOOLok=true;//record market can not meet the needs of supermarketsmemset (SUM,0,sizeof(SUM)); memset (All,0,sizeof(All));  for(intI=1; i<=n;i++)        {             for(intj=1; j<=k;j++) {scanf ("%d", &store[i][j]);//The demand for article J items in Shop Isum[j]+=Store[i][j]; }        }         for(intI=1; i<=m;i++)        {             for(intj=1; j<=k;j++) {scanf ("%d", &market[i][j]);//supply of item J for article I suppliersall[j]+=Market[i][j]; }        }         for(intI=1; i<=k;i++)        {             for(intj=1; j<=n;j++)            {                 for(intk=1; k<=m;k++) {scanf ("%d", &good[i][j][k]);//k Suppliers Supply the unit price of item I to J store                }            }        }         for(intI=1; i<=k;i++)        {            if(sum[i]>All[i]) {OK=false;  Break; }        }        if(ok==false) {printf ("-1\n");//supply cannot meet demand, output-1        }        Else        {             for(intI=1; i<=k;i++)//minimum cost maximum flow algorithm for article I items{ent=0;//Initializing edgesmemset (head,-1,sizeof(head));//initializing adjacency tables                 for(intj=1; j<=m;j++) {Add (0, J,market[j][i],0);//from the source point to the supplier's side                }                 for(intj=1; j<=m;j++)                {                     for(intk=1; k<=n;k++) {Add (J,k+M,INT_MAX,GOOD[I][K][J]);//from the supplier to the shop side                    }                }                 for(intj=1; j<=n;j++) {Add (J+m,m+n+1, Store[j][i],0);//from the store to the edge of the meeting point} s=0; T=m+n+1; Cost+=MCMF (); } printf ("%d\n", cost); }    }}

POJ 2516 minimum cost maximum flow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.