多重背包记录方案

题目:

http://poj.org/problem?id=1787

  1. 列表内容

R - 多重背包记录方案
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.

//这道题目主要在于记录每一次的最佳数据。
//其他的就是简单的多重背包问题了,
//也许你们喜欢写另外的形式的但是
//我是比较喜欢写这种形式的
//不仅节省空间,而且非常容易理解
//所以我比较推荐大家写这种格式的
//G++条件下可以过,C++条件竟然不能过,有些出乎我的意料。所以之后我好好改进的
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int INF=10000000;



struct state {
    int v,id;
    state(int vv,int idd) {
        v=vv;
        id=idd;
    }
};


struct sta {
    int c[5],v;
    void init() {
        for(int i=0; i<5; i++) {
            c[i]=0;
        }
        v=-INF;
    }
} dp[10000+5];



vector<state>sizes;
vector<state>value;
int p,c[5];
const int money[]= {0,1,5,10,25};
int cur;


void sh() {/这是将众多的物品拆分,至于原理大家可以百度搜一下,便能够知道
    for(int i=1; i<=4; i++) {
        for(int j=1; j<=c[i]; j<<=1) {
            sizes.push_back(state(j,i));
            value.push_back(state(j*money[i],i));
            c[i]-=j;
        }
        if(c[i]>0) {
            sizes.push_back(state(c[i],i));
            value.push_back(state(c[i]*money[i],i));
        }
    }
}/


void init(int m) {
    for(int i=0; i<=m; i++) {
        dp[i].init();
    }
    dp[0].v=0;
}
/技巧提示:如果要填满某个容器的话那么,dp[0]=0,代表着0容量的为零。dp[1.....v]=-INF,代表着这些在最开始的时候都是不存在的
//也就是没有填满,不满足条件,(也就是此时初始状态的时候始终达不到使dp[1.....v]填满的要求,所以他们是-INF)


int main() {
    while(~scanf("%d%d%d%d%d",&p,&c[1],&c[2],&c[3],&c[4]),p||c[1]||c[2]||c[3]||c[4]) {
        sizes.clear();
        value.clear();
        init(p);
        sh();
        for(int i=0; i<value.size(); i++) {
            for(int j=p; j>=value[i].v; j--) {
                //printf("<%d,%d,%d>\n",dp[j].v,dp[j-value[i].v].v,sizes[i].v);
                if(dp[j].v<dp[j-value[i].v].v+sizes[i].v) {
                    dp[j].v=dp[j-value[i].v].v+sizes[i].v;
                    for(int k=0; k<5; k++) {
                        dp[j].c[k]=dp[j-value[i].v].c[k];
                    }
                    dp[j].c[sizes[i].id]=dp[j-value[i].v].c[sizes[i].id]+sizes[i].v;
                }
            }
        }
        if(dp[p].v<0) {
            printf("Charlie cannot buy coffee.\n");
        } else {
           // printf("[%d]\n",dp[p].v);
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",dp[p].c[1],dp[p].c[2],dp[p].c[3],dp[p].c[4]);
        }
    }


    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值