Poj2516 -- Minimum Cost (cost flow, which is created separately)

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

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

There are too many types of items provided, and different suppliers have different costs for supplying different goods to different stores. In this way, there are too many sides in the same graph, and direct graph creation will time out. Therefore, create a map for each type of goods, find the minimum cost of each type of goods, and get the final cost

Thanks http://blog.csdn.net/scf0920/article/details/38707085

# Include <cstdio> # include <cstring> # include <queue> # include <algorithm> using namespace STD; # define maxn 60 # define INF 0x3f3f3fstruct node {int V, W, s; int next;} p [100000]; int NK [maxn] [maxn], MK [maxn] [maxn], CNT; // NK stores the requirements of different stores for different goods. mk, different suppliers for different goods supply int head [maxn <2], vis [maxn <2], dis [maxn <2], pre [maxn <2]; queue <int> q; void add (int u, int V, int W, int S) {P [CNT]. V = V; P [CNT]. W = W; P [CNT]. S = s; P [CNT]. next = head [u]; head [u] = CNT ++; P [CNT]. V = u; P [CNT]. W = 0; P [CNT]. S =-S; P [CNT]. next = head [v]; head [v] = CNT ++;} int spfa (int s, int t) {int U, V, I; memset (DIS, INF, sizeof (DIS); vis [s] = 1; DIS [s] = 0; Pre [s] = pre [T] =-1; while (! Q. Empty () Q. Pop (); q. Push (s); While (! Q. Empty () {u = Q. Front (); q. Pop (); vis [u] = 0; for (I = head [u]; I! =-1; I = P [I]. next) {v = P [I]. v; If (P [I]. W & dis [v]> dis [u] + P [I]. s) {dis [v] = dis [u] + P [I]. s; Pre [v] = I; If (! Vis [v]) {vis [v] = 1; q. push (v) ;}}}if (pre [T] =-1) return 0; return 1;} int F (int s, int T, int K) {int ans_s = 0, ans_w = 0, min1, I; // minimum cost of ans_s storage, and ans_w stores the total traffic of memset (VIS, 0, sizeof (VIS )); memset (PRE,-1, sizeof (pre); While (spfa (S, T) {min1 = inf; for (I = pre [T]; I! =-1; I = pre [p [I ^ 1]. v]) if (P [I]. W <min1) min1 = P [I]. w; for (I = pre [T]; I! =-1; I = pre [p [I ^ 1]. v]) {P [I]. w-= min1; P [I ^ 1]. W + = min1; ans_s + = min1 * P [I]. s;} ans_w + = min1;} If (ans_w = NK [0] [k]) return ans_s; Return-1;} int main () {int I, j, n, m, KK, K, X, ANS, and flag; // The input is very complex, especially when different suppliers Supply different goods to different stores at different prices. While (scanf ("% d", & N, & M, & K) & N + M + k! = 0) {/* create a map for each type of goods, and create a map for the ki type of goods: from the source point to each supplier (capacity is the amount of goods supplied, the cost is 0), from the supplier to the store (the capacity is INF, the cost is the price of the supply of KI goods), there is a store to the settlement point to build a map (the capacity is the amount of goods required by the store, the cost is 0) */memset (NK, 0, sizeof (NK); for (I = 1; I <= N; I ++) for (j = 1; j <= K; j ++) {scanf ("% d", & NK [I] [J]); NK [0] [J] + = NK [I] [J]; // NK [0] [] Total number of goods needing to be purchased under storage} for (I = 1; I <= m; I ++) for (j = 1; j <= K; j ++) scanf ("% d", & MK [I] [J]); ans = Flag = 0; // flag determines whether the previous goods can be supplied enough. If not, you do not need to go down Computing. For (KK = 1; KK <= K; KK ++) {CNT = 0; memset (Head,-1, sizeof (head); for (I = 1; I <= m; I ++) add (0, I, MK [I] [Kk], 0); for (I = 1; I <= N; I ++) add (m + I, m + n + 1, NK [I] [Kk], 0); for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) {scanf ("% d", & X); add (J, M + I, INF, x);} If (flag! =-1) {flag = f (0, N + m + 1, KK); If (flag! =-1) ans + = flag;} If (flag =-1) 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.