dp (01 knapsack problem)

Miller says the story last week, the small and the small Hi Ho strenuous efforts finally got the vast number of lotteries! And now, finally time to receive the reward of a small Ho!

Ho now have small hands of M sheets lottery, prize and the prize area has N parts, respectively numbered 1 to N, where the i-th member need prizes need (i) redeem lottery sheets, and also can be redeemed once, in order to make hard the lottery does not get wasted, Ho small prizes to each have got points, including the first prize of i pieces score value value (i), he expressed his preference value of this piece of the prize. Now he wanted to know, by virtue of these lotteries in his hand, which can be exchanged for prizes, prizes preferences so that these values ​​and to the greatest.

Tip one: abstract reasonable question, the definition of the state is the most critical step Dynamic Programming

Tip two: told you reduce the time consumption, we look at how to reduce space consumption

Input

Each test point (input file) and only a set of test data.

Number of the first two acts of each test positive integers N and M, it represents a prize, and a small number of lottery Ho hands.

The following description n rows each row describes a prize, wherein the behavior of two integers i need (i) and the value (i), as previously described meaning.

Ensure that test data

To 100% of the data, the value of N is not more than 500, the value of M is not more than 10 ^ 5

To 100% of the data, need (i) no more than 2 * 10 ^ 5, value (i) does not exceed 10 ^ 3

Sample Input

5 1000
144 990
487 436
210 673
567 58
1056 897

Sample Output

2099
Output

For each test case, the output of an integer Ans, Ho represents the small total preference value can be obtained.

#include <the iostream> 
#include <the iostream> 
#include <cstdio> 
#include < String > 
#include <CString> 
#include <algorithm> 
#include <stdio.h> 
#include < String .h> 
#include <Vector> 
# the include < SET >
 the using  namespace STD;
 int    V [ 509 ], W [ 509 ], DP [ 509 ] [ 100009 ]; // start array ML results are large one and two dimensional arrays.
// Later, a large two-dimensional array of small wa a result. 

int main()
{
    int n , bag ;
    while(cin >> n >> bag)
    {
        memset(dp , 0 , sizeof(dp)); //初始化为0 
        //memset(v , 0 , sizeof(v));
        //memset(w , 0 , sizeof(w));
        for(int i = 1 ; i <= n ; i++)
        {
            cin >> w[i] >> v[i];
        }
        for(int i = 1 ; i <= n ; i++)
        {
            for(int= J . 1 ; J <= Bag; J ++ ) 
            { 
                IF (J> = W [I]) // When the Backpacks capacity to meet the volume of the product 
                {
                     // note ultra array boundary dp 
                    dp [i] [j] = max ( DP [I - . 1 ] [J], DP [I - . 1 ] [J - W [I]] + V [I]); // value of the product and does not take a maximum value of the product taken 
                }
                 the else 
                    DP [I ] [J] = DP [I - . 1 ] [J]; // backpack volume smaller than the volume of the product, can only take on a maximum value of items 
            } 
        } 
        COUT << DP [n-] [Bag] << endl; 
    } 

    return  0;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11228441.html