Basic algorithm (vii)--Dynamic Programming "0/1 knapsack problem"

Source: Internet
Author: User

First of all, for the dynamic planning, I would like to make a brief introduction, I believe you can understand. Dynamic programming (programming) is a branch of operations research and a mathematical method for the optimization of decision-making processes (decision process). First, one of the simplest examples (primary level of Olympiad):

This is a simulation of the dynamic planning Method!

Okay, now, let's get to know the simplest example of dynamic planning:0/1 backpacks .

"Description of the problem" a traveler is going out of town and going to the store to prepare things. In the store, there are N-like items, each with its own volume, but only one for each item. The traveler has a backpack that restricts the contents of a V-volume, but the traveler is more wealthy and wants to buy something expensive. Please program to find out: which items are loaded into the backpack so that the sum of the cost of these items does not exceed the Backpack volume V, and the value of the largest sum.

At first sight, it feels like a direct simulation of violence. However, when the amount of data is very large, this method does not seem to be very useful. So, we are going to use more space to change time.

Array Dp[i,j] Indicates the maximum value that can be obtained from the first I items in a backpack of volume J. The array with the volume of each item is C, and the value array is W

Then a state transition equation can be listed as: Dp[i,j]:=max{dp[i-1,j],dp[i-1,v-c[i]+w[i]}.

So just a little enumeration can be done. It's actually a bit like a memory search.

"Reference program"

1 var2C,p:Array[1.. +] ofLongint;3 I,j:longint;4F:Array[1.. +,1.. +] ofLongint;5 N,m,x:longint;6 Function Max (A,b:integer): Longint;7 begin8   ifA>b Thenmax:=a9     Elsemax:=b;Ten End; One  A BEGIN - readln (n,m); -    fori:=1  toN Do the readln (C[i],p[i]); -    fori:=1  toN Do -      forj:=1  toM Do -       ifJ>=c[i] ThenF[i,j]:=max (f[i-1, j],f[i-1, j-c[i]]+P[i]) +         Elsef[i,j]:=f[i-1, j]; - Writeln (f[n,m]); +END.

In fact, the space can be further optimized, as follows: note the second cycle is Downto

1 varA:Array[0..100000] ofLongint;2V,p,w,c:Array[0.. -] ofLongint;3 N,m,i,j:longint;4 begin5READLN (M,n);{m represents the number of items to choose from, and the volume of n packets}6    fori:=1  toM Do7Read (V[i],w[i]);{V[i],w[i] represents the volume and value of article I items respectively}8    fori:=1  toM Do9    forJ:=nDowntoV[i] Do {changes in volume from the first item to the article I item}Ten     ifa[j]<a[j-v[i]]+W[i] One        ThenA[j]:=a[j-v[i]]+w[i];{Compare the value of the items placed in J-1 and put in J pieces} A Writeln (A[n]); - End.

Basic algorithm (vii)--Dynamic Programming "0/1 knapsack problem"

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.