2214 Knapsack problem

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

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

 Sample Output

15

 Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

 
 
题目大意:给你T组数据,每组有n个物品,一个背包容量B,每件有体积和价值。问你这个背包容纳的物品最大价值是多少。每个物品只能放入一次背包。
 
解题思路:首先这个很清楚看出来是个01背包。但是这个背包容量特别大,同时物品的体积很大,总的价值却很少。寻常意义上dp[i]表示背包容量为i时的最大价值,那么我们把这个dp[]数组的含义改变一下,dp[i]表示装价值为i时所需的最小容量。
 
#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背包的超大背包】的更多相关文章

  1. FZU 2214 Knapsack problem 01背包变形

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

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

  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. FOJProblem 2214 Knapsack problem(01背包+变性思维)

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

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

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

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

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

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

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

  8. FZU-2214 Knapsack problem(DP使用)

    Problem 2214 Knapsack problem Accept: 863    Submit: 3347Time Limit: 3000 mSec    Memory Limit : 327 ...

  9. (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 ...

随机推荐

  1. .net core in Docker 部署方案(随笔)

    前一段时间由于项目需要 .net core 在docker下的部署,途中也遇到很多坑,看了各同行的博客觉得多多少少还是有些问题,原本不想写此篇文章,由于好友最近公司也需要部署,硬是要求,于是花了些时间 ...

  2. 关于函数传参的其他问题(const形参实参/可变形参)

    const 形参和实参 当形参是 const 变量时,实参是 const 或者不是 const 变量都可以. 实参初始化形参时会忽略掉顶层 const: void gel(const int a){ ...

  3. Python之路Python全局变量与局部变量、函数多层嵌套、函数递归

    Python之路Python全局变量与局部变量.函数多层嵌套.函数递归 一.局部变量与全局变量 1.在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量.全局变量作用域是整个程序,局 ...

  4. 1. UML统一建模语言

    (1)UML概述: 建模: 对现实系统进行适当的过滤, 用适当的表现规则描述出简洁的模型. 建模是一种深入解决问题的方法. UML: UML(United Modeling Language, 统一建 ...

  5. hadoop集群配置方法---mapreduce应用:xml解析+wordcount详解---yarn配置项解析

    注:以下链接均为近期hadoop集群搭建及mapreduce应用开发查找到的资料.使用hadoop2.6.0,其中hadoop集群配置过程下面的文章都有部分参考. hadoop集群配置方法: ---- ...

  6. CentOS71611部署Django

    web.conf <VirtualHost *:> WSGIScriptAlias / /var/www/datacn/datacn/wsgi.py Alias /static/ /var ...

  7. P3615 如厕计划

    $ \color{#0066ff}{ 题目描述 }$ 竞赛比完之后,水箱里充满水的选手们鱼贯而出.凡华中学的厕所规划的很糟,只有两个厕位,于是厕所门前排起了长长的队伍. 厕所有两个,一个是女生专用厕所 ...

  8. 【离散数学】 SDUT OJ 1.1联结词真值运算

    1.1联结词真值运算 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 已知命题变元p和 ...

  9. docker 安装 redis

    docker拉去镜像以及配置生成容器的步骤几乎和之前的nginx安装一样,直接写下面的命令了 1. docker pull redis 2. docker run -p 6379:6379 -v /U ...

  10. JavaWeb学习笔记(十二)—— JDBC的基本使用

    一.JDBC概述 1.1 数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡,同样道 ...