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

Minimum Cost

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 18195   Accepted: 6417

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

POJ Monthly--2005.07.31, Wang Yijie

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
#include <cmath>
#define P pair<int, int>
using namespace std;

const int inf = 0x7fffffff;
const int mn = 110;

struct edge
{
	int to, cap, cost, rev;
};
vector<edge> G[mn];
void addedge (int u, int v, int w, int f)
{
	edge e;
	e.to = v, e.cap = w, e.cost = f, e.rev = (int) G[v].size();
	G[u].push_back (e);
	e.to = u, e.cap = 0, e.cost = -f, e.rev = (int) G[u].size() - 1;
	G[v].push_back ( e );
}

int h[mn];
int dis[mn];
int preve[mn], prevv[mn];  /// 前驱结点及对应边
int min_cost_flow (int s, int t, int f)
{
	int res = 0;
	memset (h, 0, sizeof h);
	while (f > 0) /// dijkstra 更新h
	{
		priority_queue<P, vector<P>, greater<P> > que;
		for ( int i = 0; i <= mn; i++ )
			dis[i] = inf;
		dis[s] = 0;
		que.push (P (0, s));
		while (!que.empty())
		{
			P p = que.top();
			que.pop();
			if (dis[p.second] < p.first)
				continue;
			int v = p.second;
			for (int i = 0; i < ( int ) G[v].size(); i++)
			{
				edge &e = G[v][i];
				if (e.cap > 0 && dis[e.to] > dis[v] + e.cost + h[v] - h[e.to])
				{
					dis[e.to] = dis[v] + e.cost + h[v] - h[e.to];
					prevv[e.to] = v;
					preve[e.to] = i;
					que.push (P (dis[e.to], e.to ));
				}
			}
		}

		/// 无法增广 得到答案
		if (dis[t] == inf)
			return -1;
		for (int i = 1; i <= mn; i++)
			h[i] += dis[i];
		int d = f;
		for (int v = t; v != s; v = prevv[v])
			d = min (d, G[prevv[v]][preve[v]].cap);

		f -= d;
		res += d * h[t];
		for (int v = t; v != s; v = prevv[v])
		{
			edge &e = G[prevv[v]][preve[v]];
			e.cap -= d;
			G[v][e.rev].cap += d;
		}
	}
	return res;
}

int xu[55][55], gong[55][55], pay[55][55][55];
int main()
{
	freopen ( "D:\\in.txt", "r", stdin );

	int n, m, k;
	while (~scanf ("%d %d %d", &n, &m, &k) && (n || m || k))
	{
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= k; j++)
			scanf("%d", &xu[i][j]);
		for (int i = 1; i <= m; i++)
			for (int j = 1; j <= k; j++)
			scanf("%d", &gong[i][j]);
		for (int i = 1; i <= k; i++)
			for (int p = 1; p <= n; p++)
				for (int q = 1; q <= m; q++)
				scanf("%d", &pay[i][p][q]);

		int res = 0;
		int st = 0, ed = 101;
		for (int K = 1; K <= k; K++)  /// 分别求K次最小费用最大流
		{
			for (int i = 0; i <= mn; i++)
				G[i].clear();

			int sum = 0;
			for (int i = 1; i <= n; i++)
			{
				sum += xu[i][K];
				addedge(st, i, xu[i][K], 0);
			}
			for (int i = 1; i <= m; i++)
				addedge(50 + i, ed, gong[i][K], 0);
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= m; j++)
				addedge(i, 50 + j, inf, pay[K][i][j]);

			int Q = min_cost_flow(st, ed, sum);
			if (Q == -1)
			{
				res = -1;
				break;
			}
			else
				res += Q;
		}

		printf("%d\n", res);
	}
	return 0;
}

///每个店拆k个点 TLE
/*
int s = 0, t = 5500;
for (int i = 1; i <= m; i++)
	addedge(s, i, inf, 0);
for (int i = 1; i <= n; i++)
	addedge(5100 + i, t, inf, 0);

int sum = 0;  // 商店需求总数
for (int i = 1; i <= n; i++)
{
	for (int j = 1; j <= k; j++)
	{
		int a;
		scanf("%d", &a);
		sum += a;
		addedge(2600 + 50 * (i - 1) + j, 5100 + i, a, 0);
	}
}

for (int i = 1; i <= m; i++)
{
    for (int j = 1; j <= k; j++)
    {
        int a;
        scanf("%d", &a);
        addedge(i, 50 + 50 * (i - 1) + j, a, 0);
    }
}

for (int i = 1; i <= k; i++)
{
    for (int p = 1; p <= n; p++)
    {
        for (int q = 1; q <= m; q++)
        {
            int a;
            scanf("%d", &a);
            addedge(50 + 50 * (q - 1) + i, 2600 + 50 * (p - 1) + i, inf, a);
        }
    }
}

printf("%d\n", min_cost_flow(s, t, sum));
*/

猜你喜欢

转载自blog.csdn.net/ummmmm/article/details/81319874