题面

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

分别表示有N个商店,M个供货商,K中货物

接下来是N行,每行K个整数

对于第i行第j列,表示的是第i个商店对于货物j的需求

再接着,M行,每行K个整数

对于第i行第j列,表示的是第i个供货商对于货物j的存货

接下来有K个N*M的矩形

第X个矩形的第i行第j列表示的是

从供货商j 运送一个单位的 货物X 到商店i的 花费

最后要求的是

在满足 所有商店的供应的 情况下的 最小花费

如果无法满足,则输出-1

题解:

首先弄清楚题目的意思

考虑k种货物都是独立的,因此只需要考虑k遍最小的费用然后求和

对于每一次的最小费用,显然直接求解最小费用流即可,

对于图的构建并不难,

但是要考虑清楚每条边的容量

从汇点向每个供货商连接一条容量为存货数量,费用为0的边

然后从每个供货商向每个商店连接一条容量为INF,费用为花费的边(容量连接成存货数量也行)

从商店向汇点连接一条容量为需求,费用为0的边

求解K次最小费用流累加答案即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define MAX 200
#define MAXL 200000
#define INF 1000000000
struct Line
{
int v,next,w,fb,fy;
}e[MAXL];
int h[MAX],cnt=1,cost,ff;
int tot[MAX],need[MAX][MAX],have[MAX][MAX],Cost[MAX][MAX][MAX];
int pe[MAX],pr[MAX],Ans;
inline void Add(int u,int v,int w,int fy)
{
e[cnt]=(Line){v,h[u],w,cnt+1,fy};
h[u]=cnt++;
e[cnt]=(Line){u,h[v],0,cnt-1,-fy};
h[v]=cnt++;
}
int dis[MAX],S,T,N,M,K; bool vis[MAX];
bool SPFA()
{
memset(dis,63,sizeof(dis));
dis[S]=0;
queue<int> Q;while(!Q.empty())Q.pop();
Q.push(S);
memset(vis,0,sizeof(vis));
while(!Q.empty())
{
int u=Q.front();Q.pop();
vis[u]=false;
for(int i=h[u];i;i=e[i].next)
{
int f=dis[u]+e[i].fy,v=e[i].v;
if(e[i].w&&dis[v]>f)
{
dis[v]=f;
pe[v]=i;
pr[v]=u;
if(!vis[v])
{
vis[v]=true;
Q.push(v);
}
}
}
}
if(dis[T]==dis[T+1])return false;//增广失败
int re=INF;
for(int v=T;v!=S;v=pr[v])
re=min(re,e[pe[v]].w);//计算增广的最大流
for(int v=T;v!=S;v=pr[v])
{
e[pe[v]].w-=re;
e[e[pe[v]].fb].w+=re;
}
ff+=re;
cost+=re*dis[T];
return true;
}
int main()
{
while(233)
{
cin>>N>>M>>K;
if(N==0&&M==0&&K==0)break;
memset(tot,0,sizeof(tot));
for(int i=1;i<=N;++i)
{
for(int j=1;j<=K;++j)
{
cin>>need[i][j];
tot[j]+=need[i][j];
}
}
for(int i=1;i<=M;++i)
{
for(int j=1;j<=K;++j)
{
cin>>have[i][j];
tot[j]-=have[i][j];
}
}
for(int i=1;i<=K;++i)
{
for(int j=1;j<=N;++j)
for(int k=1;k<=M;++k)
cin>>Cost[i][j][k];
}
S=0;T=N+M+1;
bool fl=true;
for(int k=1;k<=K;++k)
if(tot[k]>0)//需求多于提供
{
fl=false;
break;
}
if(!fl)
{
fl=true;
printf("%d\n",-1);
continue;
}
Ans=0;
for(int k=1;k<=K;++k)//K遍费用流
{
cnt=1;
memset(h,0,sizeof(h));
for(int j=1;j<=M;++j)
{
Add(S,j,have[j][k],0);
for(int i=1;i<=N;++i)
Add(j,i+M,have[j][k],Cost[k][i][j]);
}
for(int i=1;i<=N;++i)
Add(i+M,T,need[i][k],0);
cost=ff=0;
while(SPFA());
Ans+=cost;
}
printf("%d\n",Ans);
}
return 0;
}

