当前位置:网站首页>Multiple knapsack problem
Multiple knapsack problem
2022-06-13 02:33:00 【Prodigal son's private dishes】
Concept : Yes N Class 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 values is the largest .
Multiple backpacks and 01 Backpacks are very much like , Why and 01 Backpacks are like ?
Each item has at most Mi Pieces available , hold Mi The pieces are spread out , It's really just a 01 It's a knapsack problem .
for example :
The maximum weight of the backpack is 10.
Item is :
weight value Number
goods 0 1 15 2
goods 1 3 20 3
goods 2 4 30 2
What's the maximum value of the items a backpack can carry ?
Is it different from ?
weight value Number
goods 0 1 15 1
goods 0 1 15 1
goods 1 3 20 1
goods 1 3 20 1
goods 1 3 20 1
goods 2 4 30 1
goods 2 4 30 1
It makes no difference , This turns into a 01 It's a knapsack problem , And only once per item .
The code to implement multiple knapsack in this way is as follows :
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));
}
}
There's another way to do it , It is to put the number of traversals of each commodity in 01 I'm going through it in my backpack .
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
List<Integer> weight = new ArrayList<>(Arrays.aslist(1, 3, 4));
List<Integer> price = new ArrayList<>(Arrays.aslist(15, 20, 20));
List<Integer> nums = new ArrayList<>(Arrays.aslist(2, 3, 2));
int bagWeight = 10;
// for(int i = 0; i < nums.size(); i++){
// if(nums[i] > 0){
// weight.add(weight.get(i));
// price.add(price.get(i));
// nums[i]--;
// }
// }
int[] dp = new int[bagWeight+1];
for(int i = 0; i < weight.size();i++){
for(int j = bagWeight; j >= weight.get(i); j--){
for(int k = 1; k <= nums[i] && j- k * weight[i] >= 0; k++){
dp[j] = Math.max(dp[j], dp[j-k * weight[i]] + k * price[i]);
}
}
}
return dp[bagWeight];
}
}
边栏推荐
- Model prediction of semantic segmentation
- Understand HMM
- Area of basic exercise circle ※
- [keras] train py
- Opencvshare4 and vs2019 configuration
- Chapter7-10_ Deep Learning for Question Answering (1/2)
- The precision of C language printf output floating point numbers
- Yovo3 and yovo3 tiny structure diagram
- Restrict cell input type and display format in CXGRID control
- ROS learning-7 error in custom message or service reference header file
猜你喜欢
03 recognize the first view component
Understanding and thinking about multi-core consistency
Mean Value Coordinates
Leetcode 926. 将字符串翻转到单调递增 [前缀和]
Barrykay electronics rushes to the scientific innovation board: it is planned to raise 360million yuan. Mr. and Mrs. Wang Binhua are the major shareholders
Chapter7-11_ Deep Learning for Question Answering (2/2)
Armv8-m (Cortex-M) TrustZone summary and introduction
How to destroy a fragment- How to destroy Fragment?
Is space time attention all you need for video understanding?
Opencvshare4 and vs2019 configuration
随机推荐
[Dest0g3 520迎新赛] 拿到WP还整了很久的Dest0g3_heap
Opencv 10 brightness contrast adjustment
Introduction to arm Cortex-M learning
[analysis notes] source code analysis of siliconlabs efr32bg22 Bluetooth mesh sensorclient
微信云开发粗糙理解
Think: when do I need to disable mmu/i-cache/d-cache?
Number of special palindromes in basic exercise of test questions
Introduction to armv8/armv9 - learning this article is enough
Basic exercises of test questions Fibonacci series
Redirection setting parameters -redirectattributes
[reading some papers] introducing deep learning into the public horizon alexnet
Understand HMM
Huffman tree and its application
redis. Conf general configuration details
03 认识第一个view组件
[open source] libinimini: a minimalist ini parsing library for single chip computers
How to destroy a fragment- How to destroy Fragment?
Gadgets: color based video and image cutting
[pytorch] kaggle image classification competition arcface + bounding box code learning
[reading papers] transformer miscellaneous notes, especially miscellaneous