FZU 2214 Knapsack problem(背包问题)

Description

题目描述

Given a set of n items, each with a weight w[i] and a value v[i], determine a way to choose the items into a knapsack so that the total weight is less than or equal to a given limit B and the total value is as large as possible. Find the maximum total value. (Note that each item can be only chosen once).

给你n件物品,以及每件物品的质量w[i]和价值v[i]。选择一种装包方式使得背包的最终质量小等于上限B并且最终价值尽可能大。找出最大的总价值。(注意,每件物品只能被选择一次)

 

Input

输入

The first line contains the integer T indicating to the number of test cases.

For each test case, the first line contains the integers n and B.

Following n lines provide the information of each item.

The i-th line contains the weight w[i] and the value v[i] of the i-th item respectively.

1 <= number of test cases <= 100

1 <= n <= 500

1 <= B, w[i] <= 1000000000

1 <= v[1]+v[2]+...+v[n] <= 5000

All the inputs are integers.

输入的首行是一个整数T表示测试样例的数量。

对于每个测试样例,第一行包含两个整数n和B。 

接下来有n行表示每件物品的信息。

第i行分别包含第i件物品的质量w[i]与价值v[i]。

1 <= 测试样例数量 <= 100

1 <= n <= 500

1 <= B, w[i] <= 1000000000

1 <= v[1]+v[2]+...+v[n] <= 5000

输入均为整数。

 

Output

输出

For each test case, output the maximum value.

每个测试样例输出其最大价值。

 

Sample Input - 输入样例

Sample Output - 输出样例

1

5 15

12 4

2 2

1 1

4 10

1 2

15

 

【题解】

最大质量为1000000000,数组肯定不够用。

不过,总价值才5000,我们以价值为轴开辟记录剩余可载质量的一维数组,后面的做法就与01背包如出一辙。

【代码 C++】

 1 #include<cstdio>
 2 #include<cstring>
 3 int main(){
 4     int weight[5001], t, i, j, n, B, max_value, w, v;
 5     scanf("%d", &t);
 6 
 7     while (t--){
 8         scanf("%d%d", &n, &B);
 9         memset(weight, 0, sizeof(weight));
10         weight[0] = B, max_value = 0;
11 
12         for (j = 0; j < n; ++j){
13             scanf("%d%d", &w, &v);
14             for (i = max_value; i >= 0; --i){
15                 if (weight[i] - w > weight[i + v]) weight[i + v] = weight[i] - w;
16             }
17             for (i = max_value + 1; i <= 5000; ++i) if (weight[i]) max_value = i;
18         }
19 
20         printf("%d\n", max_value);
21     }
22     return 0;
23 }

 

你可能感兴趣的:(FZU 2214 Knapsack problem(背包问题))