POJ 1787 Change 多重|完全背包

Charlie's Change
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3895   Accepted: 1183

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.

Source

CTU Open 2003

点击打开题目链接

有四种硬币,面值为1,5,10,25,求组成钱 P 能用的最多的硬币数量。

方法一:多重背包+记录路径,多重背包入门:多重背包入门链接

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int INF = 0x7fffffff;
const int MAXN = 10;
const int MAXP = 1e4 + 10;
int dp[MAXP], ans[MAXP], sum[MAXP], path[MAXP];
int  value[MAXN] = {1, 5, 10, 25};
int P;

void zeroOnePack(int i, int k)      ///01背包,取k个第i种硬币
{
    for (int j = P; j >= k * value[i]; j--)
    {
        if (dp[j - k * value[i]] + k > dp[j])
        {
            dp[j] = dp[j - k * value[i]] + k;
            path[j] = j - value[i];
        }
    }
}

void completePack(int i)        ///完全背包,第i种硬币
{
    for (int j = value[i]; j <= P; j++)
        if (dp[j - value[i]] + 1 > dp[j])
        {
            dp[j] = dp[j - value[i]] + 1;
            path[j] = j - value[i];
        }
}

void multiPack()            ///多重背包
{
    dp[0] = 0;
    for (int i = 1; i <= P; i++) dp[i] = -INF;

    for (int i = 0; i < 4; i++)
    {
        if (sum[i] == 0) continue;      ///没有这种硬币
        if (value[i] > P) return;       ///这种硬币的价值大于P,就不再取了
        if (value[i] * sum[i] >= P)      ///只取第i种硬币可以组成P
            completePack(i);        ///转化为完全背包
        else
        {
            for (int k = 1; k <= sum[i]; k *= 2) ///转化为01背包,系数为1,2,4...2^(k-1)
            {
                zeroOnePack(i, k);
                sum[i] -= k;
            }
            if (sum[i] != 0)    ///最后剩余的硬币数(sum[i]-2^k+1)
                zeroOnePack(i, sum[i]);
        }
    }
}

int main()
{
    while (~scanf("%d", &P))
    {
        for (int i = 0; i < 4; i++) scanf("%d", &sum[i]);
        if (P == 0) break;
        multiPack();

        if (dp[P] > 0)        ///能组成P钱
        {
            memset(ans, 0, sizeof(ans));
            while (P != 0)      ///根据路径计算用到的各种硬币的个数
            {
                ans[P - path[P]]++;
                P = path[P];
            }
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",
                   ans[1], ans[5], ans[10], ans[25]);
        }
        else
            printf("Charlie cannot buy coffee.\n");
    }
    return 0;
}

方法二:完全背包变形,这里的完全背包每个物品的数量受到了限制

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int INF = 0x7fffffff;
const int MAXN = 10;
const int MAXP = 1e4 + 10;
int dp[MAXP], ans[MAXP], num[MAXP], path[MAXP];
int sum[MAXN], v[MAXN] = {1, 5, 10, 25};

int main()
{
    int P;
    while (~scanf("%d", &P))
    {
        for (int i = 0; i < 4; i++) scanf("%d", &sum[i]);
        if (P == 0) break;

        dp[0] = 0;
        for (int i = 1; i <= P; i++) dp[i] = -INF;
        for (int i = 0; i < 4; i++)
        {
            memset(num, 0, sizeof(num));    ///一般完全背包的硬币数量没有限制,不需要dp[j-v[i]]!=-1
            for (int j = v[i]; j <= P; j++) ///这里用的完全背包硬币数受到了限制,导致有的数无法组成
                if (dp[j - v[i]] != -1 && dp[j] < dp[j - v[i]] + 1 && num[j - v[i]] < sum[i])
                {
                    ///要把这些无法组成的数排除
                    dp[j] = dp[j - v[i]] + 1;
                    num[j] = num[j - v[i]] + 1;     ///又用了一个第i种硬币
                    path[j] = j - v[i];             ///记录路径
                }
        }

        if (dp[P] > 0)        ///能组成P钱
        {
            memset(ans, 0, sizeof(ans));
            while (P != 0)      ///根据路径计算用到的各种硬币的个数
            {
                ans[P - path[P]]++;
                P = path[P];
            }
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",
                   ans[1], ans[5], ans[10], ans[25]);
        }
        else
            printf("Charlie cannot buy coffee.\n");
    }
    return 0;
}


你可能感兴趣的:(dp,ACM)