POJ 2516 Minimum Cost(最小费用最大流)

Minimum Cost
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 18176   Accepted: 6408

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

Source


【思路】

由于一开始没算好复杂度,把供应商和商店的各商品种类区分开来建图超时了。

只需要在每种商品的情况下,对n个商店和m个供应商见图即可,把所有情况的最小费用加起来就是总的了。这种做法建出来的图自然就会小许多,而K仅作为常数所以时间够了。我思考问题真的还是太naive了。


【代码】

//******************************************************************************
// File Name: POJ_2516.cpp
// Author: Shili_Xu
// E-Mail: [email protected]
// Created Time: 2018年07月13日 星期五 14时08分09秒
//******************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;

const int MAXN = 5100, INF = 0x3f3f3f3f;

struct edge {
	int from, to, cap, flow, cost;

	edge() {}
	edge(int u, int v, int c, int f, int w) : from(u), to(v), cap(c), flow(f), cost(w) {}
};

int shopkeepers, providers, kinds, n;
vector<edge> e;
vector<int> g[MAXN];
int a[MAXN], p[MAXN], d[MAXN], out[55], in[55];
int need[55][55], give[55][55];
bool inq[MAXN];
queue<int> q;

void init()
{
	e.clear();
	for (int i = 0; i <= n; i++) g[i].clear();
}

void add_edge(int u, int v, int c, int f, int w)
{
	e.push_back(edge(u, v, c, f, w));
	g[u].push_back(e.size() - 1);
}

bool spfa(int s, int t, int &flow, int &cost)
{
	for (int i = 0; i <= n; i++) d[i] = INF;
	memset(inq, false, sizeof(inq));
	d[s] = 0; q.push(s); inq[s] = true;
	a[s] = INF;
	while (!q.empty()) {
		int u = q.front(); q.pop(); inq[u] = false;
		for (int i = 0; i < g[u].size(); i++) {
			edge &now = e[g[u][i]];
			if (now.cap - now.flow > 0 && d[now.to] > d[u] + now.cost) {
				d[now.to] = d[u] + now.cost;
				a[now.to] = min(a[u], now.cap - now.flow);
				p[now.to] = g[u][i];
				if (!inq[now.to]) {
					q.push(now.to);
					inq[now.to] = false;
				}
			}
		}
	}
	if (d[t] == INF) return false;
	for (int i = t; i != s; i = e[p[i]].from) {
		e[p[i]].flow += a[t];
		e[p[i] ^ 1].flow -= a[t];
	}
	flow +=a[t];
	cost += d[t] * a[t];
	return true;
}

int min_cost_max_flow(int s, int t, int &cost)
{
	int ans = 0;
	cost = 0;
	while (spfa(s, t, ans, cost));
	return ans;
}

int main()
{
	while (scanf("%d %d %d", &shopkeepers, &providers, &kinds) == 3) {
		if (shopkeepers == 0 && providers == 0 && kinds == 0) break;
		n = shopkeepers + providers + 1;
		memset(out, 0, sizeof(out));
		memset(in, 0, sizeof(in));
		for (int i = 1; i <= shopkeepers; i++)
			for (int j = 1; j <= kinds; j++) scanf("%d", &need[i][j]), in[j] += need[i][j];
		for (int i = 1; i <= providers; i++)
			for (int j = 1; j <= kinds; j++) scanf("%d", &give[i][j]), out[j] += give[i][j];
		bool flag = true;
		for (int i = 1; i <= kinds; i++)
			if (out[i] < in[i]) flag = false;
		int ans = 0;
		for (int k = 1; k <= kinds; k++) {
			init();
			for (int i = 1; i <= shopkeepers; i++) {
				add_edge(providers + i, n, need[i][k], 0, 0);
				add_edge(n, providers + i, 0, 0, 0);
			}
			for (int i = 1; i <= providers; i++) {
				add_edge(0, i, give[i][k], 0, 0);
				add_edge(i, 0, 0, 0, 0);
			}
			for (int j = 1; j <= shopkeepers; j++)
				for (int m = 1; m <= providers; m++) {
					int x;
					scanf("%d", &x);
					add_edge(m, providers + j, INF, 0, x);
					add_edge(providers + j, m, 0, 0, -x);
				}
			if (!flag) continue;
			int cost;
			min_cost_max_flow(0, n, cost);
			ans += cost;
		}
		if (!flag) {
			printf("-1\n");
			continue;
		}
		printf("%d\n", ans);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/shili_xu/article/details/81038159