POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流)
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
Http
POJ:https://vjudge.net/problem/POJ-2516
Source
网络流,最小费用流
题目大意
有n家商店,m家供应商,k中货物,现在给出每一家供应商的存货和每家商店的需求,以及供应商的每一种货物到商店的花费,现在求使得所有供应商得到满足的最小花费方案
解决思路
这道题目的输入和描述有些繁琐,要弄明白,前n行是n家商家,再有m行是供应商的供应数,再有k个n*m的矩阵,第i个矩阵的a行b列表示第i种货物从供应商b到商家a的话费。
首先我们可以看出一点,每一种货物的方案是互相独立的,这点输入也告诉我们了,我们可以把每一种货物分开来求,最后求和即可。
我们先来看不合法的情况,只要统计一下货物的需求量与供应量,如果需求量>供应量,则直接说明不合法。但要注意,因为有多组数据,所以要把输入读完。
再来考虑如何建图,其实这题的建图比较好想,从源点连流量为对应商店需求、费用为0的边,再对每一对商店与供货商之间连流量为无穷大、费用为对应费用的边,最后将供应商与汇点相连,跑最小费用最大流即可。
注意反边的建立,对于所有的反向边,费用为正向边的相反数,流量为0
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxN=300;
const int inf=2147483647;
int n,m,K;
int Path[maxN];//跑最大流时记录下某节点的前驱
int Flow[maxN];//记录到每一个点时的流量
int Dist[maxN];//spfa以话费跑最小值的到每一个点的距离
int ques[maxN][maxN];//保存每一家商店的需求
int prov[maxN][maxN];//保存每一家供应商的存货
int G[maxN][maxN];//流量图
int C[maxN][maxN];//费用图
bool inqueue[maxN];//是否在队列中,spfa用
bool spfa();
int main()
{
while (cin>>n>>m>>K)//多组数据
{
if ((n==0)&&(m==0)&&(K==0))
break;
for (int i=1;i<=n;i++)//输入
for (int j=1;j<=K;j++)
scanf("%d",&ques[i][j]);
for (int i=1;i<=m;i++)
for (int j=1;j<=K;j++)
scanf("%d",&prov[i][j]);
int Ans=0;
bool had_ans=1;//标记是否有解
for (int i=1;i<=K;i++)//判断是否有解,即供应量是否大于需求量
{
int sum1=0;
for (int j=1;j<=n;j++)
sum1+=ques[j][i];
int sum2=0;
for (int j=1;j<=m;j++)
sum2+=prov[j][i];
if (sum2<sum1)
{
had_ans=0;
break;
}
}
for (int ki=1;ki<=K;ki++)//注意,即使我们前面判断出了无解,也要把数据读完,因为有多组数据
{
memset(G,0,sizeof(G));//每次都要清空
memset(C,0,sizeof(C));
for (int i=1;i<=n;i++)//读入费用
for (int j=1;j<=m;j++)
{
scanf("%d",&C[i][j+n]);//建正向边
C[j+n][i]=-C[i][j+n];//建反向边
G[i][j+n]=inf;//连接相应的商店和供应商,这里[1,n]是商店,[n+1,n+m]是供应商
}
if (had_ans==0)//如果是无解情况,直接进入下一层循环把输入读完即可
continue;
for (int i=1;i<=n;i++)//连源点到商店,这里源点是0
G[0][i]=ques[i][ki];
for (int i=1;i<=m;i++)//连供应商到汇点,汇点是n+m+1
G[i+n][n+m+1]=prov[i][ki];
int cost=0;//记录当前第K中商品的总花费
while (spfa())//spfa求出花费最小的增广路
{
int now=n+m+1;
int last=Path[now];
cost+=Flow[n+m+1]*Dist[n+m+1];//先累计一下花费
while (now!=0)//从汇点出发,依次修改路径上的残量网络
{
G[last][now]-=Flow[n+m+1];
G[now][last]+=Flow[n+m+1];
now=last;
last=Path[now];
}
}
Ans+=cost;//最后总答案累加每一个K种货物的总和
}
if (had_ans)//若有解则输出解,否则输出-1
cout<<Ans<<endl;
else
cout<<-1<<endl;
}
return 0;
}
bool spfa()//spfa求花费最小的增广路
{
memset(Dist,127,sizeof(Dist));//初始化
memset(Path,-1,sizeof(Path));
memset(Flow,0,sizeof(Flow));
memset(inqueue,0,sizeof(inqueue));
queue<int> Q;
while (!Q.empty())
Q.pop();
Q.push(0);//把源点放入队列
Dist[0]=0;
Flow[0]=inf;
do
{
int u=Q.front();
inqueue[u]=0;
Q.pop();
for (int i=0;i<=n+m+1;i++)
if ((G[u][i]>0)&&(Dist[i]>Dist[u]+C[u][i]))//这里要求有流量且i原来的花费大于u的花费+路径上的花费
{
Dist[i]=Dist[u]+C[u][i];//用u的数据修改i的
Path[i]=u;
Flow[i]=min(Flow[u],G[u][i]);
if (inqueue[i]==0)
{
Q.push(i);
inqueue[i]=1;
}
}
}
while (!Q.empty());
if (Path[n+m+1]==-1)//如果汇点没有经过过,则说明增广完毕,不存在增广路了
return 0;
return 1;
}
POJ 2516 Minimum Cost (网络流,最小费用流)的更多相关文章
- POJ 2516 Minimum Cost(最小费用流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- Poj 2516 Minimum Cost (最小花费最大流)
题目链接: Poj 2516 Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...
- POJ 2516 Minimum Cost (最小费用最大流)
POJ 2516 Minimum Cost 链接:http://poj.org/problem?id=2516 题意:有M个仓库.N个商人.K种物品.先输入N,M.K.然后输入N行K个数,每一行代表一 ...
- POJ 2516 Minimum Cost 最小费用流 难度:1
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 13511 Accepted: 4628 Des ...
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- POJ - 2516 Minimum Cost 每次要跑K次费用流
传送门:poj.org/problem?id=2516 题意: 有m个仓库,n个买家,k个商品,每个仓库运送不同商品到不同买家的路费是不同的.问为了满足不同买家的订单的最小的花费. 思路: 设立一个源 ...
- 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个 ...
- POJ 2516 Minimum Cost(拆点+KM完备匹配)
题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...
- POJ 2516 Minimum Cost [最小费用最大流]
题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...
随机推荐
- springboot整合redis——redisTemplate的使用
一.概述 相关redis的概述,参见Nosql章节 redisTemplate的介绍,参考:http://blog.csdn.net/ruby_one/article/details/79141940 ...
- Node.js 下载路径/微软产品下载路径
https://nodejs.org/en/ https://www.microsoft.com/en-us/download //微软官方下载地址,可以下载VS2015 SQL 等 微软产品
- [Zlib]_[初级]_[使用zlib库压缩和解压STL string]
场景 1.一般在使用文本json传输数据, 数据量特别大时,传输的过程就特别耗时, 因为带宽或者socket的缓存是有限制的, 数据量越大, 传输时间就越长. 网站一般使用gzip来压缩成二进制. 说 ...
- JVM源码---教你傻瓜式编译openjdk7(JAVA虚拟机爱好者必看)
LZ经过一个星期断断续续的研究,终于成功的搞定了JDK的成功编译与调试.尽管网络上的教程也有不少,包括源码中也有自带的编译步骤说明,但真正自己动手的话,还是会遇到不少意料之外的错误. 为了方便各位猿友 ...
- [转]申瓯 JSY2000-06 程控电话交换机呼叫转移设置
说明:若申瓯程控电话交换机分机有事不在位置上或遇忙分机正忙时为使某些重要来话不丢失,可设置将呼入本机的电话转移至其他分机及公网固定电话或手机.电话交换机使用了本功能不管分机用户在什么地方都能接听到办公 ...
- 个人新站 【EXP技术分享博客】 落成~ 全新的技术资源~ 欢迎莅临~
对的,我筹备了大半年的个站技术网站正式开张了~ EXP技术分享博客: http://exp-blog.com CSDN上面大部分文章已经迁移过去了,以后也会更多地在新站点更新~ 为了庆祝新站开张,近期 ...
- 基于Shader实现的UGUI描边解决方案
基于Shader实现的UGUI描边解决方案 前言 大扎好,我系狗猥.当大家都以为我鸽了的时候,我又出现了,这也是一种鸽.创业两年失败后归来,今天想给大家分享一个我最近研究出来的好康的,比游戏还刺激,还 ...
- Istio如何使用相同的端口访问网格外服务
1.1.背景 写这篇文章的目的是为了说明以下问题:如何使用TCP协议相同的端口访问网格外多个服务? 这是最近直播的时候有一个同学提出的,当时我没有完全明白,“访问多集群” 的意思.后来仔细思考了一下, ...
- kali linux 安装Nessus
Nessus 介绍: Nessus 是目前全世界最多人使用的系统漏洞扫描与分析软件.总共有超过75,000个机构使用Nessus 作为扫描该机构电脑系统的软件. 下载Nessus,我的是64为,我选择 ...
- No.1101_第十次团队会议
今天项目进展很多,大家都在现在的成果而开心,信心高涨,后面的任务的完成也基本都能指日可待.之前团队出现了各种问题,沟通出现了很多障碍,导致各方面受阻.现在大家再面对面坦诚相对,交流了一下自己的想法,结 ...