zoj 2156 Charlie's Change(多重背包+倍增优化+记录路径)

本文深入探讨了Charlie'sChange问题,该问题属于多重背包类型,涉及到使用最少硬币支付咖啡费用的策略。文章详细阐述了解决方法,包括输入输出规范、问题背景、解题思路以及代码实现,为读者提供了全面的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Charlie's Change

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 Specification

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,  C 1C 2C 3C 4, 0  <=  C i  <= 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 Specification

For each situation, your program should output one line containing the string " Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where  T 1T 2T 3T 4 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: Czech Technical University Open 2003

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2156

分析:这题还是付钱的问题,做法依旧是多重背包,比较不一样的是这题要求出背包的具体装法,也就是记录一下转移路径就行了

代码:

#include<cstdio>
using namespace std;
const int mm=11111;
int v[]= {1,5,10,25};
int f[mm],p[mm],t[mm],c[4],ans[4];
int i,j,m;
void CompletePack(int v,int k)
{
    for(int i=v;i<=m;++i)
        if(f[i-v]>=0&&f[i]<=f[i-v])
        {
            f[i]=f[i-v]+1;
            p[i]=i-v;
            t[i]=k;
        }
}
void ZeroOnePack(int v,int d,int k)
{
    for(int i=m;i>=v;--i)
        if(f[i-v]>=0&&f[i]<f[i-v]+d)
        {
            f[i]=f[i-v]+d;
            p[i]=i-v;
            t[i]=k;
        }
}
int main()
{
    while(scanf("%d%d%d%d%d",&m,&c[0],&c[1],&c[2],&c[3]),m+c[0]+c[1]+c[2]+c[3])
    {
        for(i=0; i<=m; ++i)f[i]=-1000000000;
        f[0]=0;
        for(i=0; i<4; ++i)
            if(c[i])
            {
                if(c[i]*v[i]>=m)CompletePack(v[i],i);
                else
                {
                    j=1;
                    while(j<c[i])
                    {
                        ZeroOnePack(v[i]*j,j,i);
                        c[i]-=j;
                        j<<=1;
                    }
                    ZeroOnePack(v[i]*c[i],c[i],i);
                }
            }
        if(f[m]>=0)
        {
            for(i=0; i<4; ++i)ans[i]=0;
            while(m)
            {
                ans[t[m]]+=(m-p[m])/v[t[m]];
                m=p[m];
            }
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",ans[0],ans[1],ans[2],ans[3]);
        }
        else puts("Charlie cannot buy coffee.");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值