POJ 1276 Cash Machine 【多重背包】

来源:互联网 发布:淘宝网图片保护入口 编辑:程序博客网 时间:2024/05/22 02:30

题目来戳呀

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.
题意:给你一个金额,请找出利用这些货币可以凑成的最接近且小于等于给定的数字cash的金额。
想法:理解题意最重要==静下心来读一读就很简单了啊==就是一道多重背包的裸题==和hdu2191一模一样,甚至还要简单==

#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;const int maxn=1e5+5;int w[maxn],dp[maxn];int main(){    int n,m,cou;    int temp1[maxn],temp2[maxn];    while(~scanf("%d %d",&n,&m))//钱数 种数    {        cou=1;        memset(w,0,sizeof(w));        memset(dp,0,sizeof(dp));        memset(temp1,0,sizeof(temp1));        memset(temp2,0,sizeof(temp2));        for(int i=1;i<=m;++i)        {            scanf("%d %d",&temp1[i],&temp2[i]);//纸币数 纸币金额            for(int k=1;k<=temp1[i];k<<=1)            {                w[cou++]=k*temp2[i];                temp1[i]-=k;            }            if(temp1[i])            {                w[cou++]=temp1[i]*temp2[i];            }        }        for(int i=1;i<=cou;++i)        {            for(int j=n;j>=w[i];--j)            {                dp[j]=max(dp[j-w[i]]+w[i],dp[j]);            }        }        printf("%d\n",dp[n]);    }  return 0;}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果6s锁屏密码忘了怎么办 父亲去世后妈将父亲存款带走怎么办 狗和别的狗打架腿瘸了怎么办 神经病砍人警察不积极处理怎么办 才买了车里面的气味很大怎么办 家里装修两年了很大的木味怎么办 装修一年的房子夏天味很大怎么办 死了怕下地狱活着又受煎熬怎么办 狗狗的疫苗证丢了怎么办 剃了毛的狗不睡觉怎么办 家里的小狗送人了孩子一直哭怎么办 半个月的小狗一天没拉屎怎么办 把狗狗的毛剃了怎么办 用了维a酸乳膏过敏怎么办 药水点痣留下的红印怎么办 小孩牙齿被虫子吃了个洞怎么办 一岁宝宝贫血值是84怎么办 荒岛求生手机版被困在石室里怎么办 工伤认定期间被厂里辞退工资怎么办 怀孕50天看恐怖片肚子阴痛怎么办 欧卡二进游戏就卡画面了怎么办 魅族手机的微信图标找不到了怎么办 金立手机:微信图标找不到怎么办? 鞋厂装跟机老是卡钉怎么办 苹果手机摔了一下开不了机怎么办 百度网盘解析的种子保存不了怎么办 下载了种子百度网盘解析不了怎么办 正畸复诊后吃饭一直会磨嘴怎么办 遇到儿子说话对母亲不满母亲怎么办 冰箱旧了怎么办教你创新冰箱翻新法 微信陌生人加好友发红包怎么办 微信红包发了对方收不到怎么办 发红包是微信密码不知道了该怎么办 微信上红包密码不知道密码怎么办 本人不知道微信自动发红包怎么办 微信钱包里的钱没了怎么办 微信聊天删除了红包没领怎么办 微信红包未领取删掉了怎么办 微信红包没领取删掉了怎么办 微信红包没领取就删除好友了怎么办 老公一个人出去玩不愿带老婆怎么办