【POJ2516】Minimum Cost
【POJ2516】Minimum Cost
题意:有N个收购商、M个供应商、K种物品。对于每种物品,每个供应商的供应量和每个收购商的需求量已知、每个供应商与每个收购商之间运送该物品的运费已知。求满足收购商要求的前提下的最小运费。(n,M,K<=50,每种物品供求量<=3,单位运费<=100)
题解:怎么看怎么是最小费用流,但是刚学的KM算法,还是要用一下的~
由于各种物品间没有影响,所以可以将k种物品拆开;由于供求量≤3,所以可以将供应商和收购商都拆开,然后跑KM算法。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n,m,N,M,K,now,ret,temp,ans;
int va[200],vb[200],pri[60][60][60],from[200],la[200],lb[200];
int sa[60][60],sb[60][60],ba[200],bb[200],v[200][200];
int dfs(int x)
{
va[x]=1;
for(int i=1;i<=M;i++)
{
if(vb[i]) continue;
if(la[x]+lb[i]-v[x][i]==0)
{
vb[i]=1;
if(!from[i]||dfs(from[i]))
{
from[i]=x;
return 1;
}
}
else temp=min(temp,la[x]+lb[i]-v[x][i]);
}
return 0;
}
int calc()
{
int i,j,ret=0;
N=M=0;
for(i=1;i<=n;i++)
while(sa[i][now]--)
ba[++N]=i;
for(i=1;i<=m;i++)
while(sb[i][now]--)
bb[++M]=i;
if(N>M) return -1;
memset(la,0x80,sizeof(la));
memset(lb,0,sizeof(lb));
for(i=1;i<=N;i++)
for(j=1;j<=M;j++)
v[i][j]=-pri[ba[i]][bb[j]][now],la[i]=max(la[i],v[i][j]);
memset(from,0,sizeof(from));
for(i=1;i<=N;i++)
{
while(1)
{
memset(va,0,sizeof(va));
memset(vb,0,sizeof(vb));
temp=1<<29;
if(dfs(i)) break;
if(temp==(1<<29)) return -1;
for(j=1;j<=N;j++) if(va[j]) la[j]-=temp;
for(j=1;j<=M;j++) if(vb[j]) lb[j]+=temp;
}
}
for(i=1;i<=N;i++) ret+=la[i];
for(i=1;i<=M;i++) ret+=lb[i];
return -ret;
}
void work()
{
int i,j,k;
for(i=1;i<=n;i++)
for(k=1;k<=K;k++)
scanf("%d",&sa[i][k]);
for(i=1;i<=m;i++)
for(k=1;k<=K;k++)
scanf("%d",&sb[i][k]);
for(k=1;k<=K;k++)
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&pri[i][j][k]);
ans=0;
for(now=1;now<=K;now++)
{
k=calc();
if(k==-1)
{
printf("-1\n");
return ;
}
ans+=k;
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d%d%d",&n,&m,&K)!=EOF)
{
if(n==0) return 0;
work();
}
}
【POJ2516】Minimum Cost的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】【Easy】Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【leetcode】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- 【leetcode】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 【leetcode】Minimum Window Substring (hard) ★
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【leetcode】Minimum Depth of Binary Tree (easy)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【hdu1394】Minimum Inversion Number
Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...
- 【leetcode】Minimum Path Sum(easy)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
随机推荐
- js 获取中文的拼音首字母
es6 + 模块化封装 "use strict"; module.exports = { //参数,中文字符串 //返回值:拼音首字母串数组 makePy (str) { if ( ...
- hibernate中一对一映射
一.hibernate中一对一映射有两种 1 主键方式,一张表的主键是通过另一张表的主键生成的 2 外键方式,一张表添加外键引用另一张表的主键,并添加唯一unique约束 二.下面进行简单例子,用户和 ...
- 蜜果私塾:informix数据库学习合集[不断补充]
一.infomix使用备忘录 目录结构: 1. 启动与停止命令: 2. 修改数据库编码: 3. 查看informix占用的端口: 4. 使用dbacces ...
- Genymotion INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
出现这个错误的原因是Genymotion默认的处理器是x86的,不是arm的.所以安装的时候会出错,仅仅要把x86转换成arm就能够了. 转换方法: 1.下载转换的zip文件:X86 to ARM 2 ...
- vim如何选择ESC的键位绑定
vim除了hijk之外,按键频率最高的大概是Esc,本人已经有点Esc强迫症的兆头了.默认的Esc键远在边陲,按起来也忒麻烦了.怎么解决? 在google大神的帮助下,找到了两个方案: CapsLoc ...
- C++加载dll失败或显示乱码
右键项目-属性-字符集-使用多字节字符集
- atitit.orm的缺点与orm框架市场占有率,选型attilax总结
atitit.orm的缺点与orm框架市场占有率,选型attilax总结 1. attilax的orm框架要求 1 2. orm框架市场占有率 2 3. spring jdbc templt 3 4. ...
- 实用crontab命令
常用crontab如下: crontab -e 编辑 crontabcrontab -l 显示 crontabcrontab -r 删除 crontabcrontab -v 显示上一次编辑 cro ...
- 深入理解Spark 2.1 Core (十一):Shuffle Reduce 端的原理与源代码分析
http://blog.csdn.net/u011239443/article/details/56843264 在<深入理解Spark 2.1 Core (九):迭代计算和Shuffle的原理 ...
- 让低版本IE也能正常运行HTML5+CSS3网站的3种解决方案
现在我们可以选择浏览器非常多,所以浏览器的环境也是种类繁多,同一个浏览器也是包含各种不同的版本,不同的版本之间的渲染方法也存在差异,,它们支持的 HTML5.CSS3 特性恐怕也不尽相同.这种情况于是 ...