博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Knapsack problem FZU - 2214 ( 01背包 )
阅读量:4046 次
发布时间:2019-05-25

本文共 2214 字,大约阅读时间需要 7 分钟。

 

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                                                                                                                                                                                                      

题意: 

给定一组n个项目,每个项目具有权重w[i]和值v[i],确定一种将项目选择到背包中的方法,使得总重量小于或等于给定极限B,并且总值尽可能大。求最大值。(注意每个项目只能选择一次)。

这个就是01背包的题目,按照常理就是通过重量来找价值,看一个物品是否放到背包中让价值最大。

for(int i = 1; i <= n; i++)

     for(int j = B; j >= w[i]; j--)
          dp[j] = max(dp[j], dp[j - w[i]] + v[i]); 

但是由于这个题目中给出的B范围太大,1 <= B <= 1000000000,所以不能按照这个方法。

那么就可以转换过来,统计所有的物品的价值,通过价值来找重量,当重量小于B并最接近B时取此时的价值。

for(int i = 1; i <= n; i++)

     for(int j = sumv; j >= v[i]; j--)
          dp[j] = min(dp[j], dp[j - v[i]] + w[i]);

然后找出dp数组中小于B并且最接近B的元素的下标,即为最大价值。

代码:

#include
#include
#include
#include
#include
using namespace std; typedef long long ll;const int inf = 0x3f3f3f3f;const int maxn = 1e4 + 5;int v[505], w[505], n, maxv;ll dp[maxn], B; int main(){ int T; scanf("%d", &T); while(T--){ maxv = 0; scanf("%d %d", &n, &B); for(int i = 1; i <= n; i++) { scanf("%d %d", &w[i], &v[i]); maxv += v[i]; //统计出总价值 } for(int i = 1; i <= maxv; i++) dp[i] = 1e17; dp[0] = 0; for(int i = 1; i <= n; i++){ for(int j = maxv; j >= v[i]; j--){ dp[j] = min(dp[j], dp[j - v[i]] + w[i]); } } ll ans = inf; for(int i = maxv; i >= 0; i--){ if(dp[i] <= B) { ans = i; break; } } printf("%lld\n", ans); } return 0;}

 

转载地址:http://yszci.baihongyu.com/

你可能感兴趣的文章
opensuse12.2 PL2303 minicom
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>
计算机的发展史
查看>>
带WiringPi库的交叉编译如何处理一
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Spring事务的七种传播行为
查看>>
ES写入找不到主节点问题排查
查看>>
Java8 HashMap集合解析
查看>>
欢迎使用CSDN-markdown编辑器
查看>>
Android计算器实现源码分析
查看>>
Android系统构架
查看>>
Android 跨应用程序访问窗口知识点总结
查看>>
各种排序算法的分析及java实现
查看>>
SSH框架总结(框架分析+环境搭建+实例源码下载)
查看>>
js弹窗插件
查看>>
自定义 select 下拉框 多选插件
查看>>
js判断数组内是否有重复值
查看>>