poj 1276 Cash Machine(多重背包)

poj 1276(多重背包) 我的英语差的不只一点点啊,题意看不完全懂,直接看别人的解题报告才把题意弄清(我很菜),但是感觉这题完全就是完全背包基础知识,只要将cost视为与weight完全相等就ok。 #include #include #include using namespace std; #define N 11 int dp[100010]; int num[N]; int a[N]; int V,n; inli 阅读详情
 
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 19122 Accepted: 6659

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

Hint

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash.

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.

Source

[Submit] [Go Back] [Status] [Discuss]

 

Home Page Go Back To top

 

 

思路1:将多重背包转换为01背包问题

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100005
using namespace std;
int w[N],cost[N],num[N];
int money,m;
void DP()
{
  int i,j,k;
  for(k=0;k<=money;k++)
  for(i=0;i<m;i++)
  {
    for(j=1;j<=num[i];j++)
    {
      if(k-cost[i]>=0)
      w[k]=max(w[k],w[k-cost[i]]+cost[i]);
    }
  }
}
int main()
{
  while(scanf("%d%d",&money,&m)!=EOF)
  {
    for(int i=0;i<m;i++)
    scanf("%d%d",&num[i],&cost[i]);
    memset(w,0,sizeof(w));
    DP();
    printf("%d\n",w[money]);
  }
}


超时TLE了。

思路二:利用多重背包特性

#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100005
using namespace std;
int w[N],cost[N],num[N],dpnum[N];
int money,m;
void DP()
{
  int i,j,k;
  for(i=0;i<m;i++)
  {
    memset(dpnum,0,sizeof(dpnum));
    for(j=cost[i];j<=money;j++)
    {
      if(w[j]<w[j-cost[i]]+cost[i]&&dpnum[j-cost[i]]<num[i])
      {
        w[j]=w[j-cost[i]]+cost[i];
        dpnum[j]=dpnum[j-cost[i]]+1;
      }
    }
  }
}
int main()
{
  while(scanf("%d%d",&money,&m)!=EOF)
  {
    for(int i=0;i<m;i++)
    scanf("%d%d",&num[i],&cost[i]);
    memset(w,0,sizeof(w));
    DP();
    printf("%d\n",w[money]);
  }
}



 

POJ1276 Cash Machine详解(多重背包与二进制优化) 我的第二篇题解hhh 虽然网上可以搜到的该题的题解并不少,而且都能确定出是“多重背包”的解法。但是对于“背包”和“物品”的描述,我感觉交待的并不是很清楚,至少对于像我这样的初学者来说,是不容易理解的。 这篇博客可以帮助你更好的理解这个题目,但是你必须有多重背包的基础。 ———————————————————————————————————— 原题 POJ1276 Cash Machine 题意 有... 阅读详情

相关推荐

poj1276解答和分析

这里我把自己对poj1276的理解和分析还有源代码都放在了里面,希望对大家有所帮助。谢谢

Cash Machine 多重背包

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...

wait 364

鸿蒙读书笔记1:《鸿蒙操作系统设计原理与架构》

