Knapsack problem--------max value of K

Source: Internet
Author: User

This algorithm is poisonous, accidentally stained on the undead also peeling!

In the knapsack problem this to ask the K big value to harass me all day, make me uneasy, feel uncomfortable--!

I see this kind of writing is the original dp[x] plus one dimension to dp[x][y],x to determine the current backpack capacity, Y is the Y value, respectively, the record from the largest to the first K, (because only ask K, so those smaller values will not be recorded)

Next is the discussion of this dp[x][y], which can be understood as It is the value of the first Y when the backpack capacity is X , while dp[x][] can be understood as the value of all backpack capacity X , so it is easy to understand the following.

The 01 knapsack formula used in general knapsack problem solving is aboutDp[x]=max (dp[x],dp[x-v]+w), this is a simplified version, the original two-dimensional switch to one-dimensional, have done 01 of the backpack know, and then add "K value" this condition, then changed into a dp[x][k]={dp[x][1], dp[x-v][1]+w ,dp[x][2], dp[x-v][2]+w, .... , Dp[x][k], dp[x-v][k]+w} Such a collection, do not understand? is for each dp[x][k] can be obtained two values, one is not put the current item s (V,W) <v is the capacity, W is value > put in the value of dp[x][k], and put this item in the value of Dp[x-v][k]+w, the second value of the moral refers toforthe volume after placing this item just to reach The capacity value of the backpack currently under considerationof theBackpackineach of the valuesall put in itemsThe resulting value, because dp[x][] records all the values that a backpack with a capacity of X can get, so understand? Let's draw a picture! I like to use the diagram to speak, I learn the algorithm for all things with the figure is surprisingly fast, quack!



Again, dp[x][] records all the data that can be contained in a backpack with a capacity of X, and retains only the first k when it asks for a large value of K.

Knapsack problem--------max value of K

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.