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

Minimum Cost

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 18305   Accepted: 6465

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

 

     就一道模板题,不过我有一个点就是在计算之前回去算算各个货物的总提供量对于总需要量是否足够,如果不哦够的话直接不用再之后中计算了,肯定-1。另外就是我用的是每个货物建一次图,在不同的图之间注意那个需要将head和cnt恢复成开始计算前的状态;

#include
#include
#include
#include
using namespace std;
const int maxn = 100005;
const int maxm = 1000500;
const int inf = 0x3f3f3f3f;
struct fuck {
	int v, ne, cap, flow, cost, u;
}ed[maxm];
int head[maxn], cnt;
int pre[maxn], dis[maxn];
bool vis[maxn];
int shp, sup, kin;
int n, m;
void init() {
	cnt = 0;
	memset(head, -1, sizeof(head));
}
void add(int u, int v, int cap, int cost) {
	ed[cnt].v = v; ed[cnt].cap = cap; ed[cnt].flow = 0; ed[cnt].u = u;
	ed[cnt].cost = cost; ed[cnt].ne = head[u]; head[u] = cnt++;
	ed[cnt].v = u; ed[cnt].cap = 0; ed[cnt].flow = 0; ed[cnt].u = v;
	ed[cnt].cost = -cost; ed[cnt].ne = head[v]; head[v] = cnt++;
}
bool spfa(int st, int en) {
	queueq;
	for (int s = 0; s <= n; s++) {//这里视情况而定
		dis[s] = inf; vis[s] = 0; pre[s] = -1;
	}
	dis[st] = 0;
	vis[st] = 1;
	q.push(st);
	while (q.size()) {
		int u = q.front(); q.pop();
		vis[u] = 0;
		for (int s = head[u]; ~s; s = ed[s].ne) {
			int v = ed[s].v;
			if (ed[s].cap > ed[s].flow&&dis[v] > dis[u] + ed[s].cost) {
				dis[v] = dis[u] + ed[s].cost;
				pre[v] = s;
				if (!vis[v]) {
					vis[v] = 1; q.push(v);
				}
			}
		}
	}
	if (pre[en] == -1)return 0;
	return 1;
}
int MinMax(int st, int en, int &cost) {
	int flow = 0;
	cost = 0;
	while (spfa(st, en)) {
		int min = inf;
		for (int s = pre[en]; ~s; s = pre[ed[s ^ 1].v]) {
			if (min > ed[s].cap - ed[s].flow)
				min = ed[s].cap - ed[s].flow;
		}
		for (int s = pre[en]; ~s; s = pre[ed[s ^ 1].v]) {
			ed[s].flow += min;
			ed[s ^ 1].flow -= min;
			cost += ed[s].cost*min;
		}
		flow += min;
	}
	return flow;
}
int shop[105][105];
int supply[105][105];
int mon[105][105];
bool check()
{
	for (int t = 1; t <= kin; t++){
		int sum = 0;
		for (int s = 1; s <= sup; s++)
			sum += supply[s][t];
		for (int s = 1; s <= shp; s++)
			sum -= shop[s][t];
		if (sum < 0)return 0;
	}
	return 1;
}
int main() {
	while (~scanf("%d%d%d", &shp, &sup, &kin) && shp + sup + kin) {
		init();
		for (int s = 1; s <= shp; s++)
			for (int w = 1; w <= kin; w++)
				scanf("%d", &shop[s][w]);
		for (int s = 1; s <= sup; s++)
			for (int w = 1; w <= kin; w++)
				scanf("%d", &supply[s][w]);
		int tmp = cnt, money, end = shp + sup + 1,ans = 0;
		int copy[maxn], cal = check();
		for (int s = 0; s <= end; s++)
			copy[s] = head[s];
		for (int t = 1; t <= kin; t++) {
			for (int s = 1; s <= shp; s++)
				for (int w = 1; w <= sup; w++)
					scanf("%d", &money), add(s, w + shp, inf, money);
			for (int s = 1; s <= shp; s++)
				add(0, s, shop[s][t], 0);
			for (int s = 1; s <= sup; s++)
				add(s + shp, end, supply[s][t], 0);
			if (!cal)continue;
			n = end; int tans;
			MinMax(0, n, tans); ans += tans;
			cnt = tmp;
			for (int s = 0; s <= end; s++)
				head[s] = copy[s];
		}
		if (cal)cout << ans << endl;
		else cout << -1 << endl;
	}
}

 

你可能感兴趣的:(图论,网络流)