当前位置:网站首页>[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;
}
边栏推荐
- QT releases multilingual International Translation
- Detectron: train your own data set -- convert your own data format to coco format
- C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
- 2022.7.2-----leetcode.871
- How to implement cross domain requests
- Realize IIC data / instruction interaction with micro batg135
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- C语言中的排序,实现从小到大的数字排序法
- High performance parallel programming and optimization | lesson 02 homework at home
- The width of the picture in rich text used by wechat applet exceeds the problem
猜你喜欢
我的NVIDIA开发者之旅——优化显卡性能
C實現貪吃蛇小遊戲
Sword finger offer II 038 Daily temperature
【问题记录】03 连接MySQL数据库提示:1040 Too many connections
Appium基础 — APPium安装(二)
Functions in C language (detailed explanation)
C réaliser des jeux de serpents gourmands
How to use multithreading to export excel under massive data? Source code attached!
Learning multi-level structural information for small organ segmentation
ORICO ORICO outdoor power experience, lightweight and portable, the most convenient office charging station
随机推荐
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
云原生——上云必读之SSH篇(常用于远程登录云服务器)
lightroom 导入图片灰色/黑色矩形 多显示器
Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
C语言练习题(递归)
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Uninstall Google drive hard drive - you must exit the program to uninstall
如何避免 JVM 内存泄漏?
Practical gadget instructions
InputStream/OutputStream(文件的输入输出)
Vant --- detailed explanation and use of list component in vant
Is the insurance annuity product worth buying? Is there a hole?
QT QTableWidget 表格列置顶需求的思路和代码
Abap:ooalv realizes the function of adding, deleting, modifying and checking
Detectron: train your own data set -- convert your own data format to coco format
Grounding relay dd-1/60
SQL injection SQL lab 11~22
双色球案例
[Android reverse] function interception (CPU cache mechanism | CPU cache mechanism causes function interception failure)