hdu 3591 The trouble of Xiaoqian( 多重背包+完全背包)

The trouble of Xiaoqian

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


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
 


让使用钱数最少,用多重背包解决买家出的钱数最少,完全背包解决卖家找钱钱数最少,最后再枚举一下买家所有出钱的可能就可以了。

代码:

//93ms
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=20000+100;
const int inf=99999999;
int dp[maxn];
int dp2[maxn];
struct node
{
    int v;
    int c;
}edge[1500];
int v[150];
int c[150];
int main()
{
    int n,m,ca=1;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        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<maxn;i++)
        {
            dp[i]=inf;
            dp2[i]=inf;
        }
        dp[0]=dp2[0]=0;
        int t=0;
        for(int i=1;i<=n;i++)//二进制优化,edge[i].v为物品所占的容量,edge[i].c为物品价值,就是有几个钱币
        {
            for(int k=0;(1<<k)<=c[i];k++)
            {
                c[i]-=(1<<k);
                edge[t].v=v[i]*(1<<k);
                edge[t++].c=(1<<k);
            }
            edge[t].v=v[i]*c[i];
            edge[t++].c=c[i];
        }
        for(int i=0;i<t;i++)//多重背包解决买家出的钱数最少
        {
            for(int j=maxn-1;j>=edge[i].v;j--)
            {
                dp[j]=min(dp[j],dp[j-edge[i].v]+edge[i].c);
            }
        }
        for(int i=1;i<=n;i++)//完全背包解决找钱最少
        {
            for(int j=v[i];j<maxn;j++)
            {
                dp2[j]=min(dp2[j],dp2[j-v[i]]+1);
            }
        }
        int ans=inf;
        //printf("%d %d\n",dp[75],dp2[5]);
        for(int i=m;i<maxn;i++)//枚举卖家所有可能出钱数
        {
            if(dp[i]!=inf)
            {
                ans=min(ans,dp[i]+dp2[i-m]);
            }
        }
        printf("Case %d: ",ca++);
        if(ans!=inf)
        printf("%d\n",ans);
        else
        printf("-1\n");
    }
    return 0;
}


你可能感兴趣的:(Algorithm,背包)