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 should 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 should tell how to arrange the goods supply to minimize the total cost of transport.
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 should not be processed.
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".
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
4 -1
POJ:https://vjudge.net/problem/POJ-2516
网络流,最小费用流
有n家商店,m家供应商,k中货物,现在给出每一家供应商的存货和每家商店的需求,以及供应商的每一种货物到商店的花费,现在求使得所有供应商得到满足的最小花费方案
这道题目的输入和描述有些繁琐,要弄明白,前n行是n家商家,再有m行是供应商的供应数,再有k个n*m的矩阵,第i个矩阵的a行b列表示第i种货物从供应商b到商家a的话费。 首先我们可以看出一点,每一种货物的方案是互相独立的,这点输入也告诉我们了,我们可以把每一种货物分开来求,最后求和即可。 我们先来看不合法的情况,只要统计一下货物的需求量与供应量,如果需求量>供应量,则直接说明不合法。但要注意,因为有多组数据,所以要把输入读完。 再来考虑如何建图,其实这题的建图比较好想,从源点连流量为对应商店需求、费用为0的边,再对每一对商店与供货商之间连流量为无穷大、费用为对应费用的边,最后将供应商与汇点相连,跑最小费用最大流即可。 注意反边的建立,对于所有的反向边,费用为正向边的相反数,流量为0
转载于:https://www.cnblogs.com/SYCstudio/p/7259433.html
相关资源:JAVA上百实例源码以及开源项目