poj 2516 Minimum Cost(多源多汇费用流)

Minimum Cost
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 15764 Accepted: 5514

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
题意:有n个顾客,m个仓库,k件物品

先给出每个顾客需要的k件物品的数量,m个仓库k件物品的库存,每件物品每个仓库对每个顾客的运费,问总的运费最少是多少?如果不能全部运满就输出-1

思路:题目按照物品给出一个个n*m的矩阵,很容易想到枚举k件物品,这里也确实可以把所有物品分解开来,因为相互之间是不影响各自的答案的,这里是每件物品的运费而不是距离(同样一段距离运送一种物品跟运送两种物品不一样,即使容量是合法的),所以边枚举边计算每件物品的最少运费,最后加起来即可,如果有一种无法达到满流就是不合法的,标记一下,最后判断下

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 210
#define M 30010
#define INF 0x3f3f3f3f
struct Edge
{
    int from,to,next,cap,cost;///起点,终点,同起点下一条边,残余流量,费用
} edge[M*7];
int cnt,head[N],sum[N];
int vis[N],d[N],pp[N],num[N][N],ma[N][N],a[N][N];
int sumflow;///最大流量总和
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int from,int to,int cap,int cost)
{
    edge[cnt].from=from;
    edge[cnt].to=to;
    edge[cnt].cost=cost;
    edge[cnt].cap=cap;
    edge[cnt].next=head[from];
    head[from]=cnt++;
    edge[cnt].from=to;
    edge[cnt].to=from;
    edge[cnt].cost=-cost;
    edge[cnt].cap=0;
    edge[cnt].next=head[to];
    head[to]=cnt++;///存反向边
}
int spfa(int s,int t,int n)
{
    queue<int>q;
    memset(vis,0,sizeof(vis));
    memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号
    for(int i=0; i<=n; i++)
        d[i]=INF;
    d[s]=0;
    vis[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap>0&&d[v]>d[u]+edge[i].cost)
            {
                d[v]=d[u]+edge[i].cost;
                pp[v]=i;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    if(d[t]==INF) return 0;///找不到一条到终点的路
    return 1;
}
int MCMF(int s,int t,int n)
{
    int mincost=0,minflow,flow=0;///最小费用,路径中最小流量,总流量
    while(spfa(s,t,n))///找当前的最短路
    {
        minflow=INF+1;
        for(int i=pp[t]; i!=-1; i=pp[edge[i].from])
            minflow=min(minflow,edge[i].cap);///从路径中找最小的流量
        flow+=minflow;///总流量加上最小流量
        for(int i=pp[t]; i!=-1; i=pp[edge[i].from])
        {
            edge[i].cap-=minflow;///当前边减去最小流量
            edge[i^1].cap+=minflow;///反向边加上最小流量
        }
        mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次)
    }
    sumflow=flow;
    return mincost;
}
int main()
{
    int n,m,k;
    while(~scanf("%d %d %d",&n,&m,&k)&&(n+m+k))
    {
        memset(sum,0,sizeof(sum));
        for(int i=1; i<=n; i++)
            for(int j=1; j<=k; j++)
            {
                scanf("%d",&num[i][j]);
                sum[j]+=num[i][j];
            }
        for(int i=1; i<=m; i++)
            for(int j=1; j<=k; j++)
                scanf("%d",&ma[i][j]);
        int flag=0,s=0,t=n+m+1,ans=0;
        for(int l=1; l<=k; l++)
        {
            init();
            for(int i=1; i<=n; i++)
                for(int j=1; j<=m; j++)
                    scanf("%d",&a[i][j]);
            if(flag) continue;
            for(int i=1; i<=m; i++)
                addedge(s,i,ma[i][l],0);
            for(int i=1; i<=m; i++)
                for(int j=1; j<=n; j++)
                    addedge(i,m+j,INF,a[j][i]);
            for(int i=m+1; i<=m+n; i++)
                addedge(i,t,num[i-m][l],0);
            sumflow=0;
            ans+=MCMF(s,t,t);
            if(sumflow<sum[l]) flag=1;
        }
        if(flag) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值