当前位置:网站首页>[backpack DP] backpack problem
[backpack DP] backpack problem
2022-07-04 06:30:00 【Nathan Qian】
Complete knapsack problem
subject
explain
- Because each item can choose unlimited pieces , So when we decide to use an item , The number of pieces needs to be considered
- So the expression is in the form of the first formula
- Look at the second expression f[i,j-v] In the form of , There is only one difference from the first w[i]
- Therefore, the recurrence relation can be expressed as
- f(i,j)=max(f(i,j-v)+w,f(i-1,j);
Code segment
#include<iostream>
using namespace std;
const int N=1010;
int f[N][N],v[N],w[N];
int n,m;
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;
}
边栏推荐
- uniapp 自定義環境變量
- R统计绘图-随机森林分类分析及物种丰度差异检验组合图
- Gridview出现滚动条,组件冲突,如何解决
- what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
- Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
- Compound nonlinear feedback control (2)
- AWT common components, FileDialog file selection box
- Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
- 如何避免 JVM 内存泄漏?
- Understanding of cross domain and how to solve cross domain problems
猜你喜欢
Learning multi-level structural information for small organ segmentation
Practical gadget instructions
如何避免 JVM 内存泄漏?
Sleep quality today 78 points
C realize Snake games
树形dp
Which water in the environment needs water quality monitoring
Arcpy 利用updatelayer函数改变图层的符号系统
How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
《ClickHouse原理解析与应用实践》读书笔记(4)
随机推荐
ADC voltage calculation of STM32 single chip microcomputer
Design and implementation of redis 7.0 multi part AOF
Weekly summary (*63): about positive energy
Learning multi-level structural information for small organ segmentation
How to determine whether an array contains an element
分布式CAP理论
金盾视频播放器拦截的软件关键词和进程信息
R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
InputStream/OutputStream(文件的输入输出)
Arcpy 利用updatelayer函数改变图层的符号系统
雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
Download kicad on Alibaba cloud image station
Grounding relay dd-1/60
4G wireless all network solar hydrological equipment power monitoring system bms110
The sorting in C language realizes the number sorting method from small to large
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
MySQL learning notes 3 - JDBC
微信小程序使用rich-text中图片宽度超出问题
[problem record] 03 connect to MySQL database prompt: 1040 too many connections
How to implement lazy loading in El select (with search function)