HDU_3591_(多重背包+完全背包)

Posted Jason333

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU_3591_(多重背包+完全背包)相关的知识,希望对你有一定的参考价值。

The trouble of Xiaoqian

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2006    Accepted Submission(s): 712


Problem Description
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种面值的货币,val[i]为面值,num[i]为数量,要支付t元,求支付货币枚数和找零货币枚数的和最小的货币枚量。售货员每种货币有无数枚,且以最优方式找零。
 
思路:一种货币若val[i]*num[i]>10000,那么令num[i]=10000/val[i];对支付使用多重背包(使用二进制转化为01背包),对找零使用完全背包,答案为min(dp1[i]+dp2[i-t])  (t<=i<=t+maxval) maxval:最大面值。
 
代码:
#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<cstring>
using namespace std;
#define INF 10000000

int numb[105],val[105];
int dp1[20005],dp2[125];
int charge[125];

void zero_one(int *dp,int can,int value,int num)
{
    for(int j=can; j>=value*num; j--)
        dp[j]=min(dp[j],dp[j-value*num]+num);
}

void coml_package(int *dp,int can,int value)
{
    for(int j=value; j<=can; j++)
        dp[j]=min(dp[j],dp[j-value]+1);
}

void multi_package(int *dp,int can,int value,int num)
{
    int tmp=1;
    while(num>=tmp)
    {
        zero_one(dp,can,value,tmp);
        num-=tmp;
        tmp=tmp<<1;
    }
    zero_one(dp,can,value,num);
}

int main()
{
    int n,k,Case=0;
    while(scanf("%d%d",&n,&k)!=EOF&&(n&&k))
    {
        for(int i=0; i<20005; i++)
        {
            dp1[i]=INF;
            if(i<125)
                dp2[i]=INF;
        }
        dp1[0]=0;
        dp2[0]=0;
        int maxval=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&val[i]);
            if(val[i]>maxval)
                maxval=val[i];
        }
        for(int i=0; i<n; i++)
        {
            scanf("%d",&numb[i]);
            if(val[i]*numb[i]>10000)
                numb[i]=10000/val[i];
        }

        for(int i=0; i<n; i++)
            multi_package(dp1,k+maxval,val[i],numb[i]);

        for(int i=0; i<n; i++)
            coml_package(dp2,maxval,val[i]);



        int res=dp1[k],g=k,r=0;
        for(int i=k+1; i<=k+maxval; i++)
          res=min(res,dp1[i]+dp2[i-k]);
if(res>=INF) printf("Case %d: -1\n",++Case); else printf("Case %d: %d\n",++Case,res); } return 0; }

 

以上是关于HDU_3591_(多重背包+完全背包)的主要内容,如果未能解决你的问题,请参考以下文章

背包专题C - The trouble of Xiaoqian hdu3591混合背包:多重背包+完全背包

HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)

HDU 3591 多重背包

动态规划_01背包_完全背包_多重背包_分组背包

HDU_2191_多重背包

hdu 3591 The trouble of Xiaoqian