POJ 2516 Minimum Cost 最小费用流 难度:1
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 13511 | Accepted: 4628 |
Description
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
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
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
这一题的边有两个性质,
1 容量:供给-种类/需求-种类
2 价格: 供给-种类-需求
怎么想都想不到怎么保留这两种性质建边
看到小you的博客,可以分种类建图,恍然大悟
于是对每个种类分成两种边
1 源点->供给 需求->最终汇点 用于控制流量,价格为0
2 供给->需求 用于控制价格,流量为inf,
值得一提的是供给->需求是单向边,返回的边价格应该是负数,在这里卡了一次,还有每次流量应该是需求量
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int maxn=;
const int maxm=;
const int maxk=;
const int maxnum=;
const int inf =0x7fffffff; int f[maxnum][maxnum];//s 151 t 152
int cons[maxk][maxnum];
int cost[maxk][maxnum][maxnum];
int e[maxnum][maxnum];
int len[maxnum]; const int sups=,supt=;
int sum,n,m,k; int s1[maxk],s2[maxk];//s1 0-n-1 s2 n-n+m
bool input(){
memset(cons,,sizeof(cons));
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2)); sum=;
if(scanf("%d%d%d",&n,&m,&k)!=)return false;
if(n==&&m==&&k==)return false;
for(int i=;i<n;i++){
int c;
for(int j=;j<k;j++){
scanf("%d",&c);
cons[j][i]=c;
s1[j]+=c;
}
}
for(int i=n;i<n+m;i++){
int c;
for(int j=;j<k;j++){
scanf("%d",&c);
cons[j][i]=c;
s2[j]+=c;
}
} for(int i=;i<k;i++){
for(int j=;j<n;j++){
for(int ii=n;ii<n+m;ii++){
int c;
scanf("%d",&c);
cost[i][j][ii]=c;cost[i][ii][j]=-c;
}
}
} for(int i=;i<n;i++){
for(int j=n;j<n+m;j++){
e[i][j-n]=j;
e[j][i]=i;
e[i][m]=;
e[j][n]=;
e[][i]=i;
e[][j-n]=j;
}
}
fill(len,len+n,m+);
fill(len+n,len+n+m,n+);
len[]=n;
len[]=m;
return true;
}
void build(int kind){
memset(f,,sizeof(f));
for(int i=;i<n;i++)f[][i]=cons[kind][i];
for(int i=n;i<n+m;i++)f[i][]=cons[kind][i];
for(int i=;i<n;i++){
for(int j=n;j<n+m;j++){
f[i][j]=inf;
}
}
}
int d[maxnum],pre[maxnum];
bool vis[maxnum];
queue<int >que;
int mincostmaxflow(int s,int flow,int kind){
build(kind);
int res=;
while(flow>){
fill(d,d+,inf);
memset(vis,,sizeof(vis));
d[s]=;
que.push(s);
while(!que.empty()){
int fr=que.front();que.pop();
vis[fr]=false;
for(int i=;i<len[fr];i++){
int t=e[fr][i];
if(f[fr][t]>&&d[t]>d[fr]+cost[kind][fr][t]){
d[t]=d[fr]+cost[kind][fr][t];
pre[t]=fr;
if(!vis[t]){
que.push(t);
vis[t]=true;
}
}
}
}
if(d[supt]==inf)return -;
int sub=flow;
for(int v=supt;v!=sups;v=pre[v]){
sub=min(sub,f[pre[v]][v]);
}
flow-=sub;
res+=sub*d[supt];
for(int v=supt;v!=sups;v=pre[v]){
f[v][pre[v]]+=sub;
f[pre[v]][v]-=sub;
}
}
return res;
}
int main(){
while(input()){
int ans=;
bool sign=false;
for(int i=;i<k;i++){
if(s1[i]>s2[i]){printf("-1\n");sign=true;break;}
}
if(sign)continue;
for(int i=;i<k;i++){
int res=mincostmaxflow(,s1[i],i);
if(res==-){printf("-1\n");sign=true;break;}
ans+=res;
}
if(sign)continue;
printf("%d\n",ans); }
return ;
}
POJ 2516 Minimum Cost 最小费用流 难度:1的更多相关文章
- 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 (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
- 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(最小费用流)
Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his s ...
- 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(拆点+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 # ...
随机推荐
- SpringCloud请求响应数据转换(一)
异常现象 近期做Spring Cloud项目,工程中对Controller添加ResponseBodyAdvice切面,在切片中将返回的结果封装到ResultMessage(自定义结构),但在Cont ...
- Bootstrap 使用教程 与jQuery的Ajax方法
jQuery.ajax(url,[settings]) 更加详细的内容参考 jQuery API 中文在线手册 概述 通过 HTTP 请求加载远程数据. jQuery 底层 AJAX 实现.简单 ...
- java的基本数据类型默认值
这里就举int类型 默认值在类实例化,也就是对象中才有默认值0,或者是静态变量. 1.先看局部变量使用(不行,报错) 2.静态变量 3.类非静态属性
- YOLO(Darknet官方)训练分类器
目录 1. 分类数据准备 2. Darknet配置 3. Darknet命令使用 4. cifar-10 使用示例 1. 分类数据准备 需要的文件列表: 1. train.list : 训练的图片的绝 ...
- sublime+LatexTools引用参考文献
在用sublime+LatexTools一段时间之后,发现用它来写Latex真的是非常方便,配置好TexLive之后直接CTRL+B就可以直接编译运行了,so cool!但是最近写课程论文的时 ...
- hdu 5144 NPY and shot 物理+三分
NPY and shot Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...
- 指数循环节 求A的B次方模C
phi(c)为欧拉函数, 欧拉定理 : 对于互质的正整数 a 和 n ,有 aφ(n) ≡ 1 mod n . A^x = A^(x % Phi(C) + Phi(C)) (mod C) (x & ...
- 自定义 Maven 的 repositories
有时,应用中需要一些比较新的依赖,而这些依赖并没有正式发布,还是处于milestone或者是snapshot阶段,并不能从中央仓库或者镜像站上下载到.此时,就需要 自定义Maven的repositor ...
- Codeforces 895C - Square Subsets
895C - Square Subsets 思路:状压dp. 每个数最大到70,1到70有19个质数,给这19个质数标号,与状态中的每一位对应. 状压:一个数含有这个质因子奇数个,那么他状态的这一位是 ...
- HDU 2569 彼岸
彼岸 思路:动态规划.因为不能有连续三个不同的颜色,所以只要看最后三个就可以了. 设dp[n]为长度为n到达彼岸的方案数. ①当第n-2个颜色和第n-1个颜色相同时,第n个位置可以取任意一种颜色,dp ...