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

题意:有k种物品,m个供应商,n个收购商。每个供应商和收购商都需要一些种类的物品若干。每个供应商与每个收购商之间的对于不同物品的运费是不同的。求满足收购商要求的情况下,最小运费。

思路:对于K种物品进行k次最小费用最大流。建图顺序为s-供应商-商店-t。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

//const int MAXN = 5000000 + 5;
//const int INF = 0x3f3f3f3f;
//最小费用最大流
const int MAXN=200;
const int INF=0x3fffffff;
int cap[MAXN][MAXN];//容量,没有边为0
int flow[MAXN][MAXN];
//耗费矩阵是对称的,有i到j的费用,则j到i的费用为其相反数
int cost[MAXN][MAXN];
int n;//顶点数目1~n
int f;//最大流
int c;//最小费用
int s, t;//源点和汇点

bool vis[MAXN];//在队列标志
int pre[MAXN];
int dist[MAXN];//s-t路径最小耗费
queue<int> q;
void init()
{
    memset(cost, 0, sizeof(cost));
    memset(cap, 0, sizeof(cap));
}
bool SPFA()
{
    while(!q.empty()) q.pop();
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= n; i++) dist[i] = INF;
    q.push(s);
    vis[s] = true;
    dist[s] = 0;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u]=false;
        for(int v = 1; v <= n;v++)
        {
            if(cap[u][v]>flow[u][v] && dist[v]>dist[u]+cost[u][v])
            {
                dist[v] = dist[u]+cost[u][v];
                pre[v]=u;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
    if(dist[t] >= INF) return false;
    return true;
}

void minCostMaxflow()
{
    memset(flow,0,sizeof(flow));
    c=f=0;
    while(SPFA())
    {
        int Min=INF;
        for(int u=t;u!=s;u=pre[u])
           Min=min(Min,cap[pre[u]][u]-flow[pre[u]][u]);
        for(int u=t;u!=s;u=pre[u])
        {
            flow[pre[u]][u]+=Min;
            flow[u][pre[u]]-=Min;
        }
        c+=dist[t]*Min;
        f+=Min;
    }
}
int shop, sup, good;
int goodShopNeed[60][60], supplyStore[60][60], unitCost[60][60][60];
int main()
{
    //freopen("input.txt", "r", stdin);
	while(cin>>shop>>sup>>good && shop)
    {
        init();
        for(int i = 1; i <= shop; i++)
            for(int j = 1; j <= good; j++)
                scanf("%d", &goodShopNeed[i][j]);
        for(int i = 1; i <= sup; i++)
            for(int j = 1; j <= good; j++)
                scanf("%d", &supplyStore[i][j]);
        for(int i = 1; i <= good; i++)
            for(int j = 1; j <= shop; j++)
                for(int k = 1; k <= sup; k++)
                    scanf("%d", &unitCost[i][j][k]);
        int res = 0;
        n = 110, s = 109, t = 110;
        for(int i = 1; i <= good; i++)
        {
            int sumv = 0;
            for(int j = 1; j <= shop; j++) sumv += goodShopNeed[j][i];
            for(int j = 1; j <= sup; j++)
                cap[s][j] = supplyStore[j][i];
            for(int j = 1; j <= shop; j++)
                cap[50+j][t] = goodShopNeed[j][i];
            for(int j = 1; j <= sup; j++)
                for(int k = 1; k <= shop; k++)
                {
                    cap[j][50+k] = supplyStore[j][i];
                    cost[j][50+k] = unitCost[i][k][j];
                    cost[50+k][j] = -cost[j][50+k];
                }
            minCostMaxflow();
            if(f < sumv)
            {
                res = -1;
                break;
            }
            res += c;
        }
        cout << res << endl;
	}
    return 0;
}

















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值