POJ1276--DP, multiple backpack cash machine
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 28826 |
|
Accepted: 10310 |
Description
A Bank plans to install a machine for cash withdrawal. The machine was 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 have a supp Ly 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 ten 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 so can be effectively delivered according for the available bill supply of the machine.
Notes:
@ is the symbol of the currency delivered by the machine. For instance, @ could stand for dollar, euro, pound etc.
Input
The program input was from standard input. Each data set in the input stands for a particular transaction and have the format:
Cash N N1 D1 n2 D2 ... nN DN
where 0 <= cash <= 100000 is the amount of cash requested, 0 <=n <= are the number of Bill denominations and 0 <= nk <= are the number of available bills for the DK denomination, 1 <= dk <=, k=1,n. White space s can occur freely between the numbers in the input. The input data is 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 Belo W.
Sample Input
735 3 4 6 5 3 350633 4 ten 6 1 5 0 1735 3
Sample Output
73563000
Test instructions: To a number of denominations of banknotes, the first I banknotes have n[i] Zhang, each face value of d[i], asked to piece together not more than cash the maximum face value is how much
Idea: It is obvious that this is a multi-pack (the number of each note is limited), binary optimization and conversion to 01 Knapsack solution, if a note can be pieced together to the face value of more than cash, then as the number is unlimited, as a complete backpack processing, the reverse is limited, using binary optimization as 01 backpack processing
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespacestd;Const intmaxn=1000100;intCash,n;intN[MAXN],D[MAXN];intDP[MAXN];voidZeroonepack (intWeightintval) { for(inti=cash;i>=weight;i--) {Dp[i]=max (dp[i],dp[i-weight]+val); }}voidCompletepack (intWeightintval) { for(inti=weight;i<=cash;i++) {Dp[i]=max (dp[i],dp[i-weight]+val); }}voidMultipack (intWeightintValintamount) { if(Weight*amount>=cash) Completepack (Weight,val);//if the limit is exceeded, when the full backpack is processed Else{//Otherwise, binary optimized and converted to 01 backpack processing intk=1; while(K<amount) {//Press k=1,2,4,... The sequence of decomposition amount, after the decomposition of the part by 01 backpack processingZeroonepack (k*weight,k*val); Amount-=K; K*=2; } zeroonepack (Amount*weight,amount*val);//The rest is also handled by 01 backpacks . }}intMain () { while(cin>>cash>>N) {memset (DP,0,sizeof(DP)); for(intI=1; i<=n;i++) {cin>>n[i]>>D[i]; Multipack (D[i],d[i],n[i]); } cout<<dp[cash]<<Endl; } return 0;}
View Code
POJ1276--DP, multiple backpacks