当前位置:网站首页>[dynamic programming] - Knapsack Problem
[dynamic programming] - Knapsack Problem
2022-06-27 12:59:00 【Xuanche_】

Classification of knapsack problem

01 knapsack problem
sample input
4 5 1 2 2 4 3 4 4 5sample output :
8
In the process of selection The first i The state transition equation of an object , We can take it out
The maximum value of plus w[i]
No second choice i If it's an item , The equation of state transition is
![\small f[i,j]=max\begin{cases}f[i-1,j] & \\ f[i-1, j -V_i] +w_i &if(j\geqslant V_i) \end{cases}](http://img.inotgo.com/imagesLocal/202206/27/202206271235354600_6.gif)

#include <iostream>
#include <cstring>
using namespace std;
const int N = 1010;
int n, m;
int v[N], w[N];
int f[N][N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i];
for(int i = 1; i <= n; i ++ )
for(int j = 0; j <= m; j ++ )
{
f[i][j] = f[i - 1][j];
if(j >= v[i]) f[i][j] = max(f[i][j], f[i - 1][j - v[i]] + w[i]);
}
cout << f[n][m] << endl;
return 0;
}From two-dimensional optimization to one-dimensional optimization
adopt DP State transfer equation of , We found that , At each stage i The state of is only related to that of the previous stage i-1 The state of . under these circumstances , It can be used as “ Scrolling array ” Optimization method , Reduce time overhead .
because
there
Certain ratio
Calculated earlier , therefore
The result of calculation is i Layer of , And in our above DP Fang Chengzhong , What is used is The first i - 1 layer The data of , So if you traverse the volume from small to large , There will be “ Data pollution ” The situation of . So the enumeration of volume should be from large to small .
One dimensional optimization code implementation
#include <iostream> #include <cstring> using namespace std; const int N = 1010; int n, m; int v[N], w[N]; int f[N]; int main() { cin >> n >> m; for(int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i]; for(int i = 1; i <= n; i ++ ) for(int j = m; j >= v[i]; j -- ) { f[j] = max(f[j], f[j - v[i]] + w[i]); } cout << f[m] << endl; return 0; }
Complete knapsack problem
sample input
4 5 1 2 2 4 3 4 4 5sample output :
10

