您的位置:首页 > 其它

【最小费用最大流】POJ-2516 Minimum Cost

2014-10-25 21:58 435 查看
Minimum Cost

Time Limit: 4000MS Memory Limit: 65536K
   
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.
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

————————————————————満腹の分割線————————————————————
前言:用美团团购和室友吃了一顿27元的自助烤肉,真是爽,,,撑死了。。。
思路:该题图本来是很裸,但是需要有拆图的意识。。。我SB地跑了一发每个仓库建立K个食物点的图,TLE……
读题发现,不同食物之间是完全独立的。食物和人有独立关系,和仓库也有独立关系。所以对于每一种食物,分别建图跑费用流。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 111, M = 6666;
struct Node {
int u, v, w, c;
int next;
Node(){}
Node(int _u, int _v, int _w, int _c, int _next):
u(_u), v(_v), w(_w), c(_c), next(_next){}
}edge[M];
int tot, S, T, head
, dis
, pre
, path
, q
;
int n, m, k, need[55][55], have[55][55];
bool inq
;

void init()
{
tot = 0;
memset(head, -1, sizeof(head));
}

void add(int u, int v, int w, int c)
{
edge[tot] = Node(u, v, w, c, head[u]); head[u] = tot++;
edge[tot] = Node(v, u, 0, -c, head[v]); head[v] = tot++;
}

bool spfa()
{
memset(inq, 0, sizeof(inq));
memset(dis, 0x3f, sizeof(dis));
dis[S] = 0; inq[S] = 1;
int fron = 0, rear = 0;
q[rear++] = S;
while(fron < rear) {
int u = q[fron%N]; fron++;
inq[u] = false;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v;
if(edge[i].w > 0 && dis[v] > dis[u] + edge[i].c) {
dis[v] = dis[u] + edge[i].c;
pre[v] = u; path[v] = i;
if(!inq[v]) {
inq[v] = true;
q[rear%N] = v; rear++;
}
}
}
}
return dis[T] < INF;
}

int EK()
{
int ret = 0;
while(spfa()) {
int mini = INF;
for(int i = T; i != S; i = pre[i]) {
mini = min(mini, edge[path[i]].w);
}
for(int i = T; i != S; i = pre[i]) {
edge[path[i]].w -= mini;
edge[path[i]^1].w += mini;
}
ret += mini * dis[T];
}
return ret;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
//n:店主 m:仓库 k:食品
while(scanf("%d%d%d", &n, &m, &k), n||m||k) {
int x;
S = 0; T = n + m + 1;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) {
scanf("%d", &x);
need[j][i] = x;
}
}
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= k; j++) {
scanf("%d", &x);
have[j][i] = x;
}
}
//对每一种食物建图
int ans = 0;
bool ok = 1;
for(int f = 1; f <= k; f++) {
init();
for(int i = 1; i <= n; i++) {
add(i+m, T, need[f][i], 0);
for(int j = 1; j <= m; j++) {
scanf("%d", &x);
add(j, i+m, INF, x);
}
}
if(ok) {
for(int i = 1; i <= m; i++) {
add(S, i, have[f][i], 0);
}
ans += EK();
for(int i = head[T]; ~i; i = edge[i].next) {
int v = edge[i].v - m;
if(edge[i].w != need[f][v]) {
ok = 0;
break;
}
}
}
}
printf("%d\n", ok ? ans : -1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: