FZU 2214 ——Knapsack problem——————【01背包的超大背包】
Accept: 6 Submit: 9
Time Limit: 3000 mSec Memory Limit : 32768 KB
Problem 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).
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
Sample Output
Source
第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 550;
const int INF = 0x3f3f3f3f;
int w[maxn], v[maxn];
int dp[5500];
int main(){
int T, n, B;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&B);
int V = 0;
for(int i = 1; i <= n; i++){
scanf("%d%d",&w[i],&v[i]);
V += v[i];
}
memset(dp,INF,sizeof(dp));
dp[0] = 0;
for(int i = 1; i <= n; i++){
for(int j = V; j >= v[i]; j--){
dp[j] = min(dp[j],dp[j-v[i]]+w[i]);
// printf("%d ",dp[j]);
}
}
int ans = 0;
for(int i = V; i >= 0; i--){
if(dp[i] <= B){
ans = i; break;
}
}
printf("%d\n",ans);
}
return 0;
}
FZU 2214 ——Knapsack problem——————【01背包的超大背包】的更多相关文章
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- 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 ...
- 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 ...
- FOJProblem 2214 Knapsack problem(01背包+变性思维)
http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4 Submit: 6Time Limit: 3000 mSec Memory Lim ...
- Problem 2214 Knapsack problem 福建第六届省赛
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2214 题目大意:给你T组数据,每组有n个物品,一个背包容量B,每件有体积和价值.问你这个背包容纳的物品最大价值 ...
- FZU Problem 2214 Knapsack problem(背包+思维转换)
转化思维,把价值当成背包容量,选择最小的花费,从上到下枚举,找到当这个最小的花费. #include<iostream> #include<cstring> #include& ...
- FZU 2214 Knapsack dp (转化背包)
就是一个背包裸题,由于物品的重量太大,开不了这么大的数组 所以转化一下,由于价值总和不大于5000,所以把价值看作重量,重量看作价值,那么就是同样的价值下,求一个最轻的重量 #include<c ...
- FZU-2214 Knapsack problem(DP使用)
Problem 2214 Knapsack problem Accept: 863 Submit: 3347Time Limit: 3000 mSec Memory Limit : 327 ...
- (01背包 当容量特别大的时候) Knapsack problem (fzu 2214)
http://acm.fzu.edu.cn/problem.php?pid=2214 Problem Description Given a set of n items, each with a ...
随机推荐
- vs2015 使用 Eazfuscator.NET 3.3
出现问题: Unable to cast object System.Xml.XmlComment’ to type ‘System.Xml.XmlElement’ 解决办法: 打开 *.csproj ...
- fwrite()
注:fwrite(),fread -可对数据块读写,且数据为二进制,文本下查看为乱码,文件的打开方式为 “b*” 实例: 写入二进制数据 for (int i = 0; i < SN; i++) ...
- SpringMVC 入门程序
SpringMVC是什么 SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架,属于 Spring FrameWork 的后续产品,已经融合在 Spr ...
- 利用keytool工具生成数字证书
一.制作数字证书 因测试微信小程序, 腾讯要求使用 https协议,所以需要使用证书.使用jdk工具制作数字证书流程如下: 1.查看JDK是否安装,使用命令java -version 2.切换目录至 ...
- MySQL学习笔记(一):查询
查询实例: 1.创建数据库并使用: create database school; use school; 2.创建表并插入内容: create table student( Sno char(9) ...
- cf555e
cf555e(缩点) 给一个 n 个点 m 条边的图,以及 q 对点 (s,t),让你给 m 条边定向.问是否存在一种方案,使每对点的 s 能走到 t. \(n,m,q≤ 2×10^5\). 首先,在 ...
- VS2010 UAC执行级别修改
配置属性 -> 链接器 -> 清单文件 -> UAC执行级别 改为 requireAdministrator 这个级别即可.
- [JSOI2009]计数问题 二维树状数组BZOJ 1452
题目描述 一个n*m的方格,初始时每个格子有一个整数权值.接下来每次有2种操作: 改变一个格子的权值: 求一个子矩阵中某种特定权值出现的个数. 输入输出格式 输入格式: 第一行有两个数N,M. 接下来 ...
- audio.play dom对象 JQ不支持play
*/ PausePlayVoice:function() { $("#spPauseAudio").click(function() { ...
- python学习之路---day23--模块
模块基本小结if __name__ == '__main__':一:import 引入模块模块:是一个包含python定义和声明的文件,文件名就是模块名字加上.py后缀,所有的py文件都可以看成是一个 ...