hdu 3591 良好的多重背包

hdu 3591 很好的多重背包

The trouble of Xiaoqian

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


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
 

Author
alpc97
思路:先对人进行多重背包,然后对售货员进行完全背包 最后求和

/*
题意:
有N种coin,给出每种coin的价值Vi,和小强拥有的个数Ci,小强去购物,要付m元,求小强和店员间交换货币时的最小个数货币,即小强付出coin个数加上店员找回coin个数。
*/
#include<iostream>
using namespace std;
#define maxn  110
int v[maxn],c[maxn],dp[20010],dp2[20010];
int main()
{
 int n,m,i,k,j,ans,cas=0;
    while (scanf("%d%d",&n,&m) != EOF)
    {
		if(!m&&!n) break;
        for (i = 1;i <= n;i++)
           scanf("%d",&v[i]);
        for (i = 1;i <= n;i++)
            scanf("%d",&c[i]);
       for(i=0;i<=20005;i++)  dp[i]=0x3f7f7f7f;// dp[i]=999999999; 不知道为什么用这个就错 一直错了n边
	 //	memset(dp,63,sizeof(dp));     这个也行 但是为什么是63  初始化是随机的 对应为一个10位数        
        dp[0] = 0;
        for (i = 1;i <= n;i++)
            for (k = 1;k<=c[i];k*=2)
            {
                if (c[i] < k)
                    k = c[i];
                int S = k * v[i];
                for (j = 20000;j >= S;j--)
                    dp[j] = dp[j]<(dp[j-S] + k)?dp[j]:(dp[j-S] + k);
					c[i] -= k;
            }
       for(i=0;i<=20005;i++)  dp2[i]=0x3f7f7f7f;
		   //dp2[i]=99999;
		// 	memset(dp2,63,sizeof(dp2));
        dp2[0] = 0;
        for (i = 1;i <= n;i++)
            for (j = v[i];j <= 20000;j++)
                dp2[j] = dp2[j]<(dp2[j-v[i]]+1)?dp2[j]:(dp2[j-v[i]]+1);
			 ans =0x3f3f3f3f;
        for (i = m;i<=20000;i++)
		//	 for(i=20000;i>=m;i--)
            ans = ans<(dp[i]+dp2[i-m])?ans:(dp[i]+dp2[i-m]);
        if (ans == 0x3f3f3f3f)
            ans = -1;
        printf("Case %d: %d\n",++cas,ans);
    }
	return 0;
}