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++】

 #include<cstdio>
#include<cstring>
int main(){
int weight[], t, i, j, n, B, max_value, w, v;
scanf("%d", &t); while (t--){
scanf("%d%d", &n, &B);
memset(weight, , sizeof(weight));
weight[] = B, max_value = ; for (j = ; j < n; ++j){
scanf("%d%d", &w, &v);
for (i = max_value; i >= ; --i){
if (weight[i] - w > weight[i + v]) weight[i + v] = weight[i] - w;
}
for (i = max_value + ; i <= ; ++i) if (weight[i]) max_value = i;
} printf("%d\n", max_value);
}
return ;
}

FZU 2214

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

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

    2214 Knapsack problem Accept: 6    Submit: 9Time Limit: 3000 mSec    Memory Limit : 32768 KB  Proble ...

  2. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  3. FZU - 2214 Knapsack problem 01背包逆思维

    Knapsack problem Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...

  4. knapsack problem 背包问题 贪婪算法GA

    knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...

  5. FOJProblem 2214 Knapsack problem(01背包+变性思维)

    http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4    Submit: 6Time Limit: 3000 mSec    Memory Lim ...

  6. Problem 2214 Knapsack problem 福建第六届省赛

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214 题目大意:给你T组数据,每组有n个物品,一个背包容量B,每件有体积和价值.问你这个背包容纳的物品最大价值 ...

  7. FZU Problem 2214 Knapsack problem(背包+思维转换)

    转化思维,把价值当成背包容量,选择最小的花费,从上到下枚举,找到当这个最小的花费. #include<iostream> #include<cstring> #include& ...

  8. FZU 2214 Knapsack dp (转化背包)

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

  9. FZU-2214 Knapsack problem(DP使用)

    Problem 2214 Knapsack problem Accept: 863    Submit: 3347Time Limit: 3000 mSec    Memory Limit : 327 ...

随机推荐

  1. linux网站目录及Apache权限的设置

    apache服务器访问权限设置禁止所有访问:Options Indexes FollowSymLinks 改为 Option None   Apache单个或多个目录禁止访问方法   这种方法通常用来 ...

  2. JavaWeb开发实例---Servlet

    1.页面转发:form表单的action属性值为Servlet类再web.xml中配置的URL. 2.重定向:sendRedirect()  只是 简单的页面跳转 转发:request.getRequ ...

  3. E2PROM与Flash的引脚图

    E2PROM(24C02):

  4. win32sdk 编程整理的些资料

    #win32sdk编程积累经验# ## ListView ## - 创建imagelist HIMAGELIST hi; HBITMAP hBmp = LoadBitmap(hInst,MAKEINT ...

  5. ASP.NET MVC Model验证总结【转】

    一.启用客户端验证: 客户端验证主要是为了提高用户体验,在网页不回刷的情况下完成验证. 第一步是要在web.config里启用客户端验证,这在MVC3自带的模板项目中已经有了: <add key ...

  6. Content Delivery Network

    Coding Standards & Best Practices 7 Reasons to use a Content Delivery Network CDN公共库汇总

  7. 30、springmvc

    第一章回顾JavaWeb中的MVC设计模式 1)MVC这种设计模式,不光运用于Web领域,而且也能用于非Web领域 2)今天说的MVC特指一种表现层设计模式,不限于Java语言 第二章回顾struts ...

  8. JavaEE基础(十九)/异常和File

    1.异常(异常的概述和分类) A:异常的概述 异常就是Java程序在运行过程中出现的错误. B:异常的分类 通过API查看Throwable Error 服务器宕机,数据库崩溃等 Exception ...

  9. 使用radioGroup的时候,每个radioButton的状态选择器要使用 state_checked=""属性,不能使用selected

    使用radioGroup的时候,每个radioButton的状态选择器要使用 state_checked=""属性,不能使用selected

  10. oracle nvl和nvl2的区别

    一直用oracle nvl函数,最近发现还有一个nvl2函数: nvl(a,b) 如果a不为null 则返回a,如果a为null则返回b; nvl2(a,b,c) ,如果a不为null 则返回b,如果 ...