您的位置:首页 > 其它

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

2017-11-21 22:07 369 查看


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

题意:给定金额最大值  告知每种货币的价值和个数,求能拼凑成给定金额范围内的最大金额,即多重背包问题。

思路:多重背包可以分解为01背包和完全背包,这样可以大量节省时间,那么为什么可以分为01背包和完全背包呢?原因就是,当此物品占用的总空间大于或等于背包当前容量的时候,我们可以想象成是可以从此种物品中随意取,因为此时在背包容纳的范围内无论取多少都不会超出物品的个数,即可想象为能够无限取,此时即为完全背包。当此物品占用的空间小于此时背包的容量时,即可想为价值不同的01背包,举个例子来说,假如给了我们 价值为 2,但是数量却是10 的物品,我们应该把10给拆开,要知道二进制可是能够表示任何数的,所以10 就是可以有1,2,
4,8之内的数把它组成,一开始我们选上 1了,然后让10-1=9,再选上2,9-2=7,在选上 4,7-4=3,而这时的3<8了,所以我们就是可以得出 10由 1,2,4,3,来组成,就是这个数量为1,2,3,4的物品了,那么他们的价值是什么呢,是2,4,6,8,也就说给我们的价值为2,数量是10的这批货物,已经转化成了价值分别是2,4,6,8元的货物了,每种只有一件啦,这就是二进制优化的思想。

AC代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define maxn 100005
using namespace std;

int V,n;
int f[maxn];
void ZeroonePack(int weight,int value) //01背包
{
int v;
for(v=V;v>=weight;--v){
f[v]=max(f[v],f[v-weight]+value);
}
}
void ComplatePack(int weight,int value) //完全背包
{
int v;
for(v=weight;v<=V;++v){
f[v]=max(f[v],f[v-weight]+value);
}
}
void MultiplePack(int weight,int value,int amount) //多重背包
{
int k;
if(amount*weight>=V){ //当此物品的体积大于背包体积时可以想象为完全背包
ComplatePack(weight,value);
return ;
}
k=1;
while(k<amount){ //将多重背包转换为多个01背包
ZeroonePack(k*weight,k*value);
amount-=k;
k*=2;
}
ZeroonePack(amount*weight,amount*value);
}
int main()
{
int nk[maxn],dk[maxn];
while(scanf("%d %d",&V,&n)!=EOF){
memset(f,0,sizeof(f));
for(int i=1;i<=n;++i){
scanf("%d %d",&nk[i],&dk[i]);
}
for(int i=1;i<=n;++i){
MultiplePack(dk[i],dk[i],nk[i]);
}
printf("%d\n",f[V]);
memset(nk,0,sizeof(nk));
memset(dk,0,sizeof(dk));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: