当前位置:网站首页>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 *(・ω・)ノ
边栏推荐
- 巩固表达式C# 案例简单变量运算
- (脚本)一键部署redis任意版本 —— 筑梦之路
- Enterprise application business scenarios, function addition and modification of C source code
- Is it safe to open an account in the College of Finance and economics? How to open an account?
- 兩個數相互替換
- Hologres query management and timeout processing
- P3304 [SDOI2013]直径(树的直径)
- Five papers recommended for the new development of convolutional neural network in deep learning
- XML的解析
- JS 将伪数组转换成数组
猜你喜欢
Tester's algorithm interview question - find mode
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
Summer challenge brings you to play harmoniyos multi terminal piano performance
PMP certificate renewal process
快解析内网穿透帮助企业快速实现协同办公
"Xiaodeng" domain password policy enhancer in operation and maintenance
[selenium automation] common notes
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
Deux nombres se remplacent
Pytoch --- use pytoch to realize linknet for semantic segmentation
随机推荐
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
挖财学院开户安全的吗?开户怎么开?
Cross domain request
业务场景功能的继续修改
[selenium automation] common notes
【C】(笔试题)指针与数组,指针
如何用快解析自制IoT云平台
How many triangles are there in the golden K-line diagram?
Upload avatar on uniapp
Advanced template
Illustrated network: what is gateway load balancing protocol GLBP?
Learning of basic amplification circuit
What did I pay for it transfer to testing post from confusion to firmness?
巩固表达式C# 案例简单变量运算
Consolidated expression C case simple variable operation
IELTS examination process, what to pay attention to and how to review?
Face recognition 5- insight face padding code practice notes
《论文笔记》Multi-UAV Collaborative Monocular SLAM
打新债开户注册安全吗?有没有风险的?靠谱吗?
多回路仪表在基站“转改直”方面的应用