POJ 1276 Cash Machine -- 动态规划(背包问题)
题目地址:http://poj.org/problem?id=1276
Description
Dk the machine has a supply 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
#include <stdio.h>
#include <string.h> #define MAX 100001
#define MAXN 11
#define Max(a, b) (a) > (b) ? (a) : (b) int cash;
int num[MAXN];
int deno[MAXN];
int dp[MAX]; void ZeroOnePack (int deno){
int i;
for (i=cash; i>=deno; --i)
dp[i] = Max(dp[i], dp[i-deno] + deno);
} void CompletePack (int deno){
int i;
for (i=deno; i<=cash; ++i)
dp[i] = Max(dp[i], dp[i-deno] + deno);
} void MultiplePack (int deno, int num){
if (deno * num >= cash)
CompletePack (deno);
else{
int k = 1;
while (k < num){
ZeroOnePack (deno * k);
num -= k;
k *= 2;
}
ZeroOnePack (deno * num);
}
} int main(void){
int N;
int i; while (scanf ("%d%d", &cash, &N) != EOF){
for (i=1; i<=N; ++i){
scanf ("%d%d", &num[i], &deno[i]);
}
memset (dp, 0, sizeof(dp));
for (i=1; i<=N; ++i){
MultiplePack (deno[i], num[i]);
} printf ("%d\n", dp[cash]);
} return 0;
}
参考资料:背包问题九讲
POJ 1276 Cash Machine -- 动态规划(背包问题)的更多相关文章
- Poj 1276 Cash Machine 多重背包
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26172 Accepted: 9238 Des ...
- POJ 1276 Cash Machine(单调队列优化多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38986 Accepted: 14186 De ...
- 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: 24213 Accepted: 8476 Descrip ...
- 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】
转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...
- [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(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- POJ 1276 Cash Machine 【DP】
多重背包的模型,但一开始直接将N个物品一个一个拆,拆成01背包竟然T了!!好吧OI过后多久没看过背包问题了,翻出背包九讲看下才发现还有二进制优化一说........就是将n个物品拆成系数:1,2,4, ...
- POJ 1276 Cash Machine(完全背包模板题)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44409 Accepted: 16184 Description A B ...
随机推荐
- Clarkson不等式
- Struts的类型转换
struts中jsp提交的数据,struts会自动转换为action中的属性类型: 对于基本的数据类型以及日期类型会自动转换: 就像前面那一篇博客的代码封装一样,会把字符串类型传过来的数据转化为相应的 ...
- mysql 在线修改表结构工具 gh-ost
gh-ost使用测试: gh-ost -host='192.168.65.136' -user=root -password='' -database='haha' -chunk-size=10000 ...
- [HTML5] Input accepts only 6 number characters
Use 'pattern' tag in html5: <input type="text" pattern="[0-9]{6}" maxlength=& ...
- shell重定向调试信息
shell重定向调试信息 fulinux ******************************************************************************* ...
- java推断字符串是否为乱码
项目中有一个功能 在IE中GET方式提交会产生乱码 但有两个入口都会走这同一段代码 固不能直接转码,所以要进行推断传过来的该值是不是乱码 可用下面方式验证: java.nio.charset.Char ...
- C/C++开发工具大比拼【转】
C/C++开发工具大比拼[转] (http://hi.baidu.com/vipdowndown/blog/item/dcd7c1b5ad3209ef30add167.html) * NetBean ...
- mysqldump 备份原理9
前文的一个细节http://blog.itpub.net/29254281/viewspace-1392757/ 5.--master-data + --single-transaction 同时 ...
- WordPress搭建Personal Blog
早就想搭建一个专属于自己的博客了,用来记录自己生活.学习的点点滴滴.之所以选WordPress,主要是因为它可以支持Latex,而且特别喜欢其简约的风格. WordPress有个the famous ...
- 获取select值
//Jquery中select的使用 //select获取当前选中的value值 $('#sel').change(function () { var depId = $(this).children ...