-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy path12.8.cpp
50 lines (45 loc) · 1.35 KB
/
12.8.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* 题目名称:Piggy-Bank
* 题目来源:HDU 1114
* 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <climits>
using namespace std;
const int INF = INT_MAX / 10;
const int MAXN = 500 + 10;
const int MAXM = 1e4 + 10;
int dp[MAXM];
int value[MAXN]; //物品价值
int weight[MAXN]; //物品重量
int main() {
int caseNumber;
scanf("%d", &caseNumber);
while (caseNumber--) {
int e, f;
scanf("%d%d", &e, &f);
int m = f - e; //背包容量
int n; //物品种类
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &value[i], &weight[i]);
}
for (int i = 1; i <= m; i++) {
dp[i] = INF; //注意初始化
}
dp[0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = weight[i]; j <= m; ++j) {
dp[j] = min(dp[j], dp[j - weight[i]] + value[i]);
}
}
if (dp[m] == INF) {
printf("This is impossible.\n");
} else {
printf("The minimum amount of money in the piggy-bank is %d.\n", dp[m]);
}
}
return 0;
}