当前位置:网站首页>Complete knapsack problem (template)
Complete knapsack problem (template)
2022-07-05 00:23:00 【The left wheel of fate】
List of articles
List of articles
Summary of problems
There's a backpack , The maximum bearing capacity is N, Now we need to pack some items i( The value of the item is value[i], The weight of the article is weight[i]), There is no limit to the number of items ( That is, it can be installed repeatedly ), Ask for the maximum value of these items packed on this back .
Complete backpack features
Each item has an infinite number of
Completely backpack ( One dimensional rolling array version )
analysis
- Ahead 01 Backpack we can know , Items circulating outside are in positive order , Internal reverse circulation backpack . Internal reverse circulation is to ensure that items can only be added once , But the complete backpack has infinite items , It shows that my backpack can traverse forward , There is nothing wrong with the superposition of sub problems .
- 01 2D in backpack dp Two of the arrays for The order of traversal can be reversed , A one-dimensional dp Two of the arrays for The sequence of the cycle must first traverse the items , Then traverse the backpack capacity .
- In a complete backpack , For one dimension dp Array , In fact, two for The nesting order of loops can be reversed
- therefore , Whether it's 01 Backpack or full backpack , We usually go through items first , And then traverse the backpack ,
Be careful : Here is general
Code
# Completely backpack ( One dimensional rolling array version )
weight =[1,3,4]
value = [15,20,30]
Max = 4 # The maximum capacity of the backpack
dp = [0]*5 # dp[j] Indicates that the backpack capacity is j The maximum value of is dp[j]
for i in range(3):
for j in range(weight[i],5):
dp[j] = max(dp[j],dp[j-weight[i]]+value[i])
print(dp)
The final results
So the capacity is 4 The backpack , The greatest value is 60
If there is a mistake , Please correct me , Welcome to exchange , thank you *(・ω・)ノ
边栏推荐
- Hash table, hash function, bloom filter, consistency hash
- Pytoch --- use pytoch to realize linknet for semantic segmentation
- Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
- 实战模拟│JWT 登录认证
- Acwing164. Accessibility Statistics (topological sorting +bitset)
- 公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
- Illustrated network: what is gateway load balancing protocol GLBP?
- 企业应用业务场景,功能添加和修改C#源码
- (脚本)一键部署redis任意版本 —— 筑梦之路
- 快解析——好用的内网安全软件
猜你喜欢
[selenium automation] common notes
uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
Pytoch --- use pytoch to realize linknet for semantic segmentation
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
Detailed explanation of openharmony resource management
What is the difference between port mapping and port forwarding
Date time type and format in MySQL
How many triangles are there in the golden K-line diagram?
随机推荐
Get to know ROS for the first time
Distributed base theory
ORB(Oriented FAST and Rotated BRIEF)
PermissionError: [Errno 13] Permission denied: ‘data. csv‘
巩固表达式C# 案例简单变量运算
Hisilicon 3559 universal platform construction: YUV422 pit stepping record
(script) one click deployment of any version of redis - the way to build a dream
URLs and URIs
Application of multi loop instrument in base station "switching to direct"
【北京大学】Tensorflow2.0-1-开篇
Consolidated expression C case simple variable operation
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
How to save your code works quickly to better protect your labor achievements
人脸识别5- insight-face-paddle-代码实战笔记
跨域请求
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
How many triangles are there in the golden K-line diagram?
2022.07.03 (LC 6109 number of people who know secrets)
如何用快解析自制IoT云平台
Significance of acrel EMS integrated energy efficiency platform in campus construction