Minimum Cost
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 13052 | Accepted: 4442 |
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 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.
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.
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 should not be processed.
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.
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 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
Source
POJ Monthly--2005.07.31, Wang Yijie
题目大意:n个顾客,m间商店,k件物品,每个顾客对每件商品都有一定数量的需求,每间商店每个物品也都有供应上限,且不同的商店对不同的顾客供应的不同的商品的价格不一样(真绕T_T)。问满足所有顾客需求的最小花费,没有就输出-1。
题目分析:对每一件物品拆开来做最小费用流即可。如果满流则有解,否则无解。PS:一开始不拆开来直接构图TLE的停不下来Q_Q。。。
代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define REP(i, n) for(int i = 1; i <= n; ++i)
using namespace std;
const int maxE = 3000000;
const int maxN = 300;
const int oo = 0x3f3f3f3f;
struct Edge{
int v, c, w, n;
};
Edge edge[maxE];
int adj[maxN], l;
int d[maxN], cur[maxN], a[maxN];
int inq[maxN], Q[maxE], head, tail;
int n, m, k;
int cost, flow, s, t;
int nk[maxN][maxN], mk[maxN][maxN];
void addedge(int u, int v, int c, int w){
edge[l].v = v; edge[l].c = c; edge[l].w = w; edge[l].n = adj[u]; adj[u] = l++;
edge[l].v = u; edge[l].c = 0; edge[l].w = -w; edge[l].n = adj[v]; adj[v] = l++;
}
int SPFA(){
memset(d, oo, sizeof d);
memset(inq, 0, sizeof inq);
head = tail = 0;
d[s] = 0;
a[s] = oo;
cur[s] = -1;
Q[tail++] = s;
while(head != tail){
int u = Q[head++];
inq[u] = 0;
for(int i = adj[u]; ~i; i = edge[i].n){
int v = edge[i].v;
if(!edge[i].c || d[v] <= d[u] + edge[i].w) continue;
d[v] = d[u] + edge[i].w;
cur[v] = i;
a[v] = min(edge[i].c, a[u]);
if(inq[v]) continue;
inq[v] = 1;
Q[tail++] = v;
}
}
if(d[t] == oo) return 0;
flow += a[t];
cost += a[t] * d[t];
for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){
edge[i].c -= a[t];
edge[i ^ 1].c += a[t];
}
return 1;
}
int MCMF(){
while(SPFA());
return flow;
}
void work(){
int w, sum = 0, _sum = 0;
REP(i, n) REP(j, k) scanf("%d", &nk[i][j]);
REP(i, m) REP(j, k) scanf("%d", &mk[i][j]);
REP(i, n) REP(j, k) sum += nk[i][j];
REP(i, m) REP(j, k) _sum += mk[i][j];
if(sum > _sum){
REP(k1, k) REP(i, n) REP(j, m) scanf("%*d");
printf("-1\n");
return;
}
s = 0; t = m + n + 1;
flow = cost = 0;
REP(k1, k) {
memset(adj, -1, sizeof adj);
l = 0;
REP(i, n) REP(j, m){
scanf("%d", &w);
addedge(i, j + n, oo, w);
}
REP(i, n) addedge(s, i, nk[i][k1], 0);
REP(j, m) addedge(j + n, t, mk[j][k1], 0);
MCMF();
}
printf("%d\n", flow == sum ? cost : -1);
}
int main(){
while(~scanf("%d%d%d", &n, &m, &k) && (n || m || k)) work();
return 0;
}