POJ1276:Cash Machine(多重背包)
Description
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.
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; struct node
{
int n,v;
} a[20]; int dp[100010]; int main()
{
int sum,n,i,j,k;
while(~scanf("%d%d",&sum,&n))
{
for(i = 1; i<=n; i++)
scanf("%d%d",&a[i].n,&a[i].v);
if(!sum)
{
printf("0\n");
continue;
}
if(!n)
{
printf("0\n");
continue;
}
memset(dp,0,sizeof(dp));
dp[0] = 1;
int MAX = 0,tem;
for(i = 1; i<=n; i++)
{
for(j = MAX;j>=0;j--)
{
if(dp[j])
{
for(k = 1;k<=a[i].n;k++)
{
tem = j+k*a[i].v;
if(tem>sum)
continue;
dp[tem] = 1;
if(tem>MAX)
MAX = tem;
}
}
}
}
printf("%d\n",MAX);
} return 0;
}
POJ1276:Cash Machine(多重背包)的更多相关文章
- POJ-1276 Cash Machine 多重背包 二进制优化
题目链接:https://cn.vjudge.net/problem/POJ-1276 题意 懒得写了自己去看好了,困了赶紧写完这个回宿舍睡觉,明早还要考试. 思路 多重背包的二进制优化. 思路是将n ...
- POJ1276 - Cash Machine(多重背包)
题目大意 给定一个容量为M的背包以及n种物品,每种物品有一个体积和数量,要求你用这些物品尽量的装满背包 题解 就是多重背包~~~~用二进制优化了一下,就是把每种物品的数量cnt拆成由几个数组成,1,2 ...
- POJ1276:Cash Machine(多重背包)
题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #in ...
- 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: 30006 Accepted: 10811 De ...
- Cash Machine(多重背包)
http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...
- POJ 1276 Cash Machine(多重背包的二进制优化)
题目网址:http://poj.org/problem?id=1276 思路: 很明显是多重背包,把总金额看作是背包的容量. 刚开始是想把单个金额当做一个物品,用三层循环来 转换成01背包来做.T了… ...
- PKU--1267 Cash Machine(多重背包)
题目http://poj.org/problem?id=1276 分析 这是一个多重背包的问题,可以把请求的金额当作背包的重量,而货币的面值就是价值又是重量. 于是这个问题便很好理解背包了. #];; ...
- [poj 1276] Cash Machine 多重背包及优化
Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver ap ...
随机推荐
- PHP判断远程图片或文件是否存在
PHP判断远程图片是否存在,此方法同样适用于判断远程文件是否存在,这是一种既然有效率且又准确的方法,建议采用此方法,以往使用get_headers()方法判断都是有问题的: function chec ...
- CWnd类
CWnd类的成员 .数据成员 m_hWnd 指明与这个CWnd对象相关联的HWND句柄 .构造和析构 CWnd 构造一个CWnd对象 DestroyWindow 销毁相关联的Windows窗口 .初始 ...
- Linux_shell条件判断if中的-a到-z的意思
[ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则 ...
- Oracle merge into 使用记录
符合条件进行更新操作,不符合则进行插入操作. merge into myd_nsrdt n using ('as nsrsbh,'' as nsrmc, ' as nowphone,sysdate a ...
- 四句话表明JSON格式定义
(1):并列的数据之间使用逗号(",")分割: (2):键值对的映射用冒号(":")表示: (3):并列数据的集合(数组)用方括号("[]" ...
- Android 将从网络获取的数据缓存到私有文件
1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- 游览器保存密码和自动填充密码的困惑 (browser save password and auto fill password )
原文 refer : http://www.cnblogs.com/happyfreelife/p/4240100.html 当一个带有username and password 的表单被提交, 游览 ...
- 无法关闭的QT程序(覆盖closeEvent,新建QProcess并脱离关系)
做一个无法关闭的QT程序(想关闭时要在任务管理器里关闭),看似很难, 其实它并不难,只要让程序在关闭时启动它自身就可以了. 上代码: #include <QtGui> class Temp ...
- android调用系统自带的的浏览器搜索关键字
//调用系统的浏览器搜索详情 public void jumpBrowser(String value) { /* 取得网页搜寻的intent */ Intent search = new Inten ...
- Java Hibernate 主键生成10大策略
本文将介绍Hibernate中主键生成的几种策略方案,有需要的朋友可以参考一下. 1.自动增长identity 适用于MySQL.DB2.MS SQL Server,采用数据库生成的主键,用于为lon ...