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 }
时间: 2024-08-03 11:13:45

FZU 2214 Knapsack problem(背包问题)的相关文章

FZU 2214 ——Knapsack problem——————【01背包的超大背包】

2214 Knapsack problem Accept: 6    Submit: 9Time Limit: 3000 mSec    Memory Limit : 32768 KB  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

FZU 2214 Knapsack problem (01背包)

题意:给你n种物品,每种只有一个,第i种物品的价值为Vi,重量为Wi,把这些物品放入一个重量限制为B的背包中,使得背包内的物品在重量不超过B的前提下,价值尽量大,输出最大价值 1 <= n <= 500 1 <= B, w[i] <= 1000000000 1 <= v[1]+v[2]+...+v[n] <= 5000 思路:我们从范围中可以看到这个物品的重量和背包所能承受的重量特别大,我们不能使用常规的01背包,我们可以把问题转化一下,我们定义d(i,j)是把第i个物

knapsack problem 背包问题 贪婪算法GA

knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按价值密度从大到小依次放入包内直到放不下,设此时放了s个物品 2.将所得价值与最大价值()所比较,取最大的作为输出 贪婪算法与最优解竞争比(近似比)为 证明:

FZU 2214 Knapsack dp (转化背包)

就是一个背包裸题,由于物品的重量太大,开不了这么大的数组 所以转化一下,由于价值总和不大于5000,所以把价值看作重量,重量看作价值,那么就是同样的价值下,求一个最轻的重量 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cstdlib> #include<cmath> #include<cstdlib>

(01背包 当容量特别大的时候) Knapsack problem (fzu 2214)

http://acm.fzu.edu.cn/problem.php?pid=2214 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

2018.3.5-6 knapsack problem, sequence alignment and optimal binary search trees

这周继续dynamic programming,这三个算法都是dynamic programming的. knapsack problem有一种greedy的解法,虽然简单但是不保证正确,这里光头哥讲的是dynamic的解法.其实和上次那个max weight independent set的算法差不多,同样是每个物件都判断一遍有这个物件和没这个物件两种情况,用bottom-up的方法来解,然后得到一个最大的value值,这时因为没有得到具体的选择方案,所以最后还需要一部重构的步骤得到具体方案.

0-1背包问题(0-1 knapsack problem)

0-1背包问题描述:一个正在抢劫商店的小偷发现了n个商品,第i个商品价值 vi 美元,重 wi 磅,vi 和 wi 都是整数.这个小偷希望拿走价值尽量高的商品,但他的背包最多能容纳 S 磅重的商品,S 是一个整数,那么他应该如何拿才能使得背包中的商品价值之和最大. 0-1背包问题的特点在于这类问题只能做出二元选择,比如上面描述的问题中每个商品不可拆分,小偷要么把它拿走,要么把它留下:不能拿走商品的一部分.所以有可能最后结果小偷的背包还有多余的空间,但却不能再多放商店的商品了.这也是使用动态规划求

0-1背包问题(the knapsack problem)

---恢复内容开始--- 关键原理:动态规划. tab[i][j] = max(tab[i-1][j-weight[i]]+value[i],tab[i-1][j]) ({i,j|0<i<=n,0<=j<=total}) i表示放第i个物品,j表示背包所容纳的重量,那么tab[i-1][j-weight[i]]+value[i]表示放入第i物品. 流程: 状态转移方程:

FZU 1753-Another Easy Problem(求多个组合数的最大公约数)

Another Easy Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 1753 Appoint description:  xietutu  (2013-03-13)System Crawler  (2015-04-27) Description 小TT最近学习了高斯消元法解方程组,现在他的问题来了,如果是以下的方程,