[Algorithmic Universe - Learning Algorithms in Stories] Backpack dp 01 Backpack Problem

Only by familiarity with hands can one become art, and only by reading can one achieve excellence. Only by studying hard and practicing hard can we succeed, and the road is long!

Please add a picture description

foreword

Although computer is a rigorous subject, it is precisely because of the rigor that it must be interesting to read. In the author's first few algorithm articles, I used a short story as an introduction to introduce the algorithm, but when I looked back, I found that the academic flavor was still too strong, and I didn't want to read it at all. Orz~ so the author Decided to reorganize the structure of the article and present the entire algorithm in the form of a story, at least for readers to read, and hope to help everyone!

text

Xiao Ming's Adventure Journey (Prologue)

Xiao Ming is a teenager who loves to explore and longs to travel in the universe, but his father keeps him under control and won't let him go out. Finally, on his eighteenth birthday, his father called him over.

Father Ming: Son, you are already a mature adult. Starting today, you can explore the universe.

Xiao Ming: Good! !

Father Ming: Wait a minute, the universe is full of dangers and pirates are rampant, so before you go out, I have something to give you.

(Ming's father took Xiao Ming to his own room, opened a big box, and a lot of equipment came into Xiao Ming's eyes)

Xiao Ming: Father, will you give me all these? Thank you father!

Father Ming said angrily: "Nizi, even if I give it all to you, why does your little broken spaceship have so much equipment?" Listen, now the weight and lethality of these equipment are written on it, you can pick whatever you want, but if you put all the equipment you picked on the spaceship and the spaceship can't fly (because the equipment is too heavy), you Just don't go out, learn the algorithm obediently, you know?

(Father Ming went out)

Xiao Ming: With so much equipment, I have to think about it...

(Xiao Ming thought for a while and decided to follow his intuition first. He picked up the first piece of equipment and threw it on the spaceship, then picked up the second piece and continued to throw it until the spaceship was about to be overweight. Then he stopped in satisfaction, Afterwards, he found that there were many small and highly lethal weapons out there, he smiled helplessly, and removed all the weapons on the ship to choose again.)

Xiao Ming: Since there are so many light weapons, it’s better to pick the light ones. Quantitative changes lead to qualitative changes. I’m so smart!

(He began to lift the weapons upward again, but this time he found that some heavy, but extremely lethal weapons seemed to have not been selected. He moved the weapons out again with great effort.)

Xiao Ming: Then this time I'll move to the head office which is more lethal first! ?

(Unfortunately, this time he only moved a few weapons, and the spaceship was overweight, and the lethality was not even as strong as the first time.)

Xiao Ming scratching his head: Oh, it's so annoying. . It's all because I didn't learn the algorithm well before, so I regret it. .

(He thought for a long time but couldn't come up with a reason. In desperation, he looked for Father Ming with his head down.)

Proud Father Ming: I knew you kid wouldn't move, why don't you study hard this time? Okay, this time I will tell you how to move the best, but you must not forget to learn the algorithm when you go on an adventure in the future! Do you remember that you did a problem of collecting herbs before? (see memoized search )

(Father Ming took out a form)

Father Ming: I am optimistic about the brat. The two dimensions of this table are which weapon to go to and the load allocated by the spaceship for the weapon that has been moved. Do you think it is similar to the question about picking herbs? That's right, these two questions are actually the same question type, 01 knapsack problem . For the 01 knapsack problem, the transfer equations are fixed:
insert image description here

So you know how to solve it? Go fill out this form and get moving!

(Xiao Ming took the form and pondered for a while. Suddenly, he had a flash of inspiration.)

Xiao Ming: Father, look at this transfer equation. The update of each i is only related to i-1, and i is updated immediately after i-1 is updated each time, and we actually only need the last row of data, that is Doesn't it mean that we can store data in only one row? Then the transfer equation can be:

Father Ming laughed and said: That's right! This is a good idea, but you have to see clearly that if you do this, since you used the last jth and jw[i]th when updating the jth data, you have to start from The data used in the update will be saved only after the update is performed forward.

(Xiao Ming cheered, took the form and ran to move the equipment. After moving the equipment, he drove the spaceship, said goodbye to his father, and started his own adventure journey.)

Summarize

Xiao Ming's story has come to an end for the time being, but our algorithm still needs a little explanation.
f i, j refers to the maximum value they can create when the i-th item is selected, if the selected item occupies j so much load.
So reinterpret the second term of the two-dimensional transfer equation at the beginning: if you choose to take the i-th item, then which items selected in the previous i-1 items can only occupy so much load of jw[i], Because the i-th item takes away the load of w[i].

There is nothing to explain the rest, just go to the code:

#include <iostream>
using namespace std;
const int maxn = 13010;
int n, W, w[maxn], v[maxn], f[maxn];

int main() {
    
    
  cin >> n >> W;
  for (int i = 1; i <= n; i++) cin >> w[i] >> v[i]; 
  for (int i = 1; i <= n; i++)//循环n次,因为要对n件物品进行选择
    for (int l = W; l >= w[i]; l--)//从后向前更新
      f[l] = max(f[l], f[l - w[i]] + v[i]);//转移方程
  cout << f[W];
  return 0;
}

Please add a picture description

I am Shuang_Ai, a newcomer working hard on the road of algorithms, thank you for reading! This time I adopted a story-based structure, I hope you like it! The code text is not easy, if you think it is good, you can pay attention to it, I will bring more and more comprehensive algorithm explanations in the future!

Guess you like

Origin blog.csdn.net/m0_72987309/article/details/130188084