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. MYSQL 下一些常用操作命令:新建用户、修改密码、修改登录host等

    1.登录服务器 mysql -u <用户名> -p 2.增加用户,并同时授权操作权限 grant select,insert,update,delete on <数据库>.* ...

  2. linux源码Makefile详解(完整)【转】

    转自:http://www.cnblogs.com/Daniel-G/p/3286614.html 随着 Linux 操作系统的广泛应用,特别是 Linux 在嵌入式领域的发展,越来越多的人开始投身到 ...

  3. rsync的断点续传设置。

    关于rsync的断点续传 关于rsync的断点续传 经常copy大文件,由于服务器.路由等网络的不确定性,老是出现 remote server not responding.于是查了下资料,发现rsy ...

  4. maven手动安装jar到本地仓库

    比如oracle驱动ojdbc5.jar 1,安装MAVEN,并配置系统环境变量 2,将jar文件复制到d: 3,打开cmd窗口,cd到d: 4,执行命令:mvn install:install-fi ...

  5. C#:只运行一个程序

    一.通过系统事件 1.实现如下: using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  6. 20145227 《Java程序设计》实验五实验报告

    20145227 <Java程序设计>实验五实验报告 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验过程 1.先运行TCP代码,一人服务 ...

  7. ACM题目————STL练习之字符串替换

    描述 编写一个程序实现将字符串中的所有"you"替换成"we" 输入 输入包含多行数据 每行数据是一个字符串,长度不超过1000 数据以EOF结束 输出 对于输 ...

  8. C# PDF添加水印

    需要iTextSharp.dll, 下载地址http://sourceforge.net/projects/itextsharp/ using System;using System.Collecti ...

  9. StringBuild,StringFormat," "+" " 三种方法速度测试

    测试方式: Stopwatch sw = new Stopwatch(); sw.Start(); string tmp = ""; StringBuilder sb = new ...

  10. Unity胶囊体的碰撞检测实现

    可选是否打开矩阵变换,支持xyz三种朝向 using UnityEngine; using System.Collections; using System.Collections.Generic; ...