Knapsack problem

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).

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.

Output

For each test case, output the maximum value.

Sample Input

1
5 15
12 4
2 2
1 1
4 10
1 2

Sample Output

15

01背包变形。将体积与价值倒着存,将不超出背包体积求最大价值问题转化为不超出总价值求最小体积问题。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <cctype>
#define MAX 5005
using namespace std;
//const int maxn = ;
const int INF = 0x3f3f3f3f;
typedef long long ll; int w[MAX],v[MAX],f[MAX];
int min(int x,int y){
return x<y?x:y;
}
int main(void){
int t,V,W,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&V);
W=;
memset(f,INF,sizeof(f));
for(i=;i<=n;i++){
scanf("%d%d",&w[i],&v[i]);
W+=v[i]; }
f[]=;
for(i=;i<=n;i++){
for(j=W;j>=v[i];j--){
if(f[j-v[i]]+w[i]<=V){
f[j]=min(f[j],f[j-v[i]]+w[i]);
}
}
}
for(i=W;i>=;i--){
if(f[i]<INF){
printf("%d\n",i);
break;
}
}
}
return ;
}

FZU - 2214 Knapsack problem 01背包逆思维的更多相关文章

  1. FZU 2214 Knapsack problem 01背包变形

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

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

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

  3. 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 t ...

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

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

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

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

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

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

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

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

  8. Educational DP Contest E - Knapsack 2 (01背包进阶版)

    题意:有\(n\)个物品,第\(i\)个物品价值\(v_{i}\),体积为\(w_{i}\),你有容量为\(W\)的背包,求能放物品的最大价值. 题解:经典01背包,但是物品的最大体积给到了\(10^ ...

  9. HDU 2955 01背包(思维)

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

随机推荐

  1. 九度OJ 1007:奥运排序问题 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7344 解决:1568 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号从0到N- ...

  2. 我的Java开发学习之旅------>求字符串中出现次数最多的字符串以及出现的次数

    金山公司面试题:一个字符串中可能包含a~z中的多个字符,如有重复,如String data="aavzcadfdsfsdhshgWasdfasdf",求出现次数最多的那个字母及次数 ...

  3. 韩国版微信Kakao推出公链,能否用区块链实现全球扩张?

    2018年是属于区块链的一年,国内外巨头们纷纷布局区块链产业,前不久韩国最大的移动社交平台Kakao集团正式启动了旗下的第一个公链Klaytn的测试网. 此次宣布只是一次试探性举措,旨在对外公布Kla ...

  4. Linux就该这么学--Shell脚本基本应用

    1.接收用户的参数: Shell脚本为了能够让用户更灵活的完成工作需求,可以在执行命令时传递参数:(命令名 参数1 参数2...) Shell预定义变量: $0 当前执行Shell脚本的程序名 $1- ...

  5. fastjson(转)

    博客地址 :http://blog.csdn.NET/shulianghan/article/details/41011605 fastjson 源码地址 : -- GitHub : https:// ...

  6. @Transactional注解不回滚原因详解

    最近试了试spring的回滚功能,根据网上的教程配置怎么都不好使,遂寻找答案, 网上的答案都是这么讲的: 1. 检查你方法是不是public的. 2. 你的异常类型是不是unchecked异常.如果我 ...

  7. Java基础教程:多线程基础(4)——Lock的使用

    Java基础教程:多线程基础(4)——Lock的使用 快速开始 Java 5中Lock对象的也能实现同步的效果,而且在使用上更加方便. 本节重点的2个知识点是:ReentrantLock类的使用和Re ...

  8. 微信小程序上传项目到github

    Step1:点击使用版本控制 点击开发工具右上角的版本控制按钮 Step2:点击初始化本地仓库: 等待初始化结束 Step3:在github新建仓库 Step4: 配置git用户密码或ssh: Ste ...

  9. web 全栈 学习 2 一个好的页面是如何炼成的

    第一章:Web页面内容的构成2.Web内容的分工一个Web页面可能的构成(视觉上看):①文字.链接.标题②交互入口(表单元素)③图片(哪些类型)④动画 Flash动画 HTML5 CSS3 动画⑤音视 ...

  10. 3 《锋利的jQuery》jQuery中的DOM操作

    DOM操作分(1)DOM Core(核心):document.geElementsByTagName("form");/ element.getAttribute("sr ...