poj 1787(多重背包)

来源:互联网 发布:神经网络算法入门书籍 编辑:程序博客网 时间:2024/05/01 23:37

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.

多重背包问题,可以理解为01背包,但在转化过程中如果没有降低包的个数的话就会超时到哭。。。。所以采用二进制转化法。

<pre name="code" class="cpp">/**************************************************采用系数化方法降低时间复杂度,首先将多重背包看成01背包且n[i]个背包可以将其带上系数分解可以降低分解的个数,最终降低时间复杂度。***************************************************/#include <iostream>#include<algorithm>#include<cmath>#include<cstring>#include<cstdio>using namespace std;struct pay{    int num;    int cent[4];    pay()    {        cent[0] = 0; cent[1] = 0; cent[2] = 0; cent[3] = 0; num = 0;    }};int mon(int i){    switch (i)    {        case 0: return 1;        case 1: return 5;        case 2: return 10;        case 3: return 25;    }}int main(){    int money[4],pri;    while ( cin >> pri >>money[0] >> money[1] >> money[2] >> money[3] )    {        if ( pri == 0 ) break;        pay ways[10005];        int vis[10005] = {0};        vis[0] = 1;        for ( int i = 0; i < 4; i++ )        {            int l = 1;            while ( money[i] - l > 0)            {                for ( int k = pri - l * mon(i); k >= 0; k-- )                {                    if ( vis[k] )                    {                        if ( ways[k + l * mon(i)].num >= ways[k].num + l );                        else                        {                            ways[k + l * mon(i)].num = ways[k].num + l;                            ways[k + l * mon(i)].cent[0] = ways[k].cent[0];                            ways[k + l * mon(i)].cent[1] = ways[k].cent[1];                            ways[k + l * mon(i)].cent[2] = ways[k].cent[2];                            ways[k + l * mon(i)].cent[3] = ways[k].cent[3];                            ways[k + l * mon(i)].cent[i] += l;                        }                        vis[ k + l*mon( i )] = 1;                    }                }                money[i] -= l;                l *= 2;///采用二进制方法降低时间复杂度,                    ///在化为01背包的同时减少背包的个数,并且保证每个数字都可以取上            }            l = money[i];            for ( int k = pri - l * mon(i); k >= 0; k-- )            {                if ( vis[k] )                {                    if ( ways[k + l * mon(i)].num >= ways[k].num + l );                    else                    {                        ways[k + l * mon(i)].num = ways[k].num + l;                        ways[k + l * mon(i)].cent[0] = ways[k].cent[0];                        ways[k + l * mon(i)].cent[1] = ways[k].cent[1];                        ways[k + l * mon(i)].cent[2] = ways[k].cent[2];                        ways[k + l * mon(i)].cent[3] = ways[k].cent[3];                        ways[k + l * mon(i)].cent[i] += l;                    }                    vis[ k + l*mon( i )] = 1;                }            }        }        if ( vis[pri] )///如果访问过的话就说明可以支付            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",                   ways[pri].cent[0],ways[pri].cent[1],ways[pri].cent[2],ways[pri].cent[3]);        else            printf("Charlie cannot buy coffee.\n");    }}





0 0
原创粉丝点击