about Simple approach Yes :
analogy 01 knapsack problem
Code implementation
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
int v[N], w[N];
int f[N][N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i];
for(int i = 1; i <= n; i ++ )
for(int j = 0; j <= m; j ++ )
for(int k = 0; k * v[i] <= j; k ++ )
f[i][j] = max(f[i][j], f[i - 1][j - k * v[i]] + k * w[i]);
cout << f[n][m] << endl;
return 0;
}By comparing the following equations, we can find that :
obtain :
![]()
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n, m;
int v[N], w[N];
int f[N][N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i];
for(int i = 1; i <= n; i ++ )
for(int j = 0; j <= m; j ++ )
{
f[i][j] = f[i - 1][j];
if(j >= v[i]) f[i][j] = max(f[i][j], f[i][j - v[i]] + w[i]);
}
cout << f[n][m] << endl;
return 0;
}One dimensional optimization
#include <iostream> #include <algorithm> using namespace std; const int N = 1010; int n, m; int v[N], w[N]; int f[N]; int main() { cin >> n >> m; for(int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i]; for(int i = 1; i <= n; i ++ ) for(int j = v[i]; j <= m; j ++ ) { f[j] = max(f[j], f[j - v[i]] + w[i]); } cout << f[m] << endl; return 0; }
contrast 01 knapsack and Completely backpack problem , There are the following differences :
01 knapsack :![f[i,j] = max(f[i,j], f[i - 1][j-v[i]] + w[i])](http://img.inotgo.com/imagesLocal/202206/27/202206271235354600_40.gif)
Completely backpack :![f[i,j]=max(f[i,j],f[i][j - v[i]+ w[i])](http://img.inotgo.com/imagesLocal/202206/27/202206271235354600_15.gif)
For a complete backpack , The enumeration of volumes can from v[i] Enumerate to m, Because it every i Layer update I'm using a number i Layer data , non-existent “ Data pollution ” The problem of .
Multiple knapsack problem
sample input
4 5 1 2 3 2 4 1 3 4 3 4 5 2sample output :
10

![f[i,j]=max(f[i-1][j-v[i] * k] + w[i] * k)](http://img.inotgo.com/imagesLocal/202206/27/202206271235354600_29.gif)
Plain writing
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, m;
int v[N], w[N], s[N];
int f[N][N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) cin >> v[i] >> w[i] >> s[i];
for(int i = 1; i <= n; i ++ )
for(int j = 0; j <= m; j ++ )
for(int k = 0; k <= s[i] && k * v[i] <= j; k ++ )
f[i][j] = max(f[i][j], f[i - 1][j - v[i] * k] + w[i] * k);
cout << f[n][m] << endl;
return 0;
}Binary optimization of multiple knapsacks
as everyone knows , from
this k individual 2 Select several addition from the integer powers of , It can show
Any integer between . further , We find satisfaction
Maximum integer for p, set up
that :
- according to p The maximum of , Yes
, Can be launched
, therefore
Choose a number of additions to show
Any integer between ; - from
Select several of them to add , It can show
Any integer between , According to the Ri The definition of ,
, therefore ,
Choose a few to show
Any integer between .
in summary , We can take the quantity as
Of the i Break an item into p + 2 Items , Their volumes are :

this p + 2 Items can be put together
Between all can be Vi Divisible number , And can not be rounded up to be greater than Ci * Vi Number of numbers . This is equivalent to the volume in the original problem being Vi Can use 0~Ci Time . This method only splits each item into
individual , More efficient .
Code implementation
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 25000, M = 2010;
int n, m;
int v[N], w[N];
int f[N];
int main()
{
cin >> n >> m;
int cnt = 0;
for(int i = 1; i <= n; i ++ )
{
int a, b, s;
cin >> a >> b >> s;
int k = 1;
while(k <= s)
{
cnt ++ ;
v[cnt] = a * k;
w[cnt] = b * k;
s -= k;
k *= 2;
}
if(s > 0)
{
cnt ++;
v[cnt] = a * s;
w[cnt] = b * s;
}
}
n = cnt;
for(int i = 1; i <= n; i ++ )
for(int j = m; j >= v[i]; j -- )
f[j] = max(f[j], f[j - v[i]] + w[i]);
cout << f[m] << endl;
return 0;
}Group knapsack problem
sample input
3 5 2 1 2 2 4 1 3 4 1 4 5sample output :
8


AC Code
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, m;
int v[N][N], w[N][N], s[N];
int f[N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
{
cin >> s[i];
for(int j = 0; j < s[i]; j ++ )
cin >> v[i][j] >> w[i][j];
}
for(int i = 1; i <= n; i ++ )
for(int j = m; j >= 0; j -- )
for(int k = 0; k < s[i]; k ++ )
if(v[i][k] <= j)
f[j] = max(f[j], f[j - v[i][k]] + w[i][k]);
cout << f[m] << endl;
return 0;
}边栏推荐
- 数据库系列:MySQL索引优化与性能提升总结(综合版)
- How to modify a node_ Files in modules
- Configuration management center of microservices
- 清楚的自我定位
- Mybaitis generator details
- 推荐系统的下一步?阿里时空聚合GNN,效果吊打LightGCN!
- Picocli getting started
- [tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)
- 诗歌一首看看
- 【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元
猜你喜欢

ACL 2022 | TAMT proposed by Chinese Academy of Sciences: TAMT: search for a portable Bert subnet through downstream task independent mask training

Hue new account error reporting solution
![[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)](/img/ce/b58e436e739a96b3ba6d2d33cf8675.png)
[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)

On the complexity of software development and the way to improve its efficiency

nmcli team bridge 基本配置

本地可视化工具连接阿里云centOS服务器的redis

Uniapp drop-down layer selection box effect demo (sorting)

夏日里的清凉

基于JSP实现医院病历管理系统

Snipaste, the world's strongest screenshot software
随机推荐
【TcaplusDB知识库】TcaplusDB-tcapulogmgr工具介绍(一)
自定义多线程基类threading.Event
Configuration management center of microservices
和动态规划的第一次相遇
Record number of visits yesterday
带你认识图数据库性能和场景测试利器LDBC SNB
Database Series: MySQL index optimization and performance improvement summary (comprehensive version)
Script defer async mode
今日睡眠质量记录78分
Deploy redis sentinel mode using bitnamiredis Sentinel
Two usages of enumeration classes
Ssh server configuration file sshd_ Config and operation
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(三)
TCP 流控问题两则
二叉树的三种遍历方式
抖音实战~公开/私密短视频互转
How to modify a node_ Files in modules
Object serialization
printf不定长参数原理
Centos7命令行安装Oracle11g


![\small f[i,j] = max(f[i -1,j],f[i-1,j-v]+w,f[i-1,j-2v]+2w\cdots )](http://img.inotgo.com/imagesLocal/202206/27/202206271235354600_38.gif)
![\small f[i,j-v]=max( f[i-1,j-v],f[i-1],f[i-1,j-2v]+w,f[i-2,j-3v]+2w\cdots )](http://img.inotgo.com/imagesLocal/202206/27/202206271235354600_3.gif)

, Can be launched
, therefore
Choose a number of additions to show
Any integer between ;
Select several of them to add , It can show
Any integer between , According to the Ri The definition of ,
, therefore ,
Choose a few to show
Any integer between .