当前位置:网站首页>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 >
边栏推荐
猜你喜欢

Windows software: how to install mysql5.7 and configure environment variables

qiankun子应用package.json中的name成了默认路径

jarvisoj_ level2

Xmind用例导入到TAPD的方案(附代码)

Chapter III Organization Code
![[OGeek2019]babyrop](/img/7a/18e8b985629488346e596cdf2a215c.png)
[OGeek2019]babyrop

DGS's mutations

文本和图片的绘制、数据存储、localStorage、sessionStorage、cookie三者的区别

OA项目之我的会议(查询)
![[Fifth space 2019 finals]pwn5](/img/51/03e6078961a8eab991fa08bb178a7b.png)
[Fifth space 2019 finals]pwn5
随机推荐
Is Zhongyuan securities reliable? Is it legal? Is it safe to open a stock account?
pytorch中with torch.no_grad(): && model.eval()
ESP8266————AT指令+网络透传
工具推荐-语雀
The title of solo article will filter out some tags
[attack and defense world web] difficulty five-star 15 point advanced question: bug
自己喜欢投资
Qt | 设置部件大小 sizeHint、minimumSizeHint、sizePolicy、stretch factor
DGS first acquaintance
[computer three-level information security] access control model
Linked list - 206. Reverse linked list (this question is very important)
Super simple training of force deduction and problem brushing
深度学习之 9 前馈神经网络 基本概念
Problems encountered in pytorch
DDD thinking structure learning
QT create a background mask, pop up the child window, and the background of the parent window turns black and dark
Esp8266 - at command + network transparent transmission
作为一个程序员,有什么想对新人说的吗?
474-82(8、221、300)
JS學習筆記-- 數組方法 底層實現方式