zoj2156 Charlie's Change 完全背包+路径记录

来源:互联网 发布:手绘头像软件 编辑:程序博客网 时间:2024/05/20 11:32

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 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, 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 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 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.

给定一个数p,要求用四种币值为1,5,10,25的硬币拼成p,并且硬币数要最多,如果无解输出”Charlie cannot buy coffee.”,1<=p<=10000,1<=硬币数量<=10000..直接dp,不过这里要记录路径。。。

#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<map>#include<stack>#pragma comment(linker,"/STACK:102400000,102400000")#define pi acos(-1.0)#define EPS 1e-6#define INF (1<<24)using namespace std;int main(){    int coin[]={1,5,10,25};    int  money;    int a[100];    int dp[10005];    int cnt[10005];    int path[10005];    while(scanf("%d %d %d %d %d",&money,&a[0],&a[1],&a[2],&a[3])==5)    {        if(money==0&&a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0) break;        memset(dp,-1,sizeof(dp));        dp[0]=0;        int i,j;        for(i=0;i<4;i++)        {            memset(cnt,0,sizeof(cnt));            for(j=coin[i];j<=money;j++)            {                if(dp[j]<dp[j-coin[i]]+1&&dp[j-coin[i]]!=-1&&cnt[j-coin[i]]<a[i])                {                    dp[j]=dp[j-coin[i]]+1;                    path[j]=j-coin[i];                    cnt[j]=cnt[j-coin[i]]+1;                }            }        }        if(dp[money]==-1)        {            printf("Charlie cannot buy coffee.\n");        }        else        {            int numbers[50];            memset(numbers,0,sizeof(numbers));            while(dp[money]!=0)            {                numbers[money-path[money]]++;                money=path[money];            }            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",numbers[1],numbers[5],numbers[10],numbers[25]);        }    }    return 0;}
0 0
原创粉丝点击