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) ...
随机推荐
- spinner的使用
item.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- struts2--验证器
1.输入验证: --struts2提供了一些基于Xwork Validation Framework的内建验证程序,使用这些验证程序不需要变编程,只要在一个XML文件里进行声明,声明的内容如下: &g ...
- PHP 获取header 的自定义参数值
$.ajax({ type: "GET", url: "default.aspx", beforeSend: function(request) { reque ...
- Python数据分析在互联网寒冬下,数据分析师还吃香吗?
伴随着移动互联网的飞速发展,越来越多用户被互联网连接在一起,用户所积累下来的数据越来越多,市场对数据方面人才的需求也越来越大,由此也带火了如数据分析.数据挖掘.算法等职业,而作为其中入门门槛相对较低. ...
- Java得到一个整数的绝对值,不使用任何判断和比较语句,包括API.
/** * Java得到一个整数的绝对值,不使用任何判断和比较语句,包括API. <br> * 1.不得使用任何API,如Math.abs()等.<br> * 2.不得使用判断 ...
- 洛谷 P4287 [SHOI2011]双倍回文题解
前言 用了一种很奇怪的方法来解,即二分判断回文,再进行某些奇怪的优化.因为这个方法很奇怪,所以希望如果有问题能够 hack 一下. 题解 我们发现,这题中要求的是字符串 \(SS'SS'\),其中 \ ...
- Linux 命令 - mknod
mknod 创建块设备或者字符设备文件.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 mknod [选项] 设备名 设备类 ...
- sklearn的train_test_split()各函数参数含义解释(非常全)
sklearn之train_test_split()函数各参数含义(非常全) 在机器学习中,我们通常将原始数据按照比例分割为“测试集”和“训练集”,从 sklearn.model_selection ...
- Tomcat启动时加载数据到缓存---web.xml中listener加载顺序(例如顺序:1、初始化spring容器,2、初始化线程池,3、加载业务代码,将数据库中数据加载到内存中)
最近公司要做功能迁移,原来的后台使用的Netty,现在要迁移到在uap上,也就是说所有后台的代码不能通过netty写的加载顺序加载了. 问题就来了,怎样让迁移到tomcat的代码按照原来的加载顺序进行 ...
- 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:匿名内部类
interface A{ public void printInfo() ; // } class B implements A{ // 实现接口 public void printInfo(){ S ...