Cash Machine
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 26954 |
|
Accepted: 9533 |
Main topic:There are a variety of currencies with different denominations. Each denomination has a different number of currencies, please find out the amount of the nearest and less equal to the given number cash that can be used to make the money. multiple backpack turn 0 1 backpackFor the first currency the possible status is w[I] to cashState equation dp[j]=dp[j-c[i]]+w[i] (c[i] means "volume" is actually value equal to w[i] easy to distinguish) use count[i] to record the number of uses of the currency in the n[i] note before each entry into the inner loop (that is, the count is cleared before the currency change 0)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int Main () { int cash,num,i,j; int n[15]; int w[15]; int dp[10001]; int count[10001]; while (Cin>>cash>>num) {for (i=0;i<num;i++) cin>>n[i]>>w[i]; memset (dp,0,sizeof dp); for (i=0;i<num;i++) { memset (count,0,sizeof count); for (j=w[i];j<=cash;j++) if (Dp[j]<dp[j-w[i]]+w[i]&&count[j-w[i]]<n[i]) { dp[j]= Dp[j-w[i]]+w[i]; count[j]=count[j-w[i]]+1; } } cout<<dp[cash]<<endl; } return 0;}
POJ 1276 Cash Machine (multiple backpack)