当前位置:网站首页>[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;
}边栏推荐
- InputStream/OutputStream(文件的输入输出)
- Arcpy 利用updatelayer函数改变图层的符号系统
- How to use multithreading to export excel under massive data? Source code attached!
- 【无标题】
- Arcpy uses the updatelayer function to change the symbol system of the layer
- Is the insurance annuity product worth buying? Is there a hole?
- 如何实现视频平台会员多账号登录
- 4G wireless all network solar hydrological equipment power monitoring system bms110
- 2022.7.3-----leetcode. five hundred and fifty-six
- 如何避免 JVM 内存泄漏?
猜你喜欢

R statistical mapping - random forest classification analysis and species abundance difference test combination diagram

Appium foundation - appium installation (II)

C language - Blue Bridge Cup - Snake filling

AWT common components, FileDialog file selection box

注释与注解

17-18. Dependency scope and life cycle plug-ins

C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing

C语言中的函数(详解)

Gridview出现滚动条,组件冲突,如何解决
![Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai](/img/a5/be967170a18cb2de097f01a6d4e039.jpg)
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
随机推荐
MySQL installation and configuration
JSON Web Token----JWT和传统session登录认证对比
Common JS tool Libraries
Abap:ooalv realizes the function of adding, deleting, modifying and checking
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
buuctf-pwn write-ups (8)
Impact relay jc-7/11/dc110v
How to realize multi account login of video platform members
Weekly summary (*63): about positive energy
Dimension and format of data
Tree DP
Experience weekly report no. 102 (July 4, 2022)
Internet of things protocol ZigBee ZigBee module uses the concept of protocol stack
注释与注解
How to determine whether an array contains an element
Manually page the list (parameter list, current page, page size)
对List进行排序工具类,可以对字符串排序
How to implement cross domain requests
Is the insurance annuity product worth buying? Is there a hole?
buuctf-pwn write-ups (8)