POJ2516:Minimum Cost(最小费用最大流)
Minimum Cost
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 19088 | Accepted: 6740 |
题目链接:http://poj.org/problem?id=2516
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种货物。
每个商店对每种货物都有一定的需求([0,3]),每个供应商也囤积了一定数量的每种货物([0,3])。
后面会输入k个矩阵,每个矩阵都是n*m的。
第K个矩阵里面,第i行第j列表示从j供应商到i商店运送第K种货物的花费。
最后求如若满足所有商店进货需求的最小花费,如果不能满足商店需求,就输出-1。
题解:
这里,每个供应商对于每种货物运送到不同的商店费用都是不一样的。
题目要求输入k个矩阵,我们考虑对于每种货物,都建一次图,源点连着所有的商店,供应点连着所有的汇点,边权都为他们所需要/拥有的货物个数,然后对于从j供应商到i商店运输的花费,都在其中间连一条容量为INF,费用为输入值的边。
最后跑k次最小费用流就行了。
另外注意的就是如果每次跑出来的最大流小于商店的需求,那么就是没解的。
最后,读懂题意很重要,我就是被坑在读题上面的...
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define t 200
#define INF 99999999
using namespace std;
typedef long long ll;
const int N = ;
int n,m,k,tot;
int head[N],need[N][N],supply[N][N],d[N],a[N],vis[N],p[N],pre[N];
struct Edge{
int v,next,c,w;
}e[(N*N)<<];
void adde(int u,int v,int c,int w){
e[tot].v=v;e[tot].next=head[u];e[tot].c=c;e[tot].w=w;head[u]=tot++;
e[tot].v=u;e[tot].next=head[v];e[tot].c=;e[tot].w=-w;head[v]=tot++;
}
int SPFA(int &flow,int &cost){
for(int i=;i<=t;i++) d[i]=a[i]=INF;d[]=;
memset(vis,,sizeof(vis));vis[]=;
memset(p,-,sizeof(p));memset(pre,-,sizeof(pre));
queue <int> q;q.push();
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(e[i].c> && d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
p[v]=u;pre[v]=i;
a[v]=min(a[u],e[i].c);
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
if(d[t]==INF) return ;
flow+=a[t];cost+=d[t]*a[t];
for(int i=t;i!=-;i=p[i]){
int edge=pre[i];
e[edge].c-=a[t];
e[edge^].c+=a[t];
}
return ;
}
int main(){
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
if(!n &&!m &&!k) break;
for(int i=;i<=n;i++)
for(int j=;j<=k;j++)
scanf("%d",&need[i][j]);
for(int i=;i<=m;i++)
for(int j=;j<=k;j++)
scanf("%d",&supply[i][j]);
int ans=;
bool flag=false;
for(int K=;K<=k;K++){
tot=;memset(head,-,sizeof(head));
for(int i=;i<=n;i++){
for(int j=,s;j<=m;j++){
scanf("%d",&s);
adde(i,j+n,INF,s);
}
}
int sum=;
for(int i=;i<=n;i++) adde(,i,need[i][K],),sum+=need[i][K];
for(int i=n+;i<=n+m;i++) adde(i,t,supply[i-n][K],);
if(flag) continue ;
int flow=,cost=;
while(SPFA(flow,cost));
if(sum>flow) flag=true;
ans+=cost;
}
if(flag) puts("-1");
else printf("%d\n",ans);
}
return ;
}
POJ2516:Minimum Cost(最小费用最大流)的更多相关文章
- POJ2516 Minimum Cost —— 最小费用最大流
题目链接:https://vjudge.net/problem/POJ-2516 Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Tota ...
- 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 [最小费用最大流]
题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...
- Poj 2516 Minimum Cost (最小花费最大流)
题目链接: Poj 2516 Minimum Cost 题目描述: 有n个商店,m个仓储,每个商店和仓库都有k种货物.嘛!现在n个商店要开始向m个仓库发出订单了,订单信息为当前商店对每种货物的需求 ...
- POJ2516 Minimum Cost(最小费用最大流)
一开始我把每个店主都拆成k个点,然后建图..然后TLE.. 看题解= =哦,愚钝了,k个商品是独立的,可以分别跑k次最小费用最大流,结果就是k次总和.. #include<cstdio> ...
- POJ2516 Minimum Cost【最小费用最大流】
题意: 有N个客户,M个仓库,和K种货物.已知每个客户需要每种货物的数量,每个仓库存储每种货物的数量,每个仓库运输各种货物去各个客户的单位费用.判断所有的仓库能否满足所有客户的需求,如果可以,求出最少 ...
- 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(k次费用流)
Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19883 Accepted: 7055 Des ...
- POJ - 2516 Minimum Cost(最小费用最大流)
1.K种物品,M个供应商,N个收购商.每种物品从一个供应商运送到一个收购商有一个单位运费.每个收购商都需要K种物品中的若干.求满足所有收购商需求的前提下的最小运费. 2.K种物品拆开来,分别对每种物品 ...
随机推荐
- python 使用生成器 来完成 监听文件输入的例子
def tail(filename):#函数 f = open(filename,encoding='utf-8') while True: line = f.readline() if line.s ...
- vue---day01
1.let和const var 全局作用域和函数作用域 存在变量提升 其实是个bug 可以重复声明 let 块级作用域 不存在变量提升 不能重复声明 const 常量 和let一样还有另外两个 定义的 ...
- centos 7 关闭IPtables
systemctl status iptables.service systemctl stopiptables.service
- python2.7入门---运算符 &案例
已经分享过变量类型的基本概念了,接下来就研究了一下运算符的基础知识.接下来我们就来看一下内容.举个简单的例子 4 +5 = 9 .例子中,4 和 5 被称为操作数,"+" ...
- Mysql双主操作
MySQL双主(主主)架构方案 在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果 ...
- 制作一个简易计算器——基于Android Studio实现
一个计算器Android程序的源码部分分为主干和细节两部分. 一.主干 1. 主干的构成 计算器的布局 事件(即计算器上的按钮.文本框)监听 实现计算 2. 详细解释 假设我们的项目名为Calcula ...
- 成为IT精英,我奋斗7年【转】
这些日子 我一直在写一个实时操作系统内核,已有小成了,等写完我会全部公开,希望能够为国内IT的发展尽自己一份微薄的力量.最近看到很多学生朋友和我当年一样没 有方向 ,所以把我的经历写出来与大家共勉,希 ...
- OJ题归纳
1.求最大公约数 利用辗转相除法求最大公约数 int gcd(int a,int b) { int c,r; if(a<b){c=a;a=b;b=c;} if(b==0) return a; r ...
- webmagic 二次开发爬虫 爬取网站图片
webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代码即可实现一个爬虫. webmagic介绍 编写一个简单的爬虫 webmagic的使用文档:http://w ...
- 【SSH】——两种添加jar包方式的比较
[前言] 在开发过程中,我们对Eclipse或MyEclipse等IDE越来越熟悉了.在使用的过程中,小编了解到两种添加jar包的方式,今天给大家说下这两种方式的差别. 方法一: 将所需要的jar包拷 ...