现在的位置: 首页 > 综合 > 正文

oj1787Charlie’s Change(多重背包+记录路径,每个包恰好被填满的基础上每个包的钱币的个数尽量多)

2018年02月22日 ⁄ 综合 ⁄ 共 2648字 ⁄ 字号 评论关闭

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.
意题:每一行为一组,第一个数为coffee的价格,后跟4个数,表示每种钱的数量。钱类型为1分,5分,10分,25分。用这些数量的钱去卖一个coffee,不能多也不能少。如果有多种可以卖的方案就输出用了钱币数量最多的一组方案,输出每种钱的数量。
解题:记录一下路径,具看代码。
#include<stdio.h>
struct nnn
{
    int tp,id,k,sumk;//分别代表当前钱的类型,前个点的位置,当前钱的类型的数量,钱总数量
}dp[10005];
int V;
void zeroonepack(int w,int tp,int k)
{
    for(int v=V;v>=w;v--)
    if(dp[v].sumk<dp[v-w].sumk+k&&dp[v-w].sumk)//如果dp[v-w].sumk!=0,说明包v-w 用了sumk个钱币恰好被填满
    {
        dp[v].sumk=dp[v-w].sumk+k;//填满用的总钱币数
        dp[v].id=v-w;//前一位置
        dp[v].k=k; //当前加的钱币数量
        dp[v].tp=tp;//当前加的钱币类型
    }
}
void complexepack(int w,int tp)
{
    for(int v=w;v<=V;v++)
    if(dp[v].sumk<dp[v-w].sumk+1&&dp[v-w].sumk)
    {
        dp[v].sumk=dp[v-w].sumk+1;
        dp[v].id=v-w;
        dp[v].k=1; dp[v].tp=tp;
    }
}
void mulitpack(int use,int n,int tp)
{
    if(use>V)return ;
    if(use*n>=V)
    complexepack(use,tp);
    else
    {
        int k=1;
        while(k<n)
        {
            zeroonepack(k*use,tp,k);
            n-=k; k*=2;
        }
        zeroonepack(n*use,tp,n);
    }
}
int main()
{
    int typ[6]={0,1,5,10,25},flog,k[6];
    while(1)
    {
        scanf("%d",&V); flog=0;
        for(int i=1;i<=4;i++)
        {
            scanf("%d",&k[i]); 
            flog+=k[i]*typ[i];
        }
        if(flog+V==0) break;
        if(flog<V)
        {
             printf("Charlie cannot buy coffee.\n");
        }
        else
        {
            for(int i=0;i<=V;i++)
            {
               dp[i].sumk=0;
               dp[i].id=-1;
            }
            dp[0].sumk=1;
            for(int i=1;i<=4;i++)
            {
                mulitpack(typ[i],k[i],i);
            }
            if(dp[V].sumk)
            {
                 for(int i=0;i<=4;i++)
                 k[i]=0;
                 int v=V;
                 while(v!=-1)//回溯路径
                 {
                     k[dp[v].tp]+=dp[v].k;
                     v=dp[v].id;
                 }
    printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",k[1],k[2],k[3],k[4]);
            }
            else
            printf("Charlie cannot buy coffee.\n");
        }
    }
}

抱歉!评论已关闭.