2.01 knapsack problem

There  N N items and a capacity  V backpack of V. Each item can only be used once.

The first  i Volume items i is  V i VI, the value is  W i Wi.

Which solving the items into the bag, the total volume of these items can not exceed the capacity of the backpack, and the total value of the maximum.
The maximum output value.

Input Format

The first line of two integers, N , V N, V, separated by spaces, respectively, and the number of articles backpack volume.

Then there are  N N rows, each row two integers  V i , W i VI, Wi, separated by spaces, each represents  i Volume i and value of items.

Output Format

Output An integer representing the maximum value.

data range

0 < N , V 1000 0 <N, V≤1000
0 < v in , w in 1000 0 <we, wi≤1000

SAMPLE INPUT

4 5
1 2
2 4
3 4
4 5

Sample output:

8


The first version of the two-dimensional dynamic programming:
Thinking:
Yan analysis DP formula:
into two parts:
Part I: state represents F [I] [J]:
(divided into two steps --->)
(1) set : All the selected method and the selection condition ----> the title from only the i-th articles selected, article and the total volume or less J
(2) into properties ---> maximum, minimum (and sometimes the two-dimensional border issues need to be considered, like the initialization positive infinity), the number of
the second portion is calculated state ------> set partitioning:

this question difficult

points and containing free i ----- i> with i-free f [i - 1] [j ], i containing a f [i - 1, (j - v [i]) + w [i]] indicates
. 1 #include <the iostream>
 2 #include <algorithm>
 . 3  
. 4  the using  namespace STD;
 . 5  
. 6  const  int N = 1010 ;
 . 7  
. 8  int n-, m;
 . 9  int V [N], W [N];
 10  int F [N ] [N];
 . 11  int main () {
 12 is      CIN >> >> n- m;
 13 is      for ( int I = . 1 ; I <= n-; I ++) CIN >> V [I] >> W [I];
 14      
15      // number of items and the volume of the backpack 
16      for (int i = . 1 ; i <= n-; i ++ )
 . 17          for ( int J = 0 ; J <= m; J ++ )
 18 is          {
 . 19              // free of the i-th item, and articles comprising the i-th, the i-free the presence of a certain article, but the article does not necessarily contain the i-th presence of
 20              // because we add the volume of the i-th article may be greater than the volume of our backpack (j <vi), so that the right may be the empty set
 21              // the first i-1 must exist all items, i inside the preexisting 
22 is              F [i] [J] = F [i - . 1 ] [J];
 23 is              // in the original volume number i-1 of this article, in determining whether we could be replaced with the i-th basis of the volume of articles in the volume of the first i-1 with the article
 24              // make maximum utilization of our backpack, the backpack is an article in the determination of the number of i j without exceeding the capacity of the largest value of the items can hold
 25              // is the number
 26              //Backpack without exceeding the volume of j, and use the current value and a value compared to
 27              // F [I] [j] denotes the current state is represented by our backpack 
28              IF (j> = V [I]) F [I] [J] = max (F [I] [J], F [I - . 1 ] [J - V [I]] + W [I]);
 29          }
 30      COUT << F [n-] [m ];
 31 is      
32      
33 is }

 

Guess you like

Origin www.cnblogs.com/luyuan-chen/p/11615687.html