poj2516Minimum Cost

Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
13110572105      Log Out
Mail:2(1)
Login Log      Archive
Language:
Minimum Cost
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 13538 Accepted: 4640

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

Source

[Submit]   [Go Back]   [Status]    [Discuss]

题目大意:

有N个店铺,M个供货商,K种商品。给出每个供货商的仓库里每种商品的数量、每种商品给每个店铺供货的费用,每个店铺需要的各种商品的数量,求最小费用。 


输入一开始是N,M,K。

然后 N行   ,每行 K列 ,第I行第J个数代表 第I个店铺 需要第J种物品多少件。

然后 M行  ,每行 K列 , 第I行第J个数代表 第I个供货商 有第J种物品多少件。

然后是K个矩阵  ,每个N行M列,第I个矩阵的第J行第D列代表着第D个供货商给第J个店铺发第I种货物一件需要的花费。

题目膜拜大神的代码:

代码整理如下:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;

const int M=105,inf=1<<30;
int need[M][M],have[M][M],cost[M][M][M],n,m,k;

int c[M][M];//针对某种物品的最大容量
int f[M][M];//流量
int w[M][M];//费用
int dis[M];//最短路算法中的距离数组,记录原点到其他点的距离
int pre[M];  //最短路中的记录数组,记录最短路径
bool vis[M]; //最短路算法中的标记数组  标记是否被访问过
int min(int a,int b)
{
    if(a>b)
    {
        a=b;
    }
    return a;
}
bool spfa() //最少的费用,最短路算法
{
    int i,j;
    for(i=0;i<=n+m+1;i++)  //初始化
    {
        dis[i]=inf;
        pre[i]=-1;
        vis[i]=false;
    }
    dis[0]=0;
    vis[0]=true;
    queue <int > q;
    q.push(0);  //初始化完成
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        vis[t]=false;
        for(i=1;i<=n+m+1;i++)
        {
            if(c[t][i]>f[t][i]&&dis[i]>dis[t]+w[t][i])  //如果流量没有到最大且超级原点经过点t到i的费用比直接到i小
            {
                dis[i]=dis[t]+w[t][i]; //更新到i点的最小花费
                pre[i]=t; //更新最短路径中点i的前驱为t
                if(!vis[i]) //如果点i没在队列中
                {
                    q.push(i);   //将点i放入队列
                    vis[i]=true; //标记已在队列中
                }
            }
        }
    }
    if(pre[n+m+1]==-1)//如果超级汇点没有在对短路中 (因为没有前驱)
    {
        return false;//返回寻找最短路失败
    }
    return true;  //返回最短路寻找成功
}
void getMaxflow()  //寻找增广路
{
    while(spfa())//如果最最小费用增广路寻找成功
    {
        int maxflow=inf;//初始化为最大值
        int p=n+m+1;
        while(pre[p]!=-1) //遍历最小费用增广路
        {
            maxflow=min(maxflow,c[pre[p]][p]-f[pre[p]][p]);//寻找关键流量,及最短路上的最小流量
            p=pre[p];
        }
        p=n+m+1; //再次初始化;
        while(pre[p]!=-1)  //再次遍历最小费用增广路
        {
            f[pre[p]][p]+=maxflow;
            f[p][pre[p]]=-f[pre[p]][p];  //调整流量
            p=pre[p];
        }
    }
}
int main()
{
    int i,j,d,ans;
    while(scanf("%d%d%d",&n,&m,&k),n||m||k)
    {
        ans=0;
        bool flat=false;
        for(i=1;i<=n;i++) //输入部分
        {
            for(j=1;j<=k;j++)
            {
                scanf("%d",&need[i][j]);
            }
        }
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=k;j++)
            {
                scanf("%d",&have[i][j]);
            }
        }
        for(i=1;i<=k;i++)
        {
            for(j=1;j<=n;j++)
            {
                for(d=1;d<=m;d++)
                {
                    scanf("%d",&cost[i][d][j]);
                }
            }
        } //输入部分完成
        for(i=1;i<=k;i++)//对于每一种货物来说
        {
            memset(c,0,sizeof(c));
            memset(f,0,sizeof(f));
            memset(w,0,sizeof(w));
            for(j=1;j<=m;j++)
            {
                c[0][j]=have[j][i];//超级远点到每个仓库的流量应该是仓库的存货量
            }
            for(j=1;j<=n;j++)
            {
                c[m+j][m+n+1]=need[j][i];//每个商店到超级汇点的容量应该是这个商店的需求量
            }
            for(j=1;j<=m;j++)
            {
                for(d=1;d<=n;d++)
                {
                    c[j][d+m]=have[j][i];//每个仓库到商店的容量应该是仓库的存货量
                }
            }
            for(j=1;j<=m;j++)
            {
                for(d=1;d<=n;d++)
                {
                    w[j][d+m]=cost[i][j][d];  //供货商到商店的花费
                    w[d+m][j]=-cost[i][j][d]; //花费的负值,用于回流时
                }
            }
            getMaxflow();//对第i种商品寻找最小费用流
            for(j=1;j<=n;j++) //对于每一个商店来说
            {
                if(c[j+m][n+m+1]!=f[j+m][n+m+1]) //如果供货量不等于需求量
                {
                    flat=true;
                    break;
                }
            }
            if(flat)  //出现供货不足    跳出
            {
                break;
            }
            for(j=1;j<=m;j++)
            {
                for(d=1;d<=n;d++)
                {
                    ans+=f[j][d+m]*w[j][d+m];//计算总费用
                }
            }
        }
        if(flat)//出现供货不足
        {
            printf("-1\n");
        }
        else
        {
            printf("%d\n",ans);
        }
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值