POJ 2516 Minimum Cost (min. max flow)

Source: Internet
Author: User
Tags cos

POJ 2516 Minimum Cost

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 1
0 1 1
1 2 2
1 0 1
1 2 3
1 1 1
2 1 1

1 1 1
3
2
20

0 0 0

Sample Output

4
-1

Main topic: Enter data.

This figure is from: ζёсяêτ-Small advantage You know this information, to find suppliers can meet the needs of the store, can not output "1", the output of the supplier to the merchant to the minimum cost of delivery. Problem-solving ideas: A Open is to read this topic, feel no way, this map is too complex. Later only thought in fact not necessarily only with a picture, the problem can be split into K-map. First of all to determine whether the supplier can meet the needs of the merchant's goods, can not be output-1, if so, continue to build the map. Each map is set up with a super source point connected to all the suppliers, and the capacity is the inventory of the supplier I Commodity (1<= I<= k) , the cost is 0, each supplier is connected to all stores, the capacity of the INF, the cost of the supplier to the corresponding store to send the cost of goods I; Finally, set up a super meeting point, so that all stores to the meeting point, capacity for the store's demand, the cost is 0; Then ask for the K-th minimum fee, the K-th minimum fee to add up, is the final answer.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;Const intN = -;Const intM =50005;Const intof = -;Const intFIN = -;Const intINF =0x3f3f3f3f;intShop[n][n], Sup[n][n], good[n][n][n], rec[n];typedef Long LongllintN, M, K, S, t, Flag;intPre[n], inq[n]; ll A[n], d[n];structedge{intFrom, to;    ll cap, flow; llCos;}; vector<Edge>Edges vector<int>G[M];voidInit () { for(inti =0; i < M;    i++) g[i].clear (); Edges.clear ();}voidAddedge (intFromintTo, ll cap, ll flow, LLCos{Edges.push_back (Edge) {from, to, Cap,0,Cos}); Edges.push_back (Edge) {To, from,0,0, -Cos});intm = Edges.size (); G[from].push_back (M-2); G[to].push_back (M-1);}voidInput () {memset(REC,0,sizeof(rec)); for(inti =0; I < n; i++) { for(intj =0; J < K; J + +) {scanf("%d", &shop[i][j]);        REC[J] + = shop[i][j]; }    } for(inti =0; I < m; i++) { for(intj =0; J < K; J + +) {scanf("%d", &sup[i][j]);        REC[J]-= sup[i][j]; }       } for(inti =0; I < K; i++) {if(Rec[i] >0) flag =0; } for(intL =0; L < K; l++) { for(inti =0; I < n; i++) { for(intj =0; J < M; J + +) {scanf("%d", &good[i][j][l]); }           }       }}intBF (intSintT, ll& Flow, ll& cost) { Queue<int>Q;memset(INQ,0,sizeof(INQ));memsetA0,sizeof(a));memset(Pre,0,sizeof(pre)); for(inti =0; i < N;    i++) D[i] = INF; D[s] =0;    A[s] = INF; Inq[s] =1;intFlag =1; Pre[s] =0; 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.Cos) {d[e.to] = D[u] + E.Cos;                A[e.to] = min (A[u], e.cap-e.flow); Pre[e.to] = G[u][i];if(!inq[e.to]) {Inq[e.to] =1;                Q.push (e.to); }}} flag =0; }if(D[t] = = INF)return 0;    Flow + = A[t]; Cost + = (LL) d[t] * (LL) a[t]; for(intu = t; U! = S;        U = edges[pre[u]].from) {Edges[pre[u]].flow + = a[t]; edges[pre[u]^1].flow-= a[t]; }return 1;}intMCMF (intSintT, ll& cost) {ll flow =0; Cost =0; while(BF (S, t, flow, cost));returnFlow;}voidSolve () {//ask for K-Times minimum feell ans =0; for(intK =0; K < K; k++) {init (); for(inti =1; I <= m; i++) {Addedge (S, I, Sup[i-1][k],0,0); } for(inti =1; I <= N; i++) { for(intj =1; J <= M; J + +) {Addedge (J, i + OF, INF,0, Good[i-1][j-1][k]); }           } for(inti =1; I <= N; i++) {Addedge (i + of, T, Shop[i-1][k],0,0);        } ll cost;        MCMF (S, t, cost);    ans + = cost; }printf("%lld\n", ans);}intMain () { while(scanf(" %d%d%d", &n, &m, &k) = =3) {if(!n &&!m &&!k) Break; Flag =1; s =0, t = FIN; Input ();if(!flag) {printf(" -1\n");Continue;    } solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2516 Minimum Cost (min. max 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.