zoukankan      html  css  js  c++  java
  • 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 ",++Case); else printf("Case %d: %d ",++Case,res); } return 0; }
  • 相关阅读:
    hdu 3579 Hello Kiki (中国剩余定理)
    hdu 1573 X问题 (非互质的中国剩余定理)
    poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
    查看cache中消耗性能的语句
    sqlserver2008 R2中查找未使用过的索引
    【转】如何新建索引:高开销的缺失索引
    查看sqlserver 2008中性能低下的语句
    经典
    符合条件的记录有则修改没有则添加的小优化技巧
    测试分区表,以及在现有表上新建分区方案,以及把分区表变为普通表测试代码!!
  • 原文地址:https://www.cnblogs.com/jasonlixuetao/p/6386575.html
Copyright © 2011-2022 走看看