B - Charlie's Change dp背包路径

原题:
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.

题意:有不同数量的1,5,10,25币值的硬币,用最多的硬币凑成P(所求值)。
思路:很明显这是一道多重背包的问题,但是如果我们只是在求最优解的过程中,记录每种硬币出现的个数,就会出现问题。所以我们要记录每个硬币从哪里来,就是记录路径。此外这个多重背包可以转换为完全背包求解会更容易。
代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int mon[5][2];
int num[10005];
int temp[10005];
int dp[10005];
int sum[30];
int main()
{
    int n;
    mon[0][1]=1,mon[1][1]=5,mon[2][1]=10,mon[3][1]=25; //硬币价值
    while(cin>>n>>mon[0][0]>>mon[1][0]>>mon[2][0]>>mon[3][0]) //硬币数
    {
        if(n==0&&mon[0][0]==0&&mon[1][0]==0&&mon[2][0]==0&&mon[3][0]==0)
            return 0;
        memset(dp,-1,sizeof(dp));  //dp赋值为-1是为了判断是否会出现无法没有足够的零钱的情况
        memset(temp,0,sizeof(temp)); 
        dp[0]=0;
        temp[0]=-1; //起点
        for(int i=0; i<4; ++i)
        {
            memset(num,0,sizeof(num));
            for(int j=mon[i][1]; j<=n; ++j)
            {   //num[j-mon[i][1]]<mon[i][0]的语句判断是为了把多重背包转换为完全背包
                if(dp[j-mon[i][1]]>=0&&dp[j]<=dp[j-mon[i][1]]&&num[j-mon[i][1]]<mon[i][0]){
                dp[j]=dp[j-mon[i][1]]+1;
                num[j]=num[j-mon[i][1]]+1; //临时记录dp值,num作用如上
                temp[j]=j-mon[i][1]; //记录路径
                }

            }
        }
        if(dp[n]==-1)
            cout<<"Charlie cannot buy coffee."<<endl;
        else{
        sum[1]=sum[5]=sum[10]=sum[25]=0;
        while(temp[n]!=-1){       //回溯
        sum[n-temp[n]]++;  
        n=temp[n];
        }
        printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",sum[1],sum[5],sum[10],sum[25]);
        }
    }
    return 0;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值