Hihocoder (1038,1043) 01 backpack with full backpack

Source: Internet
Author: User

Dynamic planning is always feel more vague things, although generally know what is the same thing, but from the flexible application is still far, but it seems that the dynamic planning of the game in particular, the two classic problems can only be considered a good learning dynamic programming model. But the beginning of everything is difficult, the key is to calm down to practice more.

01 Backpack's state-shifted :F (i, j) = Max{f (I-1, J), F (i-1, j-need[i]) +value[i]}

Directly in accordance with the above state transfer request two-dimensional array to operate, the pseudo-code is as follows:

Fori:1..n

F (0,j) = 0

For I:1..N

For J:0..M

if (J < Need (i)) f (i, j) = f (i-1, J)

    Else F (i, j) = Max{f (I-1, J), F (i-1, j-need[i]) +value[i]}  

But this writing, the spatial complexity is very large, because there is a very large two-dimensional array, but carefully observe the formula, or draw on the paper lattice, you will find that every time the calculation required is only i-1 rows of data, so it is easy to think of two lines of one-dimensional array can be used alternately, but, then carefully observed, You will find that each calculation is using the upper-left data, so that is, if the inverse is calculated:

For I:1..N

For J:m. Need (i)

F (j) = Max{f (J), F (j-need (i)) +value (i)}

It will not overwrite the "top left" data, and can successfully complete the calculation of the entire state transfer amount.

Code is actually the state transfer, so the problem of dynamic planning, as long as the correct state transfer can be listed, from the AC is not far away.

Impl:

1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6     intn,m;7CIN >> N >>M;8     int*need =New int[N];9     int*value =New int[N];Ten      for(inti =0; i < N; ++i) OneCIN >> Need[i] >>Value[i]; A     int*DP =New int[M]; -      -      for(inti =0; i < M; ++i) theDp[i] =0; -      for(inti =0; i < N; ++i) -          for(intj = m1; J > Need[i]; j--) -DP[J] = max (Dp[j], dp[j-need[i]]+value[i]); +          -cout << dp[m-1] <<Endl; +          A          at     Delete[] need; -     Delete[] value; -     Delete[] DP; -          -     return 0; -}
View Code

state-shifting of the complete backpack :F (i, j) = Max{f (I-1, J), F (i, j-need[i]) +value[i]}

I can take any of the items in the description,

Similarly, if you apply a two-dimensional array directly to the state transfer, the pseudo-code is as follows:

Fori:1..n

F (0,j) = 0

For I:1..N

For J:0..M

if (J < Need (i)) f (i, j) = f (i-1, J)

    Else F (i, j) = Max{f (I-1, J), F (i, j-need[i]) +value[i]}  

Obviously also can optimize the spatial complexity, directly from the array computation, each computation f (i,j) needs the bank left side of the data, therefore compared with 01 knapsack, this time only need to be counted. In fact, it can be understood that 01 of the backpack in the loop to write backwards, is to respond to each item can only be used once this condition, and the complete backpack, the item is unrestricted, so the next state must consider its front sub-state, so the loop is positive.

For I:1..N

For J:need (i): M

F (j) = Max{f (J), F (j-need (i)) +value (i)}

Impl:

1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6     intn,m;7CIN >> N >>M;8     int*need =New int[N];9     int*value =New int[N];Ten      for(inti =0; i < N; ++i) OneCIN >> Need[i] >>Value[i]; A     int*DP =New int[M]; -      -      for(inti =0; i < M; ++i) theDp[i] =0; -      for(inti =0; i < N; ++i) -          for(intj = Need[i]; J < M; ++j) -DP[J] = max (Dp[j], dp[j-need[i]]+value[i]); +          -cout << dp[m-1] <<Endl; +          A          at     Delete[] need; -     Delete[] value; -     Delete[] DP; -          -     return 0; -}
View Code

Hihocoder (1038,1043) 01 backpack with full backpack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.