Poj 1276 Cash Machine 多重背包

来源:互联网 发布:哔哩哔哩客户端mac 编辑:程序博客网 时间:2024/06/10 09:20
Cash Machine
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 28596 Accepted: 10209

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 350633 4  500 30  6 100  1 5  0 1735 00 3  10 100  10 50  10 10

Sample Output

73563000

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的金额。;


多重背包:


 有N种物品和一个容量为V的背包。第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。


乍一看感觉和完全背包差不多,其实就是与完全背包不同就在于,对于第i件物品,有你n[i]+1种策略,取0,1,2,3,.........n[i]件,

令f[i][v]表示前i种物品恰放入一个容量为v的背包的最大权值,则有状态转移方程:

f[i][v]=max{f[i-1][v-k*c[i]]+k*w[i]|0<=k<=n[i]}


所以,我们依然可以转换为01背包来求解
for(int i=0;i<N;i++){    for(int j=0;j<n[i];j++){        for(int k=V;k>=v[i];k--){            dp[k]=max(dp[k],dp[ k-v[i] ]+w[i]);        }    }}
但是当当数量太多的时候,这种单纯的转换就比较容易超时了;所以我们要进行二进制优化;及吧这个数拆解为1,2,4,…,2^(k-1),n[i]-2^k+1,例如,n[i]=13,就将该物品拆成系数为1、2、4、6的四件物品。分成的这几件物品的系数和为n[i],表明不可能取多于n[i]件的第i种物品。另外这种方法也能保证对于0..n[i]间的每一个整数,均可以用若干个系数的和表示。

比如这个题

#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 100010int cash,N;int dp[maxn];int cost[maxn];int main(){    while(scanf("%d%d",&cash,&N)!=EOF){        int n=0;        int cos,num;        for(int i=0;i<N;i++){            scanf("%d%d",&num,&cos);            for(int j=1;j<=num;j<<=1){                cost[n++]=j*cos;                num-=j;            }            if(num>0){                cost[n++]=num*cos;            }        }        memset(dp,0,sizeof(dp));        for(int i=0;i<n;i++){            for(int j=cash;j>=cost[i];j--){                dp[j]=max( dp[j],dp[ j-cost[i] ]+cost[i] );            }        }        cout<<dp[cash]<<endl;    }    return 0;}



0 0
原创粉丝点击