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

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.

 
这是一道多重背包问题,题目的意思是有一台取款机,其中有n中不同的面值的金币,dk和nk表示dk这种面值的金币有nk个,求出不超过cash的最大金额的金币大小。应用《背包九讲》的多重背包问题模板就可以求出结果。
 
 #include <iostream>
#include <cstdio>
using namespace std;
#define N 100003
int c[N],w[N];
int dp[N];
int n, v;
void ZeroOnePack(int cost,int weight) //01背包
{
for(int k=v; k>=cost; k--)
dp[k] = max(dp[k],dp[k-cost]+weight);
}
void CompeletPack(int cost,int weight) //完全背包
{
for(int k=cost; k<=v; k++)
dp[k] = max(dp[k],dp[k-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount) //多重背包
{
if(cost*amount>=v)
{
CompeletPack(cost,weight);
return;
}
else
{
int k=;
while(k<amount)
{
ZeroOnePack(k*cost,k*weight);
amount = amount-k;
k=k*;
}
ZeroOnePack(amount*cost,amount*weight);
}
}
int main()
{
int i;
while(scanf("%d %d",&v,&n)!=EOF)
{
int totle = ;
int min = ;
for(i=; i<n; i++)
{
scanf("%d%d",&w[i],&c[i]); //每种金币的个数和金币面值
totle += w[i]*c[i];
if(min>c[i])
min = c[i];
}
if(totle<v) //如果金币的总量小于输入的v,直接将totle输出
{
printf("%d\n",totle);
continue;
}
if(v<min)//如果v小于金币的最小值,则直接输出0
{
printf("0\n");
continue;
}
memset(dp,,sizeof(dp));
for (i=; i<n; i++)
{
if(w[i]*c[i] == v)
{
dp[v] = v;
break;
}
else
MultiplePack(c[i],c[i],w[i]);//多重背包
} printf("%d\n",dp[v]);
}
return ;
}

Poj 1276 Cash Machine 多重背包的更多相关文章

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

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

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

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

  3. poj 1276 Cash Machine_多重背包

    题意:略 多重背包 #include <iostream> #include<cstring> #include<cstdio> using namespace s ...

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

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

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

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

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

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

  7. POJ 1276:Cash Machine 多重背包

    Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30006   Accepted: 10811 De ...

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

    链接:POJ - 1276 题意:给你一个最大金额m,现在有n种类型的纸票,这些纸票的个数各不相同,问能够用这些纸票再不超过m的前提下凑成最大的金额是多少? 题解:写了01背包直接暴力,结果T了,时间 ...

  9. Cash Machine(多重背包)

    http://poj.org/problem?id=1276 #include <stdio.h> #include <string.h> ; #define Max(a,b) ...

随机推荐

  1. 转:ibatis的cacheModel

    转:ibatis的cacheModel cachemodel是ibatis里面自带的缓存机制,正确的应用能很好提升我们系统的性能. 使用方法:在sqlmap的配置文件中加入 <cacheMode ...

  2. linux系统中批量查找文件与文件内容的方法

    在linux中查看与修改文件权限我们都必须使用命令来操作,不能像windows一样点几下就好了,下面我们简单的介绍一下linux中的相关命令 比如查找当前目录下面所有的php文件里面某个关键字 fin ...

  3. FK JavaScript之:ArcGIS JavaScript API之地图动画

    地图要素动画应用场景:动态显示地图上的要素的属性随着时间的改变而改变,并根据其属性的变化设置其渲染.比如:某水域项目中,随着时间的变化,动态展现水域的清淤进度 本文目的:对ArcGIS JavaScr ...

  4. 遍历ArrayList时同时修改引发的问题

    看见一篇博客,没有写完整,于是增补了一下: 博客原文:http://www.cnblogs.com/alipayhutu/archive/2012/08/11/2634073.html 注:黄色字体为 ...

  5. C语言中static的作用

    (1)在函数体内,局部的static变量.生存周期为程序的整个生命周期:作用域却在定义了的函数体内.一个被声明为静态的变量在这个函数被调用过程中维持其值不变.因为它分配在静态存储区域,函数调用结束以后 ...

  6. PHP 模拟 HTTP 摘要认证(Digest )

    <?php header("Content-type: text/html; charset=utf-8"); /*php摘要认证*/ $users = ['dee'=> ...

  7. 关于JavaScript的判断语句(2)

    上一篇描述的都是一些通过条件判断来执行,符合条件下面的代码块,达到对动作的反应效果 这篇将描述的是for. for/in. while. do/while循环语句. for语句: for(i=0,i& ...

  8. 给Source Insight做个外挂系列之四--分析“Source Insight”

    外挂的目的就是将代码注入到其它进程中,所以必须要有目标进程才能完成注入,而所谓的目标进程通常是某软件的一部分或者是全部,所以要对目标程序有深入地了解.一般外挂都是针对某个应用程序开发的,其装载.运行都 ...

  9. chrome中hack解决input:-webkit-autofill自定义样式

    在使用chrome浏览器设计网页时,想将input背景改成透明,也就是 background-color:transparent; 可是效果并不如人意 hack方法: input:-webkit-au ...

  10. Android新组件CardView

    Android L以后,新增了一个CardView组件,Google官方应用中有不少地方是使用卡片来展示信息,背后应该就是这个CardView. 使用CardView要引入单独的support包:co ...