POJ_1787_Charlie'sChange(多重背包&&记录路径)

来源:互联网 发布:js动态显示隐藏div 编辑:程序博客网 时间:2024/05/16 08:51

Charlie's Change
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 4094 Accepted: 1269

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 216 0 0 0 10 0 0 0 0

Sample Output

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.Charlie cannot buy coffee.

Source

CTU Open 2003


题意

有4中硬币(1,5,10,25)给出数量凑p价值

使用尽量多的硬币

给出方案


题解

就是一个需要记录路径的多重背包问题

多开一个4维数组记录每种状态下最多的四种硬币数量即可

特别的这个题如果初始全-INF的话中间不加特判会TLE


#include <iostream>#include <stdio.h>#include <string.h>using namespace std;const int MV=1e4+5;const int MN=10;int dp[MV],step[MV][5];int v[5]={0,1,5,10,25};int c[5];int n,vm;//物品数空间void _zop(int vi,int wi,int k)//01背包子过程{    for(int i=vm;i>=vi;i--)    {        if(dp[i-vi]>=0&&dp[i]<dp[i-vi]+wi)        {            dp[i]=dp[i-vi]+wi;            step[i][k]=step[i-vi][k]+vi/v[k];            for(int j=k-1;j>=1;j--)                step[i][j]=step[i-vi][j];        }    }}void _cp(int vi,int wi,int k)//完全背包子过程{    for(int i=vi;i<=vm;i++)    {        if(dp[i-vi]>=0&&dp[i]<dp[i-vi]+wi)        {            dp[i]=dp[i-vi]+wi;            step[i][k]=step[i-vi][k]+vi/v[k];            for(int j=k-1;j>=1;j--)                step[i][j]=step[i-vi][j];        }    }}void _mulp(int vi,int wi,int ni,int kk)//多重背包子过程{    if(vi*ni>=vm)    {        _cp(vi,wi,kk);        return;    }    int k=1;    while(k<ni)    {        _zop(vi*k,wi*k,kk);        ni-=k;        k<<=1;    }    _zop(vi*ni,wi*ni,kk);}int main(){    int p;    //freopen("1.in","r",stdin);    while(1)    {        scanf("%d%d%d%d%d",&p,&c[1],&c[2],&c[3],&c[4]);        if(!p&&!c[1]&&!c[2]&&!c[3]&&!c[4])            break;        vm=p;        memset(dp,0x80,(p+5)*sizeof(int));        dp[0]=0;        memset(step,0,(p+5)*5*sizeof(int));        for(int i=1;i<=4;i++)        {            _mulp(v[i],1,c[i],i);//            cout<<"dp"<<i<<endl;//            for(int j=0;j<=p;j++)//                cout<<dp[i]<<" ";//            cout<<endl;//            cout<<"step"<<i<<endl;//            for(int j=0;j<=p;j++)//                cout<<step[j][i]<<" ";//            cout<<endl;        }        if(dp[p]<0)            printf("Charlie cannot buy coffee.\n");        else        {//            int nc4=step[p][4];//            int nc3=step[p-nc4*25][3];//            int nc2=step[p-nc4*25-nc3*10][2];//            int nc1=step[p-nc4*25-nc3*10-nc2*5][1];            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",step[p][1],step[p][2],step[p][3],step[p][4]);        }    }    return 0;}


0 0