题目地址:http://poj.org/problem?id=1276

Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination
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

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format:



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

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below.

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 -- 动态规划(背包问题)的更多相关文章

  1. Poj 1276 Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26172   Accepted: 9238 Des ...

  2. POJ 1276 Cash Machine(单调队列优化多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38986   Accepted: 14186 De ...

  3. poj 1276 Cash Machine(多重背包)

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33444   Accepted: 12106 De ...

  4. POJ 1276 Cash Machine

    Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24213 Accepted: 8476 Descrip ...

  5. 【转载】poj 1276 Cash Machine 【凑钱数的问题】【枚举思路 或者 多重背包解决】

    转载地址:http://m.blog.csdn.net/blog/u010489766/9229011 题目链接:http://poj.org/problem?id=1276 题意:机器里面共有n种面 ...

  6. [poj 1276] Cash Machine 多重背包及优化

    Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...

  7. POJ 1276 Cash Machine(多重背包的二进制优化)

    题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...

  8. POJ 1276 Cash Machine 【DP】

    多重背包的模型,但一开始直接将N个物品一个一个拆,拆成01背包竟然T了!!好吧OI过后多久没看过背包问题了,翻出背包九讲看下才发现还有二进制优化一说........就是将n个物品拆成系数:1,2,4, ...

  9. POJ 1276 Cash Machine(完全背包模板题)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44409   Accepted: 16184 Description A B ...

随机推荐

  1. ecshop数据库表结构

    ecs_account_log //用户账目日志表 ecs_activity //活动表(代码,名称,开始,结束,描述) ecs_ad //广告表(位置,类型,名称,链接,图片,开始,结束,广告主相关 ...

  2. Android开发之有效获取状态栏(StatusBar)高度

    获取状态栏高度 一.传统方式:有时获取为0,解决方法看  二 1 2 3 4 代码 Rect frame = new Rect(); getWindow().getDecorView().getWin ...

  3. 使用 Spring 3 MVC HttpMessageConverter 功能构建 RESTful web 服务

    原文地址:http://www.ibm.com/developerworks/cn/web/wa-restful/ 简介: Spring,构建 Java™ 平台和 Enterprise Edition ...

  4. 逻辑网络(Logical Network)

    Introduction The VMM documentation indicates that “A logical network is used to organize and simplif ...

  5. Java登陆测试

    package test001; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.IOEx ...

  6. httpclient模拟浏览器訪问站点

    HttpClient 是 Apache Jakarta Common 下的子项目.能够用来提供高效的.最新的.功能丰富的支持 HTTP 协议的client编程工具包.而且它支持 HTTP 协议最新的版 ...

  7. 新闻客户端nices

    https://github.com/android-cjj/nices

  8. [React Native] Using the Image component and reusable styles

    Let's take a look at the basics of using React Native's Image component, as well as adding some reus ...

  9. 信号之sigsetjmp和siglongjmp函数

    在信号处理程序中经常调用longjmp函数以返回到程序的主循环中,而不是从该处理程序返回. 但是,调用longjmp有一个问题.当捕捉到一个信号时,进入信号捕捉函数,此时当前信号被自动地加到进程的信号 ...

  10. 如何用 PHPMailer 来发送邮件?

    <?php require_once('mantisbt-1.2.15/library/phpmailer/class.phpmailer.php'); $mail= new PHPMailer ...