当前位置:网站首页>Multi knapsack explanation of dynamic programming knapsack problem
Multi knapsack explanation of dynamic programming knapsack problem
2022-07-24 00:05:00 【Round to two meters high Xiaochen】
Previous articles on knapsack :
One 、 What is the multiple knapsack problem ?
Yes n Items , The volume of each item is v[i], Value is w[i]. The existing one has a capacity of V The backpack , Ask how to select items to put into the backpack , Make the total value of the items in the backpack the largest . Each of these items has s[i] Pieces of .
Multiple backpacks and full backpacks 、01 Backpacks are different :
- 01 There are only two situations when choosing an item in the backpack: not choosing it and choosing it once
- Complete backpack can choose a certain item without , You can also choose once , Choose twice ... There is no upper limit to the number of choices ( As long as the backpack can be put down )
- Multiple backpacks can choose an item without , You can choose once 、 secondary ... You can only choose s[i] Time ( As long as the backpack can be put down )
Two 、 Example analysis
1. subject :

It is not easy to see the rule in the sample of the original test , So use other test samples :
sample input :
3 7qu
2 3 12
2 5 15
1 2 3
sample output :
12
2. The first one is : Simple approach
01 knapsack : The first i This item can be taken from 0 Pieces of , Can take 1 Pieces of
Multiple backpack : The first i This item can be taken from 0 Pieces of , take 1 Pieces of 、 take 2 Pieces of ······ take s[i] Pieces of
Multiple backpacks are transformed into 01 Knapsack solving : The first i Items replaced s[i] Pieces of 01 Items in the backpack , The volume of each article is k* v[i], Value is k * w [i] (0<=k<=s[i])
Core code :
for(i=1;i<=n;i++)
for(j=m;j>=v[i];j--)
for(k=0;k<=s[i]&&k*v[i]<=j;k++)
f[j]=max(f[j],f[j-k*v[i]]+k*w[i]);
3. The second kind : Binary optimization

The example can be expressed as :
Binary optimization core code :
int cnt=0;// Counter
for(int i=1;i<=n;i++)
{
int a,b,s;
cin>>a>>b>>s;
int k=1;
// Binary split
while(k<=s)
{
cnt++;
v[cnt]=a*k;
w[cnt]=b*k;
s-=k;
k*=2;
}
if(s>0)// The remaining
{
cnt++;
v[cnt]=a*s;
w[cnt]=b*s;
}
}
After splitting, it becomes :
Use it once after optimization 01 knapsack :
for(int i=1;i<=cnt;i++)
for(int j=m;j>=v[i];j--)
f[j]=max(f[j],f[j-v[i]]+w[i]);
cout<<f[m]<<endl;
4. The second kind : Monotonic queue optimization < To be written >
边栏推荐
- PyTorch 中遇到的问题
- Qt | 设置部件大小 sizeHint、minimumSizeHint、sizePolicy、stretch factor
- Realize the function of uploading and downloading files and directories similar to RZ and SZ on the native terminal
- 链表——206. 反转链表(这题很重要)
- 北大青鸟昌平校区:运维就业现状怎么样?技能要求高吗?
- webrtc 1对1 -基本架构与目录
- day2
- Linked list - 707. Design linked list
- Multi table query_ External connection
- Intel Intel realsense realistic depth camera self calibration operation steps explanation D400 series is applicable
猜你喜欢

Automatic escape processing in JMeter

qiankun子应用package.json中的name成了默认路径
![[Fifth space 2019 finals]pwn5](/img/51/03e6078961a8eab991fa08bb178a7b.png)
[Fifth space 2019 finals]pwn5
Solo article body contains & lt; & gt; Labels affect page styles

Write all the code as soon as you change the test steps? Why not try yaml to realize data-driven?

深度学习之 9 前馈神经网络2:实现前馈神经网络,模型调优
![[computer three-level information security] access control model](/img/6e/6bd34fd729587a67beb93e70ff55a9.png)
[computer three-level information security] access control model
![[opencv] - when the parameter type of cv.threshold() function is a number, what does it mean](/img/7c/be8049ece7f109b44d361c32cf2da8.png)
[opencv] - when the parameter type of cv.threshold() function is a number, what does it mean

QT create a background mask, pop up the child window, and the background of the parent window turns black and dark

bjdctf_ 2020_ babystack
随机推荐
深度学习之 9 前馈神经网络 基本概念
作为一个程序员,有什么想对新人说的吗?
Redis cluster construction (cluster cluster mode, fragment cluster)
Deep learning 9 basic concepts of feedforward neural networks
Y75. Chapter IV Prometheus factory monitoring system and practice -- Prometheus alarm setting (VI)
474-82(8、221、300)
太空射击第08课: 改进的碰撞
[microservice Architecture] distributed transactions
JS学习笔记-- 数组方法 底层实现方式
jarvisoj_ level2
kubernetes error
[for loop if conditional statement] summary
Linked list - 707. Design linked list
warmup_ csaw_ two thousand and sixteen
Ubtun update source
Realize the function of uploading and downloading files and directories similar to RZ and SZ on the native terminal
DDD thinking structure learning
y75.第四章 Prometheus大厂监控体系及实战 -- prometheus报警设置(六)
Space shooting lesson 07: add graphics
webrtc 1对1 -基本架构与目录