POJ 1276:Cash Machine 多重背包
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 30006 | Accepted: 10811 |
Description
of nk bills. For example,
N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10
means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each.
Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine.
Notes:
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc.
Input
cash N n1 D1 n2 D2 ... nN DN
where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers
in the input. The input data are correct.
Output
Sample Input
735 3 4 125 6 5 3 350
633 4 500 30 6 100 1 5 0 1
735 0
0 3 10 100 10 50 10 10
Sample Output
735
630
0
0
Hint
In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered
cash.
In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.
题意是给你几种金钱的面额,每种面额有多少张纸币,然后给一个target金额。问在给定的纸币条件下,能凑成的最大的小于等于target的面额是多少。
关于多重背包,背包九讲里是这么解答处理一件多重背包物品的过程的:
def MultiplePack(F,C,W,M)
if C * M >= V
CompletePack(F,C,W)
return
k := 1
while k < M
ZeroOnePack(kC,kW)
M := M - k
k := 2k
ZeroOnePack(C * M, W * M)
我的理解:首先对于一件物品来说,如果这个物品的容量*数目已经大于背包的总容量了,那么这与完全背包问题没有区别了。因为完全背包问题就是可以任意的往背包里面放物品,而这时这个物品的容量*数目已经大于背包总容量了,所以,也就相当于在背包总容量的范围内,这个物品是可以支持 任意地往背包里面放物品。
如果这个物品的容量*数目小于背包的总容量,那么正常情况下,需要一件一件往里面放找最大值的。但是它的方法是:将第i种物品分成若干件01背包中的物品,其中每件物品有一个系数。这件物品的费用和价值均是原来的费用和价值乘以这个系数。令这些系数分别为1,2, 2的2次方,2的三次方,。。。2的k-1次方,最后是Mi - 2的k次方 + 1,且k是满足Mi - 2的k次方 + 1 的最大整数。例如,如果Mi为13,则相应的k=3,这种最多取13件物品的应被分成系数分别为1
2 4 6 的四件物品。
我觉得这里设计的很nice在于,正常情况下,举例来说这个物品有13件,我们需要一件一件放,放1件,2件,3件,。。。13件都放入,然后不断找最大值。但是上面的方法利用二进制,4个数,就做了13个数的事情。
发现 1 2 4 6 这四个数相互之间的和 就已经可以表示1到13的所有数,原因就在于当我放入1件物品,再放入2件物品找最大值的时候,实际上相当于我一下子放入了该物品的3件,所以实际上3是不需要我们再去实验的了。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int w, n;
int num[500002];
int val[500002];
int c[500002];
long long dp[500002]; void Zero_Pack(int weight, int cost, int w)
{
int i;
for (i = w; i >= cost; i--)
{
dp[i] = max(dp[i], dp[i - cost] + weight);
}
} void Complete_Pack(int weight,int cost,int w)
{
int i;
for (i = cost; i <= w; i++)
{
dp[i] = max(dp[i], dp[i - cost] + weight);
}
} long long Multi_Pack(int c[],int val[],int num[],int n,int w)
{
memset(dp, 0, sizeof(dp));
int i;
for (i = 1; i <= n; i++)
{
if (num[i] * c[i] > w)
Complete_Pack(val[i],c[i],w);
else
{
int k = 1;
while (k < num[i])
{
Zero_Pack(k*val[i],k*c[i],w);
num[i] = num[i] - k;
k = k << 1;
}
Zero_Pack(num[i]*val[i],num[i]*c[i],w);
}
}
return dp[w];
}
int main()
{
int i, j, v, max_v,re;
while (scanf("%d%d", &n,&w) != EOF)
{
memset(dp, 0, sizeof(dp));
for (i = 1; i <= n; i++)
{
scanf("%d%d%d", c + i, val + i, num + i);
}
re = Multi_Pack(c, val, num, n, w);
cout << re << endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1276:Cash Machine 多重背包的更多相关文章
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- [poj 1276] Cash Machine 多重背包及优化
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
- poj 1276 Cash Machine_多重背包
题意:略 多重背包 #include <iostream> #include<cstring> #include<cstdio> using namespace s ...
- 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】
转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...
- poj 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33444 Accepted: 12106 De ...
- POJ 1276 Cash Machine(单调队列优化多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38986 Accepted: 14186 De ...
- Cash Machine (POJ 1276)(多重背包——二进制优化)
链接:POJ - 1276 题意:给你一个最大金额m,现在有n种类型的纸票,这些纸票的个数各不相同,问能够用这些纸票再不超过m的前提下凑成最大的金额是多少? 题解:写了01背包直接暴力,结果T了,时间 ...
- Cash Machine(多重背包)
http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...
随机推荐
- flutter 启动时一直Resolving dependencies...
原因:国内网无法从Google获取资源,貌似搭了梯子也没用 修改flutter sdk Path/packages/flutter_tools/gradle/flutter.gradle这个文件,使用 ...
- Linux centos7 日常运维——使用w查看系统负载、vmstat命令、top命令、sar命令、nload命令
一.使用w查看系统负载 w .uptime查看系统负载,0.00表示1分钟之内负载为0 cat /proc/cpuinfo查看cpu核数 二.vmstat命令,查看进程.cpu.memory.交换. ...
- java关于hasNext()
编写一段程序实现如果输入的一组数中含非整数数字,输出数字相加的和以及"attention"字符,如果全部是数字便输出数字的和. 程序1: package mian; import ...
- 1-7SpringBoot之表单验证@Valid
SpringBoot提供了强大的表单验证功能实现,给我们省去了写验证的麻烦: 这里我们给下实例,提交一个有姓名和年龄的表单添加功能, 要求姓名不能为空,年龄必须是不小于18 : 我们先新建一个Stud ...
- QEMU 运行uboot,动态加载内核与文件系统
背景 上一讲我们完成了 编译 QEMU 以及简单地做了仿真.这一讲在 启动uboot 的基础上进行,以加强对于 运行地址,加载地址等理解. 有关资料: uboot 与 代码重定位 有这样的约定,ubo ...
- 将证书添加到受信任的根证书存储失败,出现以下错误:访问控制列表(ACL)结构无效
问题出现情景: 使用 vs2017 创建一个 ASP.NET Core Web 应用程序 -> Ctrl + F5 运行项目 选择是,但是添加证书失败,是什么原因导致的我不知道,有大佬的知道的话 ...
- lvs负载均衡配置
三台server的ip direct_server:192.168.248.128 real_server1:192.168.248.130 real_server2:192. ...
- 2020寒假 05 ——eclipse安装scala环境
在eclipse中安装Scala环境 1安装eclipse插件步骤,点击help,选择Eclipse Marketplace 2.输入Scala,点击go 3.选择搜索到的Scala IDE 4.7. ...
- BAT 五路internet负载均衡
一个网上下载的bat文件 也不记得从那里下载的了 记得似乎需要管理员权限运行 依稀记得测试有效 放在这里做个记录 @echo off echo. echo ╭─────────╮ echo ╭──── ...
- python 文件夹递归
import ospath = "F:/new" #文件夹目录datas = []def eachFile(filepath): fileNames = os.listdir(fi ...