当前位置:网站首页>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 *(・ω・)ノ
边栏推荐
- 电力运维云平台:开启电力系统“无人值班、少人值守”新模式
- 如何避免电弧产生?—— AAFD故障电弧探测器为您解决
- 【北京大学】Tensorflow2.0-1-开篇
- [selenium automation] common notes
- Paper notes multi UAV collaborative monolithic slam
- go踩坑——no required module provides package : go.mod file not found in current directory or any parent
- Pytoch --- use pytoch to realize linknet for semantic segmentation
- Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
- Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
- [论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
猜你喜欢
Hisilicon 3559 universal platform construction: YUV422 pit stepping record
JS how to realize array to tree
Get to know ROS for the first time
abc 258 G - Triangle(bitset)
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
IT转测试岗,从迷茫到坚定我究竟付出了什么?
Fs8b711s14 electric wine bottle opener MCU IC scheme development special integrated IC
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
2022.07.03 (lc_6111_counts the number of ways to place houses)
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
随机推荐
初识ROS
IT转测试岗,从迷茫到坚定我究竟付出了什么?
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
IELTS examination process, what to pay attention to and how to review?
[error reporting] "typeerror: cannot read properties of undefined (reading 'split')“
lambda表达式
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
快解析内网穿透帮助企业快速实现协同办公
打新债开户注册安全吗?有没有风险的?靠谱吗?
Build your own minecraft server with fast parsing
使用快解析搭建自己的minecraft服务器
abc 258 G - Triangle(bitset)
Tester's algorithm interview question - find mode
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
JS how to realize array to tree
Summer challenge brings you to play harmoniyos multi terminal piano performance
Advanced template
他做国外LEAD,用了一年时间,把所有房贷都还清了
2022.07.03 (LC 6109 number of people who know secrets)
Verilog tutorial (11) initial block in Verilog