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. Double Check Locking 双检查锁机制

    方法保证了多线程并发下的线程安全性.这里在声明变量时使用了volatile关键字来保证其线程间的可见性:在同步代码块中使用二次检查,以保证其不被重复实例化.集合其二者,这种实现方式既保证了其高效性,也 ...

  2. 爬虫-Selenium -抱错ElementNotVisibleException: Message: element not visible

    1.当使用Selenium IDE 完成了脚本的录制和回放通过后,想要将脚本转换为其他语言如java.Python等,首次使用时打开Options->Format发现没有可以转换的语言,如下: ...

  3. 04-树7 二叉搜索树的操作集(30 point(s)) 【Tree】

    04-树7 二叉搜索树的操作集(30 point(s)) 本题要求实现给定二叉搜索树的5种常用操作. 函数接口定义: BinTree Insert( BinTree BST, ElementType ...

  4. PAT 甲级 1104. Sum of Number Segments (20) 【数学】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1104 思路 最容易想到的一个思路就是 遍历一下所有组合 加一遍 但 时间复杂度 太大 会超时 ...

  5. AiCloud 2.0 AT开发文档【转】

    本文转载自:http://wiki.ai-thinker.com/aicloud/docs/atdevelop AT指令一览表 AiCloud AT指令   指令 描述 AT+CLDSTART 启动云 ...

  6. Buffer的数据存取

    缓冲区 存放要读取的数据 缓冲区 和 通道 配合使用 一个用于特定基本数据类行的容器.有java.nio包定义的,所有缓冲区都是抽象类Buffer的子类. Java NIO中的Buffer主要用于与N ...

  7. haproxy 修改 访问路径

    # 匹配 jsessionid,并去除 jessionid参数reqrep ^([^\ :]*)\ /a/test.html;jsessionid=.*\?(.*) \1\ /b/test.html? ...

  8. (5)表单Action后台验证

    /day31/src/cn/itcast/web/struts2/user/UserAction.java package cn.itcast.web.struts2.user; import com ...

  9. Elasticsearch mapping文档相似性算法

    Elasticsearch allows you to configure a scoring algorithm or similarity per field. The similarityset ...

  10. 2018.3.1 RF module distance test part II-

    1 Test  circuit diagram 2  Test demo 3 Test record 4  Test  analysis 5 Test results and discussion E ...