POJ2516--Minimum Cost

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
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1102
#define edge maxn*40
const int inf = 0x3f3f3f3f;
int first[maxn];
int vv[edge],ww[edge],nxt[edge],cst[edge];
int e;
int pre[maxn], pos[maxn];
int dis[maxn], que[maxn*10];
bool vis[maxn];
inline int min(int a,int b)
{
	return a>b?b:a;
}
void addEdge(int u, int v, int w, int cost)
{
	vv[e] = v;
	ww[e] = w;
    cst[e] = cost;
	nxt[e] = first[u];
	first[u] = e++;
	vv[e] = u;
	ww[e] = 0;
    cst[e] = -cost;
	nxt[e] = first[v];
	first[v] = e++;
}

bool spfa(int s, int t)
{
	int i;
    memset(pre, -1, sizeof(pre));
	memset(vis, 0, sizeof(vis));
    int head, tail; 
	head = tail = 0;
    for(i = 0; i < maxn; i++)
		dis[i] = inf;
    que[tail++] = s;源点入队
	pre[s] = s;///源点的前缀是源点本身
	dis[s] = 0;
	vis[s] = 1;
    while(head != tail)
    {
        int u = que[head++];
		vis[u] = 0;//出队了??
		for(i = first[u]; i != -1; i = nxt[i])
		{
			int v = vv[i];
			if(ww[i] > 0 && dis[u] + cst[i] < dis[v])
			{
				dis[v] = dis[u] + cst[i];
				pre[v] = u;
				pos[v] = i;这个我真心不知道用来干嘛的。。。
				if(!vis[v])原来我写的spfa一直不是最优化的
				{
					vis[v] = 1;
					que[tail++] = v;
				}
			}
		}
    }
    return pre[t] != -1;
}

int MinCostFlow(int s, int t, int flow)s到t的最小费用最大流。
{
	int i;
    int cost = 0;
    int nflow = 0;
    while(spfa(s, t))
    {
        int f = inf;
        for(i = t; i != s; i = pre[i])
            if (ww[pos[i]] < f) f = ww[pos[i]];
		f = min(flow - nflow,f);
        nflow += f; cost += dis[t] * f;
        for(i = t; i != s; i = pre[i])
        {
            ww[pos[i]] -= f;
            ww[pos[i] ^ 1] += f;
        }
		if(nflow == flow)break;
    }
	if(nflow == flow)
    return cost; // flow是最大流值
	return -1;
}
struct DZ
{
	int ned[58];
}dz[58];
struct SR
{
	int had[58];
}sr[58];
int main()
{
	//freopen("in.txt","r",stdin);
	int n,m,k;
	while(scanf("%d%d%d",&n,&m,&k)==3 &&(n||m||k))
	{
		//接下来就是N行,每行代表这个店主需要对应品种的数量,n个店主
		//每个店主都要满足要求,否则就不行。
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=k;j++)
			{
				scanf("%d",&dz[i].ned[j]);
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=k;j++)
			{
				scanf("%d",&sr[i].had[j]);
			}
		}
		//然后是K个矩阵,矩阵是用来存边的
		//我觉得应该一种一种物品来。
		bool flag = true;//用来判断能否实现目的
		int Ans = 0;
		for(int i=1;i<=k;i++)///枚举物品种
		{
			memset(first,-1,sizeof(first));
			e = 0;
			for(int j=1;j<=n;j++)
			{
				for(int o=1;o<=m;o++)
				{
					int cost;
					scanf("%d",&cost);
					addEdge(o,j+m,inf,cost);//各个要买东西的店主已经连好边了,接下来要连超级源点
				}
			}
			for(int j=1;j<=m;j++)
			{
				if(sr[j].had[i])
				{
					addEdge(0,j,sr[j].had[i],0);
				}
			}///超级源点连好了
			接下来要连超级汇点
			int sum = 0;
			for(int j=1;j<=n;j++)
			{
				if(dz[j].ned[i])
				{
					sum += dz[j].ned[i];
					addEdge(j+m,m+n+1,dz[j].ned[i],0);
				}
			}
			int ans = MinCostFlow(0,m+n+1,sum);
			if(ans == -1) flag = false;
			else Ans += ans;
		}
		if(flag) printf("%d\n",Ans);
		else printf("-1\n");
	}
	return 0;
}
					


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值