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 1054:字符串内排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7949 解决:4343 题目描述: 输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串. 输入: 测试数据有多组,输 ...

  2. 【题解】[Ghd]

    [题解]Ghd 一道概率非酋题? 题目很有意思,要我们选出大于\(\frac{n}{2}\)个数字使得他们的最大公约数最大. 那么我们若随便选择一个数字,他在答案的集合里的概率就大于\(0.5\)了. ...

  3. cordova 插件创建

    peng@PENG-PC /E/_My_File_____/_work/MyCode/myCode/cordova-workspace/plugman-test/ABCD $ npm install ...

  4. uboot 从sd卡加载文件并烧写到nand flash

    uboot下可以从用tftp和nfs加载文件. 但是现在有个开发板配套uboot网络功能出现异常,执行ping命令就会导致开发板重启,只能选择先从sd卡加载文件 启动开发板,任意键进入uboot,然后 ...

  5. CodeChef - ANDMIN —— 线段树 (结点最多被修改的次数)

    题目链接:https://vjudge.net/problem/CodeChef-ANDMIN Read problems statements in Mandarin Chinese, Russia ...

  6. 练习E-R图书管理数据库

  7. spring与jdbc整合

    spring+jdbc开发,我使用的是c3p0连接池 1.数据库建表: create table person( id int primary key auto_increment, name var ...

  8. 【thrift】vc中使用thrift中文字符串乱码问题解决

    问题描述: VC中使用Apache thrift时,如果字符串中包含中文,会出现乱码问题,这个问题的原因是由于thrift为了达到跨语言交互而使用了UTF-8格式发送字符串,这点对java或者C#不会 ...

  9. inode、软连接、硬链接

    一.inode是什么? 理解inode,要从文件储存说起.文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB).操作系统读取 ...

  10. MFC模态对话框程序不响应OnIdle

    从代码分析原因吧: OnIdle函数在MFC的CWinThread::Run函数中被调用,如下 // main running routine until thread exits int CWinT ...