POJ 2516 Minimum Cost (费用流)的更多相关文章

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

    POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...

  2. Poj 2516 Minimum Cost (最小花费最大流)

    题目链接: Poj  2516  Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...

  3. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

  4. POJ - 2516 Minimum Cost 每次要跑K次费用流

    传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...

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

    1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...

  6. POJ 2516 Minimum Cost(最小费用流)

    Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...

  7. POJ 2516 Minimum Cost 最小费用流

    题目: 给出n*kk的矩阵,格子a[i][k]表示第i个客户需要第k种货物a[i][k]单位. 给出m*kk的矩阵,格子b[j][k]表示第j个供应商可以提供第k种货物b[j][k]单位. 再给出k个 ...

  8. POJ 2516 Minimum Cost 最小费用流 难度:1

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 13511   Accepted: 4628 Des ...

  9. POJ 2516 Minimum Cost [最小费用最大流]

    题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...

随机推荐

  1. 在Swift项目中使用OC,在OC项目中使用Swift

    几天前,我开始新的App的开发了.终于有机会把swift用在实战中了,也学到了之前纯学语法时没有机会获得的知识. 这篇博文中,我就如何使用swift.OC混编做一个介绍. OC中使用Swift 首先, ...

  2. Vijos 1404 遭遇战

    Vijos 1404 遭遇战 背景 你知道吗,SQ Class的人都很喜欢打CS.(不知道CS是什么的人不用参加这次比赛). 描述 今天,他们在打一张叫DUSTII的地图,万恶的恐怖分子要炸掉藏在A区 ...

  3. JavaScript 常用单词整理

    JS单词 push :添加一个数组元素 document :文档 pop :删除最后一个数组元素 console :控制台 shift :删除第一个数组元素 string :字符串 Concat 组合 ...

  4. [翻译]编写高性能 .NET 代码 第一章:性能测试与工具 -- 平均值 vs 百分比

    <<返回目录 平均值 vs 百分比 在考虑要性能测试的目标值时,我们需要考虑用什么统计口径.大多数人都会首选平均值,但在大多数情况下,这个正确的,但你也应该适当的考虑百分数.但你有可用性的 ...

  5. SQL注入漏洞产生的原因是什么?怎么防止?XSS呢?

    SQL注入产生的原因:程序开发过程中不注意规范书写sql语句和对特殊字符进行过滤,导致客户端可以通过全局变量POST和GET提交一些sql语句正常执行. 防止SQL注入: 1.开启配置文件中的magi ...

  6. Google2016 面试题 吹气球 区间dp

    题意:有n个气球,编号为0到n-1,每个气球都有一个分数,存在nums数组中.每次吹气球i可以得到的分数为 nums[left] * nums[i] * nums[right],left和right分 ...

  7. 【推荐】免费,19 款仿 Bootstrap 后台管理主题下载

    声明: 1. 本篇文章提到的仿 Bootstrap 风格的主题,是基于 jQuery 的 ASP.NET MVC 控件库的主题. 2. FineUIMvc(基础版)完全免费,可以用于商业项目. 目录 ...

  8. Python多进程----从入门到放弃

    Python多进程 (所有只写如何起多进程跑数据,多进程数据汇总处理不提的都是耍流氓,恩,就这么任性) (1)进程间数据问题,因为多进程是完全copy出的子进程,具有独立的单元,数据存储就是问题了 ( ...

  9. Effective Java 第三版——35. 使用实例属性替代序数

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  10. Servlet中forward和redirect的区别(转)

    forward方式:request.getRequestDispatcher("/somePage.jsp").forwardrequest, response);     red ...