题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3591
The trouble of Xiaoqian
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1476 Accepted Submission(s): 504
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.
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.
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想买价值为T的东西, 商家和她有价值为V1,V2....VN的硬币, 其中商家每种价值的
硬币有无限多个, 而xiaoqian每种相应有C1,C2...CN个。 问 xiaoqian买价值为T的东西, 所需要的最少的硬币数, 包括商家找回的个数。
思路:只用先对xiaoqian进行分组背包(注意这里需要用到二进制的思维), 再对商家进行完全背包即可。
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <cstdlib>
#include <queue>
#include <iostream>
#include <functional>
#include <cstring>
#define UFOR(i, a, b) for(int i = a; i <= b; i++)
#define DFOR(i, a, b) for(int i = a; i >= b; i--)
#define MEM0(a) memset(a, 0, sizeof(a))
#define MEM1(a) memset(a, -1, sizeof(a))
#define MEMINF(a) memset(a, 0x3f3f3f3f, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
using namespace std;
//typedef __int64 LL;
typedef long long LL;
const int MXN = 2e3 + 10;
const int MXM = 2e4 + 130;
const int HS = 1000007;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
int dp1[MXM], dp2[MXM], a[130], v[MXN], num[MXN];
int main ()
{
int n, T, kase = 1;
while (~scanf("%d %d", &n, &T), n + T) {
MEMINF(dp1);
MEMINF(dp2);
UFOR (i, 1, n) scanf("%d", &a[i]);
int tot = 1;
UFOR (i, 1, n) {
int k;
scanf("%d", &k);
int cnt = 0;
while (k - (1 << cnt) >= 0) {
num[tot] = (1 << cnt);
v[tot++] = a[i];
k -= (1 << cnt);
cnt++;
}
if (k) {
num[tot] = k;
v[tot++] = a[i];
}
}
dp1[0] = 0;
UFOR (i, 1, tot - 1) {
DFOR (j, 20000, v[i] * num[i]) {
dp1[j] = min(dp1[j], dp1[j - num[i] * v[i]] + num[i]);
}
}
// printf("%d\n", dp1[T]); //debug
dp2[0] = 0;
UFOR (i, 1, n) {
UFOR (j, a[i], 20000) { //注意这里也要跑到20000..
dp2[j] = min(dp2[j], dp2[j - a[i]] + 1);
}
}
int ans = INF;
UFOR (i, T, 20000) {
if (dp1[i] < INF) ans = min(ans, dp1[i] + (i > T ? dp2[i - T] : 0));
}
printf("Case %d: %d\n", kase++, ans < INF ? ans : -1);
}
return 0;
}