POJ1787 Charlie's Change(多重背包+输出方案)

探讨了Charlie如何使用尽可能多的硬币支付咖啡价格的问题,通过动态规划算法解决硬币组合问题,确保硬币总数最多且恰好支付所需金额。

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

Problem Link:http://poj.org/problem?id=1787

 

                                                                                      Charlie's Change

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5185 Accepted: 1640

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.

 

 

AC code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
int P,c[5],dp[10010],f[10010][5];
int cent[5]={0,1,5,10,25};
//dp[j]表示花费j元所得到的硬币的最大个数
//f[j][k]表示“花费j元得到硬币最大个数 ”这种方案里面第K种硬币的个数
int main()
{
//	freopen("D:\\in.txt","r",stdin);
	int i,j,k;
	while(cin>>P>>c[1]>>c[2]>>c[3]>>c[4])
	{
		if(!P&&!c[1]&&!c[2]&&!c[3]&&!c[4]) break;
		//因为题目要求要全部花完,所以属于
		//“要求恰好装满的求最大值的背包问题”,故初始化为以下代码:
		for(i=1;i<=P;i++) 
            dp[i]=-INF;//注意:除了0和1以外,其他初始化不要用memset,包括0x3f3f3f3f 
        dp[0]=0;
		memset(f,0,sizeof(f)); 
		for(i=1;i<=4;i++)
		{
			if(c[i]*cent[i]>=P)//完全背包 
			{
				for(j=cent[i];j<=P;j++)
				{
					//注意理解这里是关键,只有当当前第i种硬币需要取时,状态才会更新,否则: 
					//dp[i][j]=dp[i-1][j]  f[i][j][k]=f[i-1][j][k],去掉第一维后,即: 
					// dp[j]=dp[j]  f[j][k]=f[j][k]
					//还要注意需要取第i种硬币的条件是花费j元取第i种硬币比不取得到的硬币个数多, 
					//即dp[j]<dp[j-cent[i]]+1,同时为了优化执行效率,可以把不合法的状态去掉,使得只对 
					//满足花费j-cent[i]元这个状态是存在的(即存在某种最优方案(即硬币个数最多),
					//使得前i-1种硬币恰好能够组成j-cent[i]元。
		            //(即恰好能够装满容量为j-cent[i]的背包,此时dp[j-cent[i]]!=-INF))  
					//注意:之所以加上后面那个条件是为了优化,不然TLE。。(这题卡时间效率得很紧) 
					if(dp[j]<dp[j-cent[i]]+1 && dp[j-cent[i]]!=-INF )
					{
						dp[j]=dp[j-cent[i]]+1;
						for(k=1;k<=4;k++)
						{
							if(k==i) f[j][k]=f[j-cent[i]][k]+1;
							else f[j][k]=f[j-cent[i]][k];
						}
					}
				}
			}
			else//二进制优化的01背包  
			{
				int cnt=1,num=c[i];
				while(cnt<=num)
				{
					for(j=P;j>=cent[i]*cnt;j--)
					{
						if(dp[j]<dp[j-cent[i]*cnt]+cnt && dp[j-cent[i]*cnt]!=-INF )
						{
							dp[j]=dp[j-cent[i]*cnt]+cnt;
							for(k=1;k<=4;k++)
							{
								if(k==i) f[j][k]=f[j-cent[i]*cnt][k]+cnt;
								else f[j][k]=f[j-cent[i]*cnt][k];
							}
						}
					}
					num-=cnt;
					cnt*=2;
				}
				for(j=P;j>=cent[i]*num;j--)
				{
					if(dp[j]<dp[j-cent[i]*num]+num && dp[j-cent[i]*num]!=-INF)
					{
						dp[j]=dp[j-cent[i]*num]+num;
						for(k=1;k<=4;k++)
						{
							if(k==i) f[j][k]=f[j-cent[i]*num][k]+num;
							else f[j][k]=f[j-cent[i]*num][k];
						}
					}
				}
			}
		}
		if(dp[P]>0) 
		printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",f[P][1],f[P][2],f[P][3],f[P][4]);
        else printf("Charlie cannot buy coffee.\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值