POJ 2516 — Minimum Cost 费用流

原题:http://poj.org/problem?id=2516

题意:

有n家商店,m个供应商,k个商品;

n*k的矩阵,第i行表示第i家商店对这k个商品的需求量;

m*k的矩阵,第i行表示第i家供应商的存货;

接下来有k个n*m的矩阵,

第i个矩阵表示第i个商品在运输过程中的花费;

问最小花费,若不能满足各商店的需求则输出-1;


思路:

计算k个费用流,累加;

建图 —— 

源点s到供应商,流量为存货,花费为0;

供应商到客户,流量为存货,花费为供应商到客户的费用;

客户到汇点,流量为客户的需求量,花费为0;



#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<algorithm>

using namespace std;
#define ll int
#define inf 0x3f3f3f3f  
#define N 605*605*2
#define M 605*605*4

int n, m, k;
int a[55][55], b[55][55], cost[55][55];
int aa[55], bb[55];
struct Edge 
{
    ll to, cap, cost, nex;
    Edge(){}
    Edge(ll to,ll cap,ll cost,ll next):to(to),cap(cap),cost(cost),nex(next){}
} edge[M<<1];

ll head[N], edgenum;
ll D[N], A[N], P[N];
bool inq[N];
void add(ll from,ll to,ll cap,ll cost) 
{
    edge[edgenum] = Edge(to,cap,cost,head[from]);
    head[from] = edgenum++;
    edge[edgenum] = Edge(from,0,-cost,head[to]);
    head[to] = edgenum++;
}

bool spfa(ll s, ll t, ll &flow, ll &cost) 
{
    for(ll i = 0; i <= t; i++) 
	D[i] = inf;
    memset(inq, 0, sizeof inq);
    queue<ll>q;
    q.push(s);
    D[s] = 0; A[s] = inf;
    while(!q.empty()) 
	{
        ll u = q.front(); q.pop();
        inq[u] = 0;
        for(ll i = head[u]; ~i; i = edge[i].nex)
        {
            Edge &e = edge[i];
            if(e.cap && D[e.to] > D[u] + e.cost)
            {
                D[e.to] = D[u] + e.cost;
                P[e.to] = i;
                A[e.to] = min(A[u], e.cap);
                if(!inq[e.to])
                {inq[e.to]=1; q.push(e.to);}
            }
        }
    }
    if(D[t] == inf) 
	return false;
    cost += D[t] * A[t];
    flow += A[t];
    ll u = t;
    while(u != s) 
	{
        edge[ P[u] ].cap -= A[t];
        edge[P[u]^1].cap += A[t];
        u = edge[P[u]^1].to;
    }
    return true;
}

ll Mincost(ll s,ll t)
{
    ll flow = 0, cost = 0;
    while(spfa(s, t, flow, cost));
    return cost;
}

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

int main()
{
	while(scanf("%d%d%d", &n, &m, &k)!=EOF)
	{
		if(n == 0 && m == 0 && k == 0)
		break;
		int S = 0, T = m+n+1;
		int flag = 0;
		memset(aa, 0, sizeof(a));
		memset(bb, 0, sizeof(b));
		for(int i = 1;i<=n;i++)
		{
			for(int j = 1;j<=k;j++)
			{
				scanf("%d", &a[i][j]);
				aa[j]+=a[i][j];
			}
		}
		for(int i = 1;i<=m;i++)
		{
			for(int j = 1;j<=k;j++)
			{
				scanf("%d", &b[i][j]);
				bb[j]+=b[i][j];
			}
		}
		for(int i = 1;i<=k;i++)
		{
			if(bb[i]<aa[i])
			{
				flag = 1;
				break;
			}
		}
		int sum = 0;
		for(int t = 1;t<=k;t++)
		{
			init();
			for(int i = 1;i<=n;i++)
				for(int j = 1;j<=m;j++)
					scanf("%d", &cost[j][i]);
			if(flag)	continue;
			for(int i = 1;i<=m;i++)
				add(S, i, b[i][t], 0);
			for(int i = 1;i<=n;i++)
				add(i+m, T, a[i][t], 0);
			for(int i = 1;i<=m;i++)
				for(int j = 1;j<=n;j++)
					add(i, j+m, b[i][t], cost[i][j]);
			sum+=Mincost(S, T);
		}
		if(flag)
		printf("-1\n");
		else
		printf("%d\n", sum);
	}
	return 0;
}


    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值