当前位置:网站首页>Basic knapsack problem
Basic knapsack problem
2022-07-25 10:15:00 【Wendianfei】
Basic Backpack
subject
Yes N Items and a capacity for V The backpack . The first i The weight of the item is w[i], The value is v[i]. Solve which items are loaded into the backpack so that the total weight of these items does not exceed the backpack capacity , And the sum of values is the largest .
The basic idea
This is the basic knapsack problem , Characteristic is : There is only one item of each kind , You can choose to put it or not .
Define states with subproblems : namely f[i][v] Before presentation i Items are just put into a capacity of v The maximum value that you can get from your backpack . Then State transition equation That is :
f[i][v]=max{ f[i-1][v], f[i-1][v-w[i]]+v[i] }.
It can compress space ,f[v]=max{f[v],f[v-w[i]]+v[i]}
This equation is very important , Basically, the equations of all knapsack related problems are derived from it . So it is necessary to explain it in detail :“ Before the i The capacity of items is v In my backpack ” This subproblem , If we only consider i Strategy of items ( With or without ), Then it can be transformed into a front only i-1 Problems with items . If you don't put the first i Item , Then the problem turns into “ front i-1 The capacity of items is v In my backpack ”, Value is f[i-1][v]; If you put the first i Item , Then the problem turns into “ front i-1 Put items into the remaining capacity of v-w[i] In my backpack ”, The greatest value you can get at this time is f [i-1][v-w[i]] Plus by putting the i The value of items v[i].
Be careful f[v] Meaningful if and only if there is a pre i A subset of items , The total cost is f[v]. So after recursion according to this equation , The final answer is not necessarily f[N] [V], It is f[N][0..V] The maximum of . If the state is defined as “ just ” Remove the word , We have to add another term to the transfer equation f[v-1], So that's a guarantee f[N] [V] That's the final answer . As for why this can , It's up to you to experience .
边栏推荐
猜你喜欢

数论--负进制转换

Advanced introduction to digital IC Design SOC

线程池的设计和原理

UE4源码的获取和编译
![[nearly 10000 words dry goods] don't let your resume don't match your talent -- teach you to make the most suitable resume by hand](/img/2d/e3a326175f04826b9d9c96baedc3a5.png)
[nearly 10000 words dry goods] don't let your resume don't match your talent -- teach you to make the most suitable resume by hand

Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)

Download and installation of QT 6.2

Rest使用与原理
![[necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.](/img/f5/e6739083f0dce8da1d09d078321633.png)
[necessary for growth] Why do I recommend you to write a blog? May you be what you want to be in years to come.

链表相关(设计链表及环链表问题)
随机推荐
salt常见问题
@Import, conditional and @importresource annotations
Debug篇快捷键入门
JDBC总结
腾讯云之错误[100007] this env is not enable anonymous login
小程序H5获取手机号方案
shortest-unsorted-continuous-subarray
文件的上传功能
概率论与数理统计 4 Continuous Random Variables and Probability Distributions(连续随机变量与概率分布)(上篇)
复现 SSL_Anti-spoofing, 使用 wav2vec 2.0 和数据增强的自动说话人认证的欺骗攻击与深度伪造检测
CCF 201509-2 date calculation
严重 [main] org.apache.catalina.util.LifecycleBase.handleSubClassException 初始化组件
力扣刷题组合问题总结(回溯)
C3D模型pytorch源码逐句详析(三)
动态规划,购物单问题
ROS分布式操作--launch文件启动多个机器上的节点
Qt 6.2的下载和安装
记录一些JS工具函数
Arm preliminaries
SQL 题目整理