题目
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.
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.
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
一开始看到题目没有思路,但是知道这是个最小费用最大流,但是难就难在建图。
题意:有 n 个商店,k种物品和 m 个供货商,让你求进满足商店需求的货物的最小花费?有必要说一下输入数据。首先n ,k ,m 然后是一个n*m的矩阵,n个商店对每种货物的需求,表示第 i 个商店需要第 j 种货物 x个 然后是m * k 的矩阵,m个供货商可以供k种货物的数量,表示第 i 个供货商 提供第 j 中货物 x 个接下来是 k 个 n * m 的矩阵,表示第 i 个货物,由 k 供应商发货给 j 商店的价格x(注意如果供不应求的或输出-1)
分析:很明显能看出来是最小费用最大流,但是如果直接建图的话需要拆点,把每个物品拆成 n 个,分别表示给那个商店,据说会超时,没有试。从给出的 k 个矩阵,可以看出来可以分开来求,每一个物品来求,然后求和那么对于第 k 个物品我们首先s连接每个商店 ,容量为商店需求,费用0然后商店到供货商,容量inf ,费用为进价x然后供货商到 t ,容量为供给量,费用为0
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#include <string>
#define INF 0x3f3f3f3f
#define maxn 220
typedef long long ll;
using namespace std;
struct Edge {
int from, to, cap, flow, cost;
Edge(int from, int to, int cap, int flow, int cost) :from(from), to(to), cap(cap), flow(flow), cost(cost) {};
void input() { scanf("%d%d%d%d%d", &from, &to, &cap, &flow, &cost); }
};
vector<Edge> e;
vector<int> G[maxn];
int vis[maxn], dis[maxn];
int p[maxn], a[maxn];
void addedge(int from, int to, int cap, int cost) {
e.push_back(Edge(from,to,cap,0,cost));
e.push_back(Edge(to, from, 0, 0, -cost));
int len = e.size() - 1;
G[to].push_back(len);
G[from].push_back(len - 1);
}
bool BellmanFord(int s,int t,int& flow,int& cost){
memset(dis, INF,sizeof(dis));
memset(vis, 0,sizeof(vis));
dis[s] = 0;
vis[s] = 1;
p[s] = 0;
a[s] = INF;
queue<int>q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = 0; i < G[u].size(); ++i) {
Edge& g = e[G[u][i]];
if (g.cap > g.flow && dis[g.to] > dis[u] + g.cost) {
dis[g.to] = dis[u] + g.cost;
p[g.to] = G[u][i];
a[g.to] = min(a[u], g.cap - g.flow);
if (!vis[g.to]) {
q.push(g.to);
vis[g.to] = 1;
}
}
}
}
if (dis[t] == INF)
return false;
flow += a[t];
cost += dis[t] * a[t];
int u = t;
while (u != s) {
e[p[u]].flow += a[t];
e[p[u] ^ 1].flow -= a[t];
u = e[p[u]].from;
}
return true;
}
void clear(int x) {
for (int i = 0; i <= x; ++i)
G[i].clear();
e.clear();
}
int Mincost(int s, int t) {
int flow = 0, cost = 0;
while (BellmanFord(s, t, flow, cost));
return cost;
}
int need[maxn][maxn];
int sup[maxn][maxn];
int r[maxn];
int main(){
int n, m, k;
while (scanf("%d%d%d", &n, &m, &k) && (n + m + k)){
int s = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= k; ++j) {
scanf("%d", &need[i][j]);
}
}
memset(r, 0, sizeof(r));
for(int i=1;i<=m;++i)
for (int j = 1; j <= k; ++j) {
scanf("%d", &sup[i][j]);
r[j] += sup[i][j];
}
int ans = 0, ok = 1;
for (int w = 1; w <= k; ++w) {
int s = 0, t = m + n + 1;
int temp = 0;
for (int i = 1; i <= n; ++i) {
addedge(s, i, need[i][w], 0);
temp += need[i][w];
}
if (temp > r[w])ok = 0;
for(int i=1;i<=n;++i)
for (int j = 1; j <= m; ++j) {
int x;
scanf("%d", &x);
addedge(i, n + j, INF, x);
}
for (int i = 1; i <= m; ++i)
addedge(n + i, t, sup[i][w], 0);
if (ok)ans += Mincost(s, t);
clear(t);
}
if (ok == 0)printf("-1\n");
else printf("%d\n", ans);
}
return 0;
}