Dynamic Programming - Getting Started knapsack problem

Dynamic Programming - Getting Started knapsack problem


1.01 Backpack


Outline

Gives the N items, each object having a certain volume and value, and each has only one object. The backpack has a volume V, asking how the package should be installed to make the total value of the largest objects in the backpack?

Problem-solving ideas

Knapsack problem is a typical dynamic programming class title, and dynamic planning is typical of the method by law to identify positive solutions. So the key is how to problem-solving ideas to find relationships between different data (state transition).

Direct description is not convenient to explain our example as an example:

(Note: 01 knapsack problem does not involve objects because of the shape and size of the problem and how to put the backpack, simply consider the volume and the value we can be understood as each object is liquid, and this is a big backpack tank (of course. , each liquid poured into either all, or that does not fall, do not allow down part.)


例:HDU2602 Bone Collector

Problem

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

Simple Definition

A long time ago, in Teddy's home it has been called a "bone collector" of the people. The people like to collect all kinds of bones - dog, cow, of course, he would often go to the cemetery around ...... (a wolf off)
the person has a total capacity of backpacks V, and in his travels he has a lot of bones to be collected. And obviously, different bones have different size and different values. Now given every bone he found what are, calculate which one can put into the bag, to collect the maximum value of the bones is how much?

Input

The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Input Format

Input of the first row contains an integer T, representative of the number of different cases (sets of data).
T followed a case where three lines, the first line contains two integers N, V (N <= 1000 , V <= 1000) representative of the numbers and the bone capacity backpack. The second line contains N represents an integer value of each bone. The third line contains N integers representative of the volume of each bone.

Output

One integer per line representing the maximum of the total value (this number will be less than \(2^{31}\)).

Output Format

Each line an integer representing the total value of the maximum value (this number is not greater than \ (2 ^ {31} \) ).

SAMPLE INPUT

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14

Topic analysis

Dynamic programming, which requires a variety of possible local solutions, find those local solution to achieve the best possible solution and to find the optimal solution, and give up other solutions, in order to achieve "major issues to minor ones out." .
To this question, this wolf-pack has destroyed thousands of ways, there are thousands of local solution, but we need to find is the largest value of that kind, but this one is thousands one kinds of ways, which is a topical solution, and this one is the optimal solution.

We give a set of data:

1
3 5
5 12 20
1 2 4

First, we can save the data:

The wolf off the volume of each bone is v [b], the value is w [b], compiled a number to each bone is b, we have already declared good:

    int T;
    scanf("%d",&T);
    for(int c=1;c<=T;c++)
    {
        int N,V;
        scanf("%d%d",&N,&V);
        for(int b=1;b<=N;b++)
            scanf("%d",&w[b]);
        for(int b=1;b<=N;b++)
            scanf("%d",&v[b]);
        }

His selection process of how to install the package, is his decision making process.

First to see him take the first bone:
the value of a piece of bone 5, the volume is 1.
We only have this one bone, then no matter how much our backpacks, we can only put it into a piece of bone. Then there is such a relationship:

total value 1 2 3 4 5 6 7 ...... Backpack volume
1 5 5 5 5 5 5 5 ......
No bones

Now the wolf off and picked up his second bone: then his backpack if the volume is greater than 3, then he can put into the two bones:

-
his backpack volume 1, he can only put a piece of bone 1, the value is 5, which is the greatest value;
his backpack volume 2, he can put a piece of bone 2, the value is 12, is the greatest value;
his backpack volume was 3, he can put a piece of bone 2 and a bone 1, the value is 17, is the greatest value;
his backpack volume was 4, he still can only put a piece of bone 2 and a bone 1, the value is 17, the value of maximum ;
then his backpack volume no matter how big, he can only put a bone in a bone 1 and 2, so the future value is 17:

total value 1 2 3 4 5 6 7 ...... Backpack volume
1 5 5 5 5 5 5 5 ......
2 5 12 17 17 17 17 17 ......
No bones

Then he came out of his third bones: Volume 4, the value of 20.

-
his backpack volume 1, he can only put a piece of bone 1, the value is 5, which is the greatest value;
his backpack volume 2, he can put a piece of bone 2, the value is 12, is the greatest value;
his backpack volume was 3, he can put a piece of bone and bone 2 1, the value is 17, is the greatest value to this are the same as before.
His backpack volume was 4, he can put a bone 3, that value is 20, the largest of;
his backpack volume was 5, he can put a piece of bone 3 plus a bone 1, which is 25;
his backpack volume 6, he can put a bone 3 plus a bone 2, which is 32;
his backpack volume 7, he can put a piece of bone 3, a bone 2, a bone 1, it is 37;
later, he It means that in any case only three bones, 37 are back.

total value 1 2 3 4 5 6 7 ...... Backpack volume
1 5 5 5 5 5 5 5 ......
2 5 12 17 17 17 17 17 ......
3 5 12 17 20 25 32 37 ......
No bones

……

Since there is such a table, we can put the table down. Let an array:

int dp[i][j];//在这里,表示考虑到前i块骨头,背包体积为j,可以获得的最大价值。那么dp[i][j]就是这种情况下的最大价值。

This array is then:

dp[i][j] 1 2 3 4 5 6 7 ...... j
1 5 5 5 5 5 5 5 ......
2 5 12 17 17 17 17 17 ......
3 5 12 17 20 25 32 37 ......
i

At first glance it seems to be no law, and the next is the key - find out the law in this data table, which is the essence of dynamic programming: the derivation of the state transition equation - that is, the wolf off the decision making process.

However, this wolf off to get a piece of bone i, he needs to consider the issue only his piece of bone i would want to put in the package.
If he decided to hold it over his decision it is: dp [i] [J] = dp [i-1] [j];
- bag thing has not changed, and [i-1] [j] when the package it's the same;

If he decided to put, then he is finished Decision: dp [i] [J] = dp [i-1] [jv [i]] + w [i];
- he put the bag into a number i bones, so this time the bag will have a piece of bone number i, while the volume has also been taken up v [i], but worth more w [i] - then the other way round, he went into after dp [i] [j] ---- "is that he did not put i, jv volume is that state [i] i plus the time value" -! !

(Simple things get a bit burned out letters of the brain ... QAQ)

So if he decided to hold, then the state [i] [j] is transferred from the [i-1] [j] over there;
if he decided to put, then the state [i] [j] is the [i-1] [jv [i]] transferred from there;
he put a hold all right, and we're looking for maximum value, so this time the state is -

dp[i][j]=max(dp[i-1][j-v[i]]+w[i],dp[i-1][j]);//Note:j>=v[i];

This is the state transition equation of solving the problem, then we have to put it into the program as long as there is OK:

#include<cstdio>
#include<algorithm> //max()的头
using namespace std;
int dp[1080][1080];
int w[1080],v[1080];
int main()
{
    int T;
    scanf("%d",&T);
    for(int c=1;c<=T;c++)
    {
        int N,V;
        scanf("%d%d",&N,&V);
        for(int b=1;b<=N;b++)
            scanf("%d",&w[b]);
        for(int b=1;b<=N;b++)
            scanf("%d",&v[b]);
        for(int i=1;i<=N;i++)//i个物品
            for(int j=0;j<=V;j++)//背包体积为j——转两个循环列举各个情况的状态,并用不同的状态进行转移:
            {
                if(j>=v[i])//使用状态转移方程
                    dp[i][j]=max(dp[i-1][j-v[i]]+w[i],dp[i-1][j]);
                if(j<v[i])//如果此时背包的体积比这个物体的体积小,那无论如何也放不进去,所以走“不放”的状态转移
                    dp[i][j]=dp[i-1][j];
            }
        printf("%d\n",dp[N][V]);
    }
    return 0;
}

……


You can look out: he has to decide how to put, so he's got a million decisions a variety of multiple, which in this state will certainly find the optimal tired S.
And we start with him only one bone, two bones deduced, three bones ...... contact them and find out, you can find him how to put N bones should, then we will be very easy to continue the transfer process in finding the optimal solution, which is the dynamic programming of "major issues to minor ones out," the essence of thinking.


Guess you like

Origin www.cnblogs.com/izwb003/p/dynamic-programming_knapsack-problem.html