POJ 1787 Charlie's Change

Charlie's Change

Time Limit: 1000ms
Memory Limit: 30000KB
This problem will be judged on PKU. Original ID: 1787
64-bit integer IO format: %lld      Java class name: Main
Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task. 

Your program will be given numbers and types of coins Charlie has and the coffee price. The coffee vending machines accept coins of values 1, 5, 10, and 25 cents. The program should output which coins Charlie has to use paying the coffee so that he uses as many coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly. 
 

Input

Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the line P, 1 <= P <= 10 000, is the coffee price in cents. Next four integers, C1, C2, C3, C4, 0 <= Ci <= 10 000, are the numbers of cents, nickels (5 cents), dimes (10 cents), and quarters (25 cents) in Charlie's valet. The last line of the input contains five zeros and no output should be generated for it.
 

Output

For each situation, your program should output one line containing the string "Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where T1, T2, T3, T4 are the numbers of coins of appropriate values Charlie should use to pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output "Charlie cannot buy coffee.".
 

Sample Input

12 5 3 1 2
16 0 0 0 1
0 0 0 0 0

Sample Output

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.
Charlie cannot buy coffee.

Source

解题:多重背包!由于完全背包是从前往后更新的,因此不再是01背包那样,每次逆序一次遍历,至多选一个,
 
从前往后走,有可能会选多个,所以要限制个数,只有满足个数在范围内才跟更新
 
 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 int val[5] = {1,5,10,25},cnt[5],dp[10010],path[10010],p;
 5 int sel[10010];
 6 int main() {
 7     while(~scanf("%d",&p)) {
 8         bool flag = p;
 9         for(int i = 0; i < 4; ++i) {
10             scanf("%d",cnt+i);
11             flag = flag || cnt[i];
12         }
13         if(!flag) break;
14         memset(dp,-1,sizeof dp);
15         memset(path,0,sizeof path);
16         path[0] = -1;
17         for(int i = dp[0] = 0; i < 4; ++i){
18             memset(sel,0,sizeof sel);
19             for(int j = val[i]; j <= p; ++j)
20                 if(dp[j - val[i]] > -1 && dp[j - val[i]] >= dp[j] && sel[j - val[i]] < cnt[i]){
21                     sel[j] = sel[j - val[i]] + 1;
22                     path[j] = j - val[i];
23                     dp[j] = dp[j-val[i]] + 1;
24                 }
25         }
26         if(dp[p] < 0) printf("Charlie cannot buy coffee.
");
27         else{
28             int ans[110] = {0};
29             while(~path[p]){
30                 ans[p - path[p]]++;
31                 p = path[p];
32             }
33             printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.
",ans[val[0]],ans[val[1]],ans[val[2]],ans[val[3]]);
34         }
35     }
36     return 0;
37 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4425057.html