GUI、 语 音 控 制 方 式 的 VUI(Voice User Interface, 语 音 用 户 界。电话、多媒体、DFX、MSDP(Multimodal Sensor Data Platform,多。面)、手势/姿态控制的CVUI(Computer Vision User Interface,计算。KAL(Kernel Abstract Layer,内核抽象层)通过屏蔽多内核差异,JavaScript、C、C++等多语言的用户程序开发框架,以及UI框架、

jackyrongvip的专栏 2460

POJ 1276 多重背包问题

题目在 http://poj.org/problem?id=1276 题目大意是说,我现在要从ATM中取钱,M ATM里面有若干种货币,每一种都有对应的货币面额和张数。 问现在ATM能够取出来的 小于等于M的最大金额。 若将M理解为背包重量,而每种货币的面额理解成 value,货币的面额同样理解成重量,那么这个问题就是一个多重背包问题。 多重背包问题可以转换成完全

文西 2267

三相SST固态变压器详细设计案例

本案例针对400kVA、30kHz三相固态变压器(SST)开展详细设计,核心采用输入串联输出并联(ISOP)拓扑架构(模块化多电平矩阵变换器M3C简化版),重点完成高频变压器的参数计算、绕组设计、损耗分析及液冷温升验证,同时覆盖系统级架构设计、半导体损耗估算及整机效率优化,全流程基于当前电力电子技术可行性展开。

qq_32103755的博客 3389

风力发电机并网模型建立及仿真验证

传统能源日益匮乏,环境污染日趋严重,全球向安全、可靠和环保的可持续能源体系转型迫在眉睫,风电迎来了重要的发展契机。近年来,全球风电迅猛发展,我国已成为世界风电新增装机容量第一的国家。然而,随着风电规模的不断增大,风电场接入对系统影响不容忽视。随着变速恒频风力发电系统优越的性能突出和大容量电力电子技术的成熟,风速随机性引起的功率波动及故障情况下的动态响应,对电能质量,系统电压、频率的稳定性产生的影响需要深入研究。因而,本文结合实际项目,针对变速恒频风力发电系统建模,端口动态特性及对电网电能质量、运行稳定性的影

毕业设计区博客 1666

POJ - 1276 二进制优化多重背包为01背包

题意:直接说数据,735是目标值,然后3是后面有三种钱币,四张125的,六张五块的和三张350的。 思路:能够轻易的看出这是一个多重背包问题,735是背包的容量,那些钱币是物品,而且有一定的数量,是多种背包。但是做的时候总是超时。可能是因为m和n太大。然后可以通过二进制把它转化为01背包,因为将钱币的数量化为二进制,1 2 4直到数量减一。化成的二进制数字排列组合,可以组成任意钱币数量...

HappyCode 261

Trae CN 使用入门指南

【代码】Trae CN 使用入门指南。

weixin_42551921的博客 8828

Cash Machine POJ - 1276 (多重背包)

链接:http://poj.org/problem?id=1276 题意:给出一个n 代表最多拿的值为n, m种钱币, 每种有 x个, 值为y,问在不超过n的情况下最多能拿多少钱。解题思路:也算是多重背包的模板题了,做法是将多重背包化成0 1背包, 将每种钱币分别分成 1个 2个 4个…..当不够分的时候就将剩下的也当作一组, 这样每一组都对应一个不同的值 y*1, y*2, y*4….然后直接0

sdut16szq的博客 415

利用Android系统漏洞实现后台弹窗(外弹)

关键:从 Android 8.0(API 26)开始,禁止使用 context.sendBroadcast() 发送隐式广播(即没有指定 setPackage() 或明确组件的广播)。demo核心代码展示利用Android显示系统和广播系统的漏洞实现在后台弹窗的功能,支持最新Android15,16版本。核心功能都是在so层去做,大量使用了jni反调, 如果要上架到应用市场,需要对代码做进一步混淆,加固以应对市场安全扫描。3,开启虚拟显示窗口,这是个透明像素窗口。5,可开启一个线程,调用这个外弹窗口。

2018,再出发~ 1333

1、vectorCast单元测试常用操作

进入软件主页面,点击file,选择set working directory,随便选择一个保存该项目的目录即可。

weixin_59334478的博客 2170

POJ1276-Cash Machine(多重背包)

题目链接:poj挂了以后补上 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 dis

Yorozuya 566

黑猫带你学UFS协议第23篇:UFS固件升级操作详解(FFU)

摘要:本文介绍了UFS固件更新(FFU)的实现方法与注意事项。根据UFS3.1 JEDEC协议,FFU通过write buffer命令(MODE=0Eh)下载FW固件,随后必须进行power cycle或硬件复位生效。文章详细阐述了FFU操作的10条规则,包括数据分片传输要求、寄存器访问限制等,同时指出协议未明确的厂商实现差异。关键寄存器包括bFFUTimeout、bDeviceFFUStatus和fPermanentlyDisableFwUpdate。

黑猫奥利奥的博客 1万+

POJ 1276 Cash Machine【多重背包】

题意:有各种不同面值的货币,每种面值的货币有不同的数量,请找出利用这些货币可以凑成的最接近且小于等于给定的数字cash的金额. 样例1: 要取735,取款机内有3种钱:4张125的,6张5的,3张350的. 735=3*125+2*5+1*350 动态规划,多重背包,可以二进制优化.

AC_Dreameng 1107

【OpenFOAM】-olaFlow-算例10-wavemakerTank

采用 Flap和Piston两种方式的动网格进行造波

ou_no的博客 2175

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

poj1276 Cash Machine(多重背包)

Cash Machine 本题大意:给出一个最大金额,然后给出k中币的金额和数量,求能正好提供的最大小于等于最大金额的金额。 本题是多重背包模板题,直接套上模版就可以解决。 #include <iostream> #include <cstdio> #include <algorithm> #include <string> #include &lt...

zxc757126309的博客 188

inori mmd 7z

楪祈的模型资源....................................................................................................................................................................................................................

POJ 1276 Cash Machine(多重背包)

题目地址:

scf0920 1052

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

POJ1276:Cash Machine(多重背包)

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 denominatio

ACM!荣耀之路! 8152

POJ 1276 Cash Machine 多重背包

Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31470   Accepted: 11307 Description A Bank plans to install a machine for cash withdrawal. The m

Houheshuai的专栏 541

【POJ】1276 Cash Machine(多重背包)

Cash Machine 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 distinc

Silence的程序实验室 998

Cash Machine POJ - 1276 (多重背包(模板))

Cash Machine  POJ - 1276  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 exactl

Learning_is_endless的博客 371

POJ1276 Cash Machine 多重背包

POJ1276 Cash Machine 多重背包

qq_44919369的博客 178

Cash Machine POJ - 1276 多重背包

一、内容 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 ...

Tizzi的博客 845

poj 1276 Cash Machine 多重背包

传送门:poj 1276 Cash Machine题目大意有各种不同面值的货币,每种面值的货币有不同的数量,请找出利用这些货币可以凑成的最接近且小于等于给定的数字cash的金额。 输入的前两个数表示背包容量,第二个数字表示有N种货币 接下来的N组两个数。第一个数表示货币数量,第二个数表示货币的价值 输出最接近并且小于等于背包容量的价值。解题思路在理解的01背包的基础上, 完全背包,指每个物品有

woshinannan的专栏 397

poj 1276 Cash Machine 多重背包

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 denomin

u013087645的专栏 549
上一篇: h
下一篇: hdu 1081 To The Max
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址