FZU Problem 2214 Knapsack problem(01背包,超大背包)——第六届福建省大学生程序设计竞赛-重现赛
2016-09-08 15:33
507 查看
此文章可以使用目录功能哟↑(点击上方[+])
Accept: 0 Submit: 0
Time Limit: 3000 mSec Memory Limit : 32768 KB
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).
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
<= 5000
All the inputs are integers.
For each test case, output the maximum value.
1
5 15
12 4
2 2
1 1
4 10
1 2
15
解题思路:
【题意】
有n件商品,和一个容量为B的背包
已知这n件商品每件的重量和价值
现要从中选取一些商品
问在不超过背包容量的前提下,所能获得的最大价值为多少
【类型】
01背包,超大背包
【分析】
很显然,这是一道01背包的动规题
但是,仔细看一下数据范围之后,发现又不是那么纯粹的01背包
我们知道,对于01背包的题,在用一维数组实现求解的时候,时间复杂度为O(n×B),空间复杂度为O(B)
常规做法DP部分代码如下:
for(i=0;i<n;i++)
for(j=B;j>=w[i];j--)
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
而此题B的大小范围为1 ≤ B ≤ 1000000000
由此可见,无法直接使用01背包,因为背包的容量过大
那要怎么来做呢?
继续看题,发现1 ≤ v[1]+v[2]+...+v
≤ 5000
奇怪,平时遇到01背包的题,都是不限定价值范围的(≤10^9这种除外)
而此题给了这个限制,肯定,问题的关键在此
思考一下,可以发现这样一种解法
将价值之和看作是背包容量,这样此题的做法就是
尽可能塞满背包的情况下,使得商品重量之和尽可能小
那么在满足重量之和小于B的条件下,价值之和最大的即为此题的解
DP部分代码如下:
for(i=0;i<n;i++)
for(j=sum;j>=v[i];j--)
dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
【时间复杂度&&优化】
![](https://oscdn.geek-share.com/Uploads/Images/Content/201609/8ba9a1a4c1046b9d454cd1bfb1cc9248)
题目链接→FZU Problem 2214 Knapsack problem
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/1dda3a3f6f8e9f3a85d8388576e35a50.gif)
FZU Problem 2214 Knapsack problem
Accept: 0 Submit: 0Time Limit: 3000 mSec Memory Limit : 32768 KB
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
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).
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
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
<= 5000
All the inputs are integers.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Output
For each test case, output the maximum value.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Sample Input
15 15
12 4
2 2
1 1
4 10
1 2
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Sample Output
15
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Hint
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Problem Idea
解题思路:【题意】
有n件商品,和一个容量为B的背包
已知这n件商品每件的重量和价值
现要从中选取一些商品
问在不超过背包容量的前提下,所能获得的最大价值为多少
【类型】
01背包,超大背包
【分析】
很显然,这是一道01背包的动规题
但是,仔细看一下数据范围之后,发现又不是那么纯粹的01背包
我们知道,对于01背包的题,在用一维数组实现求解的时候,时间复杂度为O(n×B),空间复杂度为O(B)
常规做法DP部分代码如下:
for(i=0;i<n;i++)
for(j=B;j>=w[i];j--)
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
而此题B的大小范围为1 ≤ B ≤ 1000000000
由此可见,无法直接使用01背包,因为背包的容量过大
那要怎么来做呢?
继续看题,发现1 ≤ v[1]+v[2]+...+v
≤ 5000
奇怪,平时遇到01背包的题,都是不限定价值范围的(≤10^9这种除外)
而此题给了这个限制,肯定,问题的关键在此
思考一下,可以发现这样一种解法
将价值之和看作是背包容量,这样此题的做法就是
尽可能塞满背包的情况下,使得商品重量之和尽可能小
那么在满足重量之和小于B的条件下,价值之和最大的即为此题的解
DP部分代码如下:
for(i=0;i<n;i++)
for(j=sum;j>=v[i];j--)
dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
【时间复杂度&&优化】
题目链接→FZU Problem 2214 Knapsack problem
![](https://oscdn.geek-share.com/Uploads/Images/Content/201608/f53e4c7ee906d105e2ffb766b96ce96a.gif)
Source Code
/*Sherlock and Watson and Adler*/ #pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<stack> #include<math.h> #include<vector> #include<map> #include<set> #include<bitset> #include<cmath> #include<complex> #include<string> #include<algorithm> #include<iostream> #define eps 1e-9 #define LL long long #define PI acos(-1.0) #define bitnum(a) __builtin_popcount(a) using namespace std; const int N = 505; const int M = 5005; const int inf = 1000000007; const int mod = 1000003; int w ,v ,dp[M]; int main() { int t,n,B,i,j,sum; scanf("%d",&t); while(t--) { sum=0; scanf("%d%d",&n,&B); for(i=0;i<n;i++) { scanf("%d%d",&w[i],&v[i]); sum+=v[i]; } for(i=0;i<=sum;i++) dp[i]=inf; dp[0]=0; for(i=0;i<n;i++) for(j=sum;j>=v[i];j--) dp[j]=min(dp[j],dp[j-v[i]]+w[i]); for(i=sum;i>=0;i--) if(dp[i]<=B) break; printf("%d\n",i); } return 0; }菜鸟成长记
相关文章推荐
- 第六届福建省大学生程序设计竞赛-重现赛,Problem C Knapsack problem【大背包】
- 第六届福建省大学生程序设计竞赛-重现赛,Problem J RunningMan
- Common Tangents【第六届福建省大学生程序设计竞赛-重现赛】
- RunningMan【第六届福建省大学生程序设计竞赛-重现赛】
- FZU Problem 2221 RunningMan(思维考查)——第六届福建省大学生程序设计竞赛-重现赛
- 第六届福建省大学生程序设计竞赛 Problem C Knapsack problem【背包问题】
- 第六届福建省大学生程序设计竞赛-重现赛
- FZU Problem 2213 Common Tangents(简单几何,分类讨论)——第六届福建省大学生程序设计竞赛-重现赛
- FZU Problem 2212 Super Mobile Charger(贪心,排序)——第六届福建省大学生程序设计竞赛-重现赛
- FZU Problem 2216 The Longest Straight(二分答案+树状数组)——第六届福建省大学生程序设计竞赛-重现赛
- FOJ 2214 Knapsack problem 第六届福建省大学生程序设计竞赛 C 01背包DP变种
- 第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)Problem A Super Mobile Charger
- 第六届福建省大学生程序设计竞赛-重现赛,Problem B Common Tangents【数学几何】
- 第六届福建省大学生程序设计竞赛(几道水题)
- 第六届福建省大学生程序设计竞赛 Problem A Super Mobile Charger【模拟】
- 第六届福建省大学生程序设计竞赛 Problem B Common Tangents【几何】
- 2017年浙江中医药大学大学生程序设计竞赛(重现赛)D - CC的神奇背包
- 第六届福建省大学生程序设计竞赛
- 第六届福建省大学生程序设计竞赛(FZU2212—FZU2221)
- 【HDU5932 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 K】【树上背包 贪心乱搞】Backpack on Tree 物品成本只有12345下的树上背包