当前位置:网站首页>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 *(・ω・)ノ
边栏推荐
- 《论文笔记》Multi-UAV Collaborative Monocular SLAM
- Face recognition 5- insight face padding code practice notes
- Skills in analyzing the trend chart of London Silver
- 【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
- abc 258 G - Triangle(bitset)
- OpenHarmony资源管理详解
- Basic points of the game setup of the points mall
- Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
- 跨域请求
猜你喜欢

【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)

How to avoid arc generation—— Aafd fault arc detector solves the problem for you

海思3559万能平台搭建:YUV422的踩坑记录

He worked as a foreign lead and paid off all the housing loans in a year

Fast parsing intranet penetration helps enterprises quickly achieve collaborative office

圖解網絡:什麼是網關負載均衡協議GLBP?

Face recognition 5- insight face padding code practice notes

Continuous modification of business scenario functions

基于三维gis平台的消防系统运用

Build your own minecraft server with fast parsing
随机推荐
【北京大学】Tensorflow2.0-1-开篇
Hologres query management and timeout processing
海思3559万能平台搭建:YUV422的踩坑记录
基本放大电路的学习
22-07-02周总结
[IELTS reading] Wang Xiwei reading P3 (heading)
Hash table, hash function, bloom filter, consistency hash
Two numbers replace each other
PermissionError: [Errno 13] Permission denied: ‘data. csv‘
JS how to realize array to tree
What is the difference between port mapping and port forwarding
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
Using fast parsing intranet penetration to realize zero cost self built website
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
【路径规划】RRT增加动力模型进行轨迹规划
(script) one click deployment of any version of redis - the way to build a dream
ORB(Oriented FAST and Rotated BRIEF)
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?