您的位置:首页 > 其它

POJ 1787 - Charlie's Change(完全背包+路径记录)

2014-10-20 17:52 471 查看
Description

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.


                 

题意:

求出使用最多的硬币数得到的钱数的方案。

思路:

可以转化为完全背包问题,使用一个数组used来记录是否超过数量。

CODE:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;

int a[]={1, 5, 10, 25};
int m[4], dp[10005], used[10005], path[10005], ans[10005];

int main()
{
freopen("in", "r", stdin);
int p;
while(~scanf("%d %d %d %d %d", &p, &m[0], &m[1], &m[2], &m[3])){
if(p == 0 && m[0] == 0 && m[1] == 0 && m[2] == 0 && m[3] == 0) break;
//memset(dp, 0, sizeof(dp));
for(int i = 0; i <= p; ++i) dp[i] = -inf;
memset(path, 0, sizeof(path));
path[0] = -1;
dp[0] = 0;
for(int i = 0;i < 4; ++i){
memset(used, 0, sizeof(used));
for(int j = a[i]; j <= p; ++j){
if(dp[j - a[i]] + 1 > dp[j] && used[j - a[i]] < m[i] && dp[j - a[i]] >= 0){
dp[j] = dp[j - a[i]] + 1;
used[j] = used[j - a[i]] + 1;
path[j] = j - a[i];
}
}
}

memset(ans, 0, sizeof(ans));
if(dp[p] < 0){
printf("Charlie cannot buy coffee.\n");
}
else{
int i = p;
while(1){
if(path[i] == -1) break;
ans[i - path[i]] ++;
i = path[i];
}
printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n", ans[a[0]], ans[a[1]], ans[a[2]], ans[a[3]]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ DP