【HDU - 3591 The trouble of Xiaoqian 】 混合背包

E - The trouble of Xiaoqian


In the country of ALPC , Xiaoqian is a very famous mathematician. She is immersed in calculate, and she want to use the minimum number of coins in every shopping. (The numbers of the shopping include the coins she gave the store and the store backed to her.) 
And now , Xiaoqian wants to buy T (1 ≤ T ≤ 10,000) cents of supplies. The currency system has N (1 ≤ N ≤ 100) different coins, with values V1, V2, ..., VN (1 ≤ Vi ≤ 120). Xiaoqian is carrying C1 coins of value V1, C2 coins of value V2, ...., and CN coins of value VN (0 ≤ Ci ≤ 10,000). The shopkeeper has an unlimited supply of all the coins, and always makes change in the most efficient manner .But Xiaoqian is a low-pitched girl , she wouldn’t like giving out more than 20000 once. 
Input
There are several test cases in the input. 
Line 1: Two space-separated integers: N and T. 
Line 2: N space-separated integers, respectively V1, V2, ..., VN coins (V1, ...VN) 
Line 3: N space-separated integers, respectively C1, C2, ..., CN 
The end of the input is a double 0. 
Output
Output one line for each test case like this ”Case X: Y” : X presents the Xth test case and Y presents the minimum number of coins . If it is impossible to pay and receive exact change, output -1.
Sample Input
3 70
5 25 50
5 2 1
0 0
Sample Output
Case 1: 3


题意:有N中规格的钱币,需要购买价值T的东西,如果没有价值恰好的钱币支付,店铺会找给你相应规格的钱币,给出每种钱币的价值和数量,求购买东西所需要经过几次钱币的转移。


分析:当做一个背包问题来做,不要被题目的T迷惑,背包上限是题中所给的20000。对给钱做混合背包处理,对找钱做多重背包处理,分别用两个数组dp1[i], dp2[i]记录钱币的转移次数。最后用dp1和dp2之间的联系找出答案。


代码如下:

#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mod 835672545
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MX = 2e4 + 5;
const int N = 105;
int n, t;
int v[N], c[N];
int dp1[MX], dp2[MX];
void zeroOnePack(int v, int k){
    for(int i = MX-5; i >= v; i--){
        dp1[i] = min(dp1[i], dp1[i-v] + k);
    }
}
void completePack(int v){
    for(int i = v; i <= MX-5; i++){
        dp1[i] = min(dp1[i], dp1[i-v] + 1);
    }
}
void mutiPack(int v, int w){
    if(v * w >= MX-5){
        completePack(v);
        return ;
    }
    int k = 1;
    while(k < w){
        zeroOnePack(v*k, k);
        w -= k;
        k <<= 1;
    }
    zeroOnePack(v*w, w);
}
int main(){
    int cas = 1;
    while(~scanf("%d%d", &n, &t), n&&t){
        memset(dp1, INF, sizeof(dp1));
        memset(dp2, INF, sizeof(dp2));
        dp1[0] = dp2[0] = 0;
        for(int i = 1; i <= n; i++){
            scanf("%d", &v[i]);
        }
        for(int i = 1; i <= n; i++){
            scanf("%d", &c[i]);
        }
        for(int i = 1; i <= n; i++){             //给钱
            mutiPack(v[i], c[i]);
        }
        for(int i = 1; i <= n; i++){             //找钱
            for(int j = v[i]; j <= MX-5; j++){
                dp2[j] = min(dp2[j], dp2[j-v[i]] + 1);
            }
        }
        int ans = INF;
        for(int i = t; i <= MX-5; i++){
            ans = min(ans, dp1[i] + dp2[i-t]);
        }
        printf("Case %d: %d\n", cas++, ans == INF ? -1 : ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值