01 knapsack problem--Dynamic programming solution

Source: Internet
Author: User

01 Knapsack Problem Specific Example: assume an existing capacity of 10kg backpack, in addition there are 3 items, respectively for A1,A2,A3. Item A1 weight is 3kg, value is 4, item A2 weight is 4kg, value is 5, item A3 weight is 5kg, value is 6. What items are placed in the backpack to make the total value of the backpack the most?

This problem has two methods, dynamic programming and greedy algorithm. This article deals with dynamic planning only.

Do not apply the specific definition of dynamic planning, try to think, encounter this topic, how to solve?

The first thought, generally is the poor lifting method, a test, for a small number of examples of the application, if the capacity increases, the increase in items, this method is useless.

Second, the most valuable objects can be put into the first, which is already the embryonic form of greedy algorithm. If you do not add certain conditions, the results may not be feasible.

Finally, it is the idea of dynamic programming. First generalize the original problem, want the backpack can obtain the total value, that is, the first I object into the capacity of the maximum value of the backpack (kg) c[i][m]--use an array to store the maximum value, when m takes 10,i 3 o'clock, that is the original problem. And the first I object into a capacity of M (kg) backpack, and can be converted into a front (i-1) objects into the backpack problem. The following uses mathematical expressions to describe the specific relationship between them.

The specific meaning of each symbol in an expression.

W[i]: The weight of the first object;

P[i]: The value of the first object;

C[I][M]: The maximum value of the first I object into the backpack with a capacity of m;

C[I-1][M]: The maximum value of the first I-1 object into a backpack of capacity m;

C[i-1][m-w[i]]: The maximum value of a backpack with a i-1 in capacity of m-w[i];

This can be done by:

C[i][m]=max{c[i-1][m-w[i]]+pi, C[i-1][m]} (will give a more specific explanation)

According to the above formula, the number of objects and the weight of the backpack is recursive, listing a table (see table below), table from (Http://blog.csdn.net/fg2006/article/details/6766384?reload), when the table in the gradual introduction of the size of each value , that's the greatest value to ask for. In the derivation process, note that it is better to start the derivation line by row rather than by column, first from the line numbered 1, the value of all C[1][m], and then the size of the row c[2][m] that is numbered 2. This is easy to understand.

    

Dynamic Programming "positive solution"

There are n items and a backpack with a capacity of V. The volume of article I is c[i], the value is w[i]. The sum of the values is maximized by solving which items are loaded into the backpack.
State transition equation:
F[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}
This equation is very important, and basically all the equations for knapsack-related problems are derived from it.
Pseudo code:
For I=1..N
For V=v. 0
F[v]=max{f[v],f[v-c[i]]+w[i]};
If you do not put the article I item, then the problem is converted to "the first I-1 items into a backpack with a capacity of V",
Value of f[i-1][v];
If you put the article I item, then the problem translates into "The first I-1 items into the remaining capacity of v-c[i] in the backpack",
The maximum value that can be obtained at this time is f[i-1][v-c[i]] plus the value obtained by placing the item in article I w[i].


② Example Two:
Drug harvesting

Time limit:1000ms Memory limit:65535kb
submissions:155 accepted:50


Description is a gifted child, his dream is to become the world's greatest physician. To this end, he wanted to worship the most prestigious physician in the vicinity as a teacher. In order to judge his qualifications, the physician gave him a difficult problem. The physician took him to a cave that was full of herbs, and said to him, "children, there are some different herbs in this cave, and it takes time to take each strain, and each plant has its own value." I will give you some time, during which time you can pick up some herbs. If you are a smart kid, you should be able to get the total value of the herbs that are picked up. ”
If you are Chen Chen, can you finish this task?
The first line of input inputs has two integers t (1 <= T <= 1000) and M (1 <= m <= 100), separated by a space, T represents the total amount of time that can be used to pick up the medicine, and M represents the number of herbs in the cave. The next M-line consists of two integers from 1 to 100 (including 1 and 100), each representing the time of picking a herbal herb and the value of the herb.
The output outputs include a row that contains only an integer that represents the maximum total value of the herbs that can be picked up within the prescribed time.
Sample Input
70 3
71 100
69 1
1 2
Sample Output
3

#include <iostream># include<cstring># define MAX (A, B) a>b?a:busing namespace Std;int main () {    int dp [101] [1001],m,t,w[101],val[101],i,j;    cin>>t>>m;    for (i=1;i<=m;i++)        cin>>w[i]>>val[i];    Memset (Dp,0,sizeof (DP));    for (i=1;i<=m;i++)     for (j=0;j<=t;j++)//j is equivalent to the V-c[i "         {    if (J>=w[i])        Dp[i][j]=max (dp[i-1 ][j],dp[i-1][j-w[i]]+val[i]);//release vessel is not put the choice    else dp[i][j]=dp[i-1][j];     }     cout<<dp[m][t]<<endl;     

01 knapsack problem--Dynamic programming solution

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.