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
对于xiaoqian 是一个多重背包,对于店家是完全背包,如果v[i] * c[i] >= 20000,xiaoqian是完全背包,否则看做01背包,真正的上限是20000而不是T
wa了四发因为初始化的问题,开始把dp1和dp2的数组初始化为0x3f,一直wa,直到改为了0x3f3f3f3f,之前一直是随便找一个就去初始化的,现在才知道0x3f3f3f3f才是无穷大,果然还是太渣了
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int MX = 2e4 + 5;
int n, t, sum, v[MX], c[MX], dp1[MX], dp2[MX];
void complete_pack(int v)
{
for(int i = v; i <= 20000; i++)
{
dp2[i] = min(dp2[i], dp2[i - v] + 1);
}
}
void zeroone_pack(int k, int v)
{
v = k * v;
for(int i = 20000; i >= v; i--)
{
dp2[i] = min(dp2[i], dp2[i - v] + k);
}
}
void pack(int v, int c)
{
if(v * c >= 20000)
{
complete_pack(v);
return ;
}
int k = 1;
while(k < c)
{
zeroone_pack(k, v);
c -= k;
k *= 2;
}
zeroone_pack(c, v);
}
int main()
{
int Case = 1;
while(scanf("%d%d", &n, &t) != EOF && n != 0 && t != 0){
memset(dp1, 0x3f3f3f3f, sizeof(dp1));
memset(dp2, 0x3f3f3f3f, sizeof(dp2));
dp1[0] = 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++)
{
for(int j = v[i]; j <= 20000; j++){
dp1[j] = min(dp1[j], dp1[j - v[i]] + 1);
}
}
for(int i = 1; i <= n; i++)
{
pack(v[i], c[i]);
}
int ans = 0x3f3f3f3f;
for(int i = t; i <= 20000; i++)
{
ans = min(ans, dp1[i - t] + dp2[i]);
}
printf("Case %d: ", Case++);
if(ans == 0x3f3f3f3f)
cout<<-1<<endl;
else
cout<<ans<<endl;
}
return 0;
}