前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图论--网络流--费用流--POJ 2156 Minimum Cost

图论--网络流--费用流--POJ 2156 Minimum Cost

作者头像
风骨散人Chiam
发布2020-10-28 11:54:01
5230
发布2020-10-28 11:54:01
举报
文章被收录于专栏:CSDN旧文

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

代码语言:javascript
复制
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

这个直接把每件物品单出来考虑完事,单独建图跑就行,然后就很简单了。就变成了普通的费用流问题,那么建图套模板即可!

代码语言:javascript
复制
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#define INF 1e9
using namespace std;
const int maxn=200+10;
 
struct Edge
{
    int from,to,cap,flow,cost;
    Edge(){}
    Edge(int f,int t,int c,int fl,int co):from(f),to(t),cap(c),flow(fl),cost(co){}
};
 
struct MCMF
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool inq[maxn];
    int d[maxn];
    int p[maxn];
    int a[maxn];
 
    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
        for(int i=0;i<n;++i) G[i].clear();
    }
 
    void AddEdge(int from,int to,int cap,int cost)
    {
        edges.push_back(Edge(from,to,cap,0,cost));
        edges.push_back(Edge(to,from,0,0,-cost));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }
 
    bool BellmanFord(int &flow, int &cost)
    {
        for(int i=0;i<n;++i) d[i]=INF;
        memset(inq,0,sizeof(inq));
        d[s]=0, a[s]=INF, inq[s]=true, p[s]=0;
        queue<int> Q;
        Q.push(s);
        while(!Q.empty())
        {
            int u=Q.front(); Q.pop();
            inq[u]=false;
            for(int i=0;i<G[u].size();++i)
            {
                Edge &e=edges[G[u][i]];
                if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
                {
                    d[e.to]= d[u]+e.cost;
                    p[e.to]=G[u][i];
                    a[e.to]= min(a[u],e.cap-e.flow);
                    if(!inq[e.to]){ Q.push(e.to); inq[e.to]=true; }
                }
            }
        }
        if(d[t]==INF) return false;
        flow +=a[t];
        cost +=a[t]*d[t];
        int u=t;
        while(u!=s)
        {
            edges[p[u]].flow += a[t];
            edges[p[u]^1].flow -=a[t];
            u = edges[p[u]].from;
        }
        return true;
    }
    int Min_cost()
    {
        int flow=0,cost=0;
        while(BellmanFord(flow,cost));
        return cost;
    }
}MM;
 
int n,m,k;
int need[50+5][50+5];       //need[i][j]表i顾客对j商品的需求量
int have[50+5][50+5];       //have[i][j]表i仓库对j商品的提供量
int cost[50+5][50+5][50+5]; //cost[x][i][j] 表j仓库到i顾客对x商品的单位运费
 
int main()
{
    while(scanf("%d%d%d",&n,&m,&k)==3 && n)
    {
        int goods[maxn];//货物需求量,用来判断货物是否足够
        int enough=true;//初始货物充足
        memset(goods,0,sizeof(goods));
 
        for(int i=1;i<=n;++i)
        for(int j=1;j<=k;++j)
        {
            scanf("%d",&need[i][j]);
            goods[j]+= need[i][j];
        }
 
        for(int i=1;i<=m;++i)
        for(int j=1;j<=k;++j)
        {
            scanf("%d",&have[i][j]);
            goods[j] -=have[i][j];
        }
 
        for(int h=1;h<=k;++h)
        for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            scanf("%d",&cost[h][i][j]);
 
        for(int i=1;i<=k;++i)if(goods[i]>0)//货物不足,不用计算了
        {
            enough=false;
            break;
        }
        if(!enough)//初始货物不足
        {
            printf("-1\n");
            continue;
        }
 
        int min_cost=0;
        for(int g=1;g<=k;++g)
        {
            int src=0, dst=n+m+1;
            MM.init(n+m+2,src,dst);
            for(int i=1;i<=m;++i) MM.AddEdge(src,i,have[i][g],0);
            for(int i=1;i<=n;++i) MM.AddEdge(m+i,dst,need[i][g],0);
            for(int i=1;i<=m;++i)
            for(int j=1;j<=n;++j)
            {
                MM.AddEdge(i,j+m,INF,cost[g][j][i]);
            }
            min_cost += MM.Min_cost();
        }
        printf("%d\n",min_cost);
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/11/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档