当前位置:网站首页>Multiple backpacks!
Multiple backpacks!
2022-07-23 14:58:00 【ATTACH_ Fine】
Yes N Items And a Capacity of V The backpack . The first i There are at most Mi Pieces available , The space consumed by each piece is Ci , The value is Wi . Find out which items can be loaded into the backpack to make the space consumed by these items The total does not exceed the capacity of the backpack , And The sum of value is the greatest .

It makes no difference , This turns into a 01 It's a knapsack problem , And only once per item .
Code
public void testMultiPack1(){
// Version of a : Change the number of items to 01 Knapsack format
List<Integer> weight = new ArrayList<>(Arrays.asList(1, 3, 4));
List<Integer> value = new ArrayList<>(Arrays.asList(15, 20, 30));
List<Integer> nums = new ArrayList<>(Arrays.asList(2, 3, 2));
int bagWeight = 10;
for (int i = 0; i < nums.size(); i++) {
while (nums.get(i) > 1) {
// Expand the item into i
weight.add(weight.get(i));
value.add(value.get(i));
nums.set(i, nums.get(i) - 1);
}
}
int[] dp = new int[bagWeight + 1];
for(int i = 0; i < weight.size(); i++) {
// Traverse the items
for(int j = bagWeight; j >= weight.get(i); j--) {
// Traverse the backpack capacity
dp[j] = Math.max(dp[j], dp[j - weight.get(i)] + value.get(i));
}
System.out.println(Arrays.toString(dp));
}
}
边栏推荐
- Building personal network disk based on nextcloud
- Opencv calculation outsourcing rectangle
- [test platform development] 23. interface assertion function - save interface assertion and edit echo
- 初识C语言函数
- Mathematical function of MySQL function summary
- How to realize 485 wireless communication between multiple sensors and Siemens PLC?
- Live classroom system 02 build project environment
- Kettle实现共享数据库连接及插入更新组件实例
- 俄方希望有效落实农产品外运“一揽子”协议
- 爬虫中selenium实现自动给csdn博主文章点收藏
猜你喜欢
![[untitled] test [untitled] test](/img/9d/c80dd9a1df2cd6cbbfc597d73a63b2.png)
[untitled] test [untitled] test

21 - 二叉树的垂直遍历
![[test platform development] 20. Complete the function of sending interface request on the edit page](/img/ab/fed56b5bec990a25303c327733a8e6.png)
[test platform development] 20. Complete the function of sending interface request on the edit page

爬虫中selenium实现自动给csdn博主文章点收藏

Advanced operation and maintenance 02
![[applet automation minium] III. element positioning - use of wxss selector](/img/ec/51eadd08bea18f8292aa3521f11e8a.png)
[applet automation minium] III. element positioning - use of wxss selector

如何实现多个传感器与西门子PLC之间485无线通讯?

Yunna - how to strengthen fixed asset management? How to strengthen the management of fixed assets?
![[applet automation minium] i. framework introduction and environment construction](/img/1f/95b78e6574c3af3ff7abcf5db838f5.png)
[applet automation minium] i. framework introduction and environment construction

PKI体系快速介绍
随机推荐
[test platform development] 20. Complete the function of sending interface request on the edit page
linux定时备份数据库脚本
452. 用最少数量的箭引爆气球
C thread lock and single multithreading are simple to use
直播课堂系统03-model类及实体
Russia hopes to effectively implement the "package" agreement on the export of agricultural products
初识C语言函数
AVX指令集加速矩阵乘法
头部姿态估计原理及可视化_loveliuzz的博客-程序员宅基地_头部姿态估计
[test platform development] 21. complete sending interface request and display response header information
转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。
[untitled] test [untitled] test
[WinForm] desktop program implementation scheme for screenshot recognition and calculation
[applet automation minium] i. framework introduction and environment construction
C# 线程锁和单多线程简单使用
什么是Promise?Promise有什么好处
运维高级作业03
直播课堂系统02-搭建项目环境
Introduction and mechanism of Aptos
【软件测试】如何梳理你测试的业务