当前位置:网站首页>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));
}
}
边栏推荐
- The self-developed data products have been iterated for more than a year. Why not buy third-party commercial data platform products?
- Selenium in the crawler realizes automatic collection of CSDN bloggers' articles
- MySQL的大心脏 — 索引
- 爬虫中selenium实现自动给csdn博主文章点收藏
- 21 - 二叉树的垂直遍历
- C thread lock and single multithreading are simple to use
- [record of question brushing] 19. Delete the penultimate node of the linked list
- 用FFT加速特殊矩阵的矩阵向量乘运算
- supervisord安装使用
- [test platform development] 21. complete sending interface request and display response header information
猜你喜欢

利用shell脚本实现封禁扫描频率过高的ip

Yunna - how to strengthen fixed asset management? How to strengthen the management of fixed assets?

MySQL unique index has no duplicate value, and the error is repeated

Zhongwang CAD professional 2022 software installation package download and installation tutorial

Kettle实现共享数据库连接及插入更新组件实例

【测试平台开发】二十、完成编辑页发送接口请求功能
![[applet automation minium] i. framework introduction and environment construction](/img/1f/95b78e6574c3af3ff7abcf5db838f5.png)
[applet automation minium] i. framework introduction and environment construction

【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显

Kettle實現共享數據庫連接及插入更新組件實例

Live classroom system 03 model class and entity
随机推荐
What is per title encoding?
直播课堂系统01-数据库表设计
[WinForm] desktop program implementation scheme for screenshot recognition and calculation
How to realize 485 wireless communication between multiple sensors and Siemens PLC?
ArgoCD 用户管理、RBAC 控制、脚本登录、App 同步
Kettle implements shared database connection and insert update component instances
linux定时备份数据库脚本
452. Detonate the balloon with the minimum number of arrows
MySQL 常用命令
CSDN writing method (II)
NVIDIA vid2vid论文复现
NVIDIA vid2vid paper reproduction
js拖拽元素
运维高级作业02
C# 线程锁和单多线程简单使用
numpy和pytorch的版本对应关系
Introduction and mechanism of Aptos
OpenHarmony南向学习笔记——Hi3861+HC-SR04超声波检测
LeetCode-227-基本计算器||
身份证号正则验证