Poj 2516 minimum cost (maximum minimum cost Stream)

Source: Internet
Author: User
Minimum Cost
Time limit:4000 Ms   Memory limit:65536 K
Total submissions:10978   Accepted:3692

Description

Dearboy, a goods victualer, now comes to a big problem, and he needs your help. in his sale area there are n shopkeepers (marked from 1 to n) which stocks goods from him. dearboy has 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 shocould arrange which supply place provide how much amount of goods to shopkeepers to cut down the total cost of transport.

It's known that the cost to transport one unit goods for different kinds from different supply places to different shopkeepers may be 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, you shoshould 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 each test case contains three integers n, m, K (0 <n, m, k <50), which are described above. the next n lines give the shopkeepers orders, with each line containing K integers (there integers are belong to [0, 3]), which represents the amount of goods each shopkeeper needs. the next M lines give the supply places 'Storage, with each line containing K integers (there integers are 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), The INTEGER (this integer is belong to (0,100) 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 shocould not be processed.

Output

For each test case, if dearboy can satisfy all the needs of all the shopkeepers, print in one line an integer, which is the 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

Source

Poj monthly -- 2005.07.31, Wang yijie This is a classic topic with the largest minimum cost stream. That is, there are K types of products, and the maximum traffic is charged with a minimum fee of K times. ViewProgramDon't explain.
# Include <stdio. h> # Include <Iostream> # Include <Algorithm># Include < String . H> # Include <Queue> Using   Namespace  STD;  //  **************************************** ********************  //  Maximum Flow with minimum costAlgorithm  //  Spfa to find the shortest path  //  Adjacent matrix form //  Initialization: CAP: capacity, no side is 0  //  Cost: Cost, symmetric form, zero if there is no edge  //  C is the minimum fee  //  F is the largest stream.  //  **************************************** ***************  Const   Int Maxn = 200  ;  Const   Int INF =0x3fffffff  ;  Int Cap [maxn] [maxn]; //  Capacity, with no side being 0  Int  Flow [maxn] [maxn];  //  The consumption matrix is symmetric. If I to J is included, the cost of J to I is the opposite.  Int  Cost [maxn] [maxn];  Int N; //  Vertex count 0 ~ N-1  Int F;//  Maximum stream  Int C; //  Minimum fee  Int Start, end; //  Source and Sink  Bool Vis [maxn]; //  In the queue flag  Int  Que [maxn];  Int  Pre [maxn];  Int Dist [maxn];//  Minimum Cost of S-T Path  Bool  Spfa (){  Int Front = 0 , Rear = 0  ;  For ( Int U = 0 ; U <= N; U ++ ){  If (U = Start) {que [rear ++] =U; Dist [u] = 0  ; Vis [u] = True  ;}  Else  {Dist [u] = INF; vis [u] = False  ;}}  While (Front! = Rear ){  Int U = que [Front ++]; Vis [u] = False  ;  If (Front> = maxn) Front = 0  ;  For ( Int V = 0 ; V <= N; V ++ ){  If (Cap [u] [v]> flow [u] [v] & Dist [v]> Dist [u] + Cost [u] [v]) {Dist [v] = DIST [u] +Cost [u] [v]; Pre [v] = U;  If (! Vis [v]) {vis [v] = True  ; Que [rear ++] = V;  If (Rear> = maxn) rear = 0  ;}}}}  If (Dist [end]> = inf) Return  False  ;  Return   True  ;}  Void  Mincostmaxflow () {memset (flow,  0 , Sizeof  (Flow); c = F = 0  ;  While  (Spfa ()){  Int Min = INF; For ( Int U = end; u! = Start; u = Pre [u]) min = Min (Min, Cap [pre [u] [u]- Flow [pre [u] [u]);  For ( Int U = end; u! = Start; u = Pre [u]) {flow [pre [u] [u] + = Min; flow [u] [pre [u] -= Min;} c + = DIST [end] * Min; f + = Min ;}} //  **************************************** ********************  Int  Order [maxn] [maxn];  Int  Supply [maxn] [maxn];  Int  Tolorder [maxn];  Int  Main (){  //  Freopen ("in.txt", "r", stdin );  //  Freopen ("out.txt", "W", stdout );      Int N, m;  Int  K;  While (Scanf ( "  % D  " , & N, & M, & K )! = EOF ){  If (N = 0 & M = 0 & Amp; k = 0 ) Break  ; Start =0  ; N = N + M + 1  ; End = N; memset (cap,  0 , Sizeof  (Cap); memset (cost,  0 , Sizeof  (Cost ));  For ( Int I = 0 ; I <n; I ++)  For ( Int J = 0 ; J <K; j ++ ) Scanf (  "  % D  " ,& Order [I] [J]);  For ( Int I = 0 ; I <m; I ++ )  For (Int J = 0 ; J <K; j ++ ) Scanf (  "  % D  " ,& Supply [I] [J]);  For ( Int I = 0 ; I <K; I ++ ) {Tolorder [I] = 0  ;  For (Int J = 0 ; J <n; j ++ ) Tolorder [I] + = Order [J] [I];}  For ( Int I = 1 ; I <= m; I ++ )  For ( Int J = m + 1 ; J <= N + m; j ++ ) CAP [I] [J] = INF; Bool Flag = True  ;  Int Ans = 0  ;  For ( Int I = 0 ; I <K; I ++ ){  For ( Int J = m + 1 ; J <= m + n; j ++ ) For ( Int K = 1 ; K <= m; k ++ ) {Scanf (  "  % D  " ,& Cost [k] [J]); cost [J] [k] =- Cost [k] [J];}  If (! Flag) Continue  ;  For (Int J = 1 ; J <= m; j ++ ) CAP [start] [J] = Supply [J- 1  ] [I];  For ( Int J = 1 ; J <= N; j ++ ) CAP [J + M] [end] = order [J- 1  ] [I];  /* For (int K = 0; k <= N; k ++) {for (Int J = 0; j <= N; j ++) printf ("% d ", cap [k] [J]); printf ("\ n ");}  */  Mincostmaxflow ();  //  Printf ("% d \ n", F, C );              If (F <tolorder [I]) Flag = False  ;  Else Ans + = C ;}  If (! Flag) printf ( " -1 \ n  "  );  Else Printf ( "  % D \ n  "  , ANS );}  Return   0  ;} 

 

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.