poj3624 Charm Bracelet

http://poj.org/problem?id=3624

 

Topic: Bessie went to the jewelry store in the mall and found a charming bracelet. Of course, she wants to wear the best charms (N (1≤N≤3402) available charms). Each charm provides a list with a weight Wi (1≤Wi≤400), a "wish" factor Di (1≤Di≤100), and can be used at most once. Bessie can only support one charm bracelet that weighs no more than M.

*Line 1: Two space-separated integers: N and M.

*Lines 2...N+1: Line i+1 describes the charm i with two spatially separated integers: Wi and Di. Considering weight limits as a constraint, and enumerating their weight and desirability for charms, deduce the maximum possible value for charms.

 

That is to say, this problem is the 01 knapsack problem. If x[ i ] is 1, it means that the i-th jewelry is put into the bracelet, and if it is 0, it is not put in the bracelet. Then the original problem becomes the constraint condition that we can use dynamic Planning, backtracking, branch and bound, etc. to solve this problem. Dynamic programming is used here.

 

Algorithmic Ideas: Dynamic Programming

a)  Abstract the knapsack problem (X1, X2, . volume and weight);

b)  Establish a model, that is, find max(D1X1+D2X2+…+DnXn);

c)  Constraints, W1X1+W2X2+…+WnXn<=M;

d)  Define V(i,j): the current backpack capacity j, the value corresponding to the best combination of the first i items;

e) The principle of optimality is the basis of dynamic programming, and the principle of optimality refers to "the optimal decision sequence of a multi-stage decision-making process has the property that no matter what the initial state and initial decision are, for a certain state caused by the previous decision In other words, the decision sequence of subsequent stages must constitute the optimal strategy.” To judge whether the problem satisfies the principle of optimality, prove by contradictory method:

Assuming that (X1, X2, ..., Xn) is the optimal solution of the 01 knapsack problem, then (X2, X3, ..., Xn) is the optimal solution of its sub-problems,

Assuming that (Y2, Y3, ..., Yn) is the optimal solution of the sub-problem of the above problem, it should be (D2Y2+D3Y3+...+DnYn)+D1X1 > (D2X2+D3X3+...+DnXn)+D1X1;

And (D2X2+D3X3+…+DnXn)+D1X1=(D1X1+D2X2+…+DnXn), then (D2Y2+D3Y3+…+DnYn)+D1X1 > (D1X1+D2X2+…+DnXn);

This formula shows that (X1, Y2, Y3, ..., Yn) is the optimal solution to the 01 knapsack problem, which is consistent with the initial assumption that (X1, X2, ..., Xn) is the optimal solution to the 01 knapsack problem. Contradiction, so the 01 knapsack problem satisfies the principle of optimality;

f)  Looking for the recurrence relation, there are two possibilities for the current commodity:

First, the capacity of the backpack is smaller than the volume of the product, and it cannot hold it. The value at this time is the same as the value of the first i-1, that is, V(i,j)=V(i-1,j);

Second, there is still enough capacity to install the product, but it may not reach the current optimal value, so choose the optimal one between installing and not installing, that is, V(i,j)=max{V (i-1,j), V(i-1,jW(i))+D(i)}

Among them, V(i-1,j) means not loaded, V(i-1,jW(i))+D(i) means the i-th commodity is loaded, the backpack capacity is reduced by W(i) but the value is increased by v( i);

From this, the recurrence relation can be derived:

1) j<W(i)      V(i,j)=V(i-1,j)

2) j>=W(i)     V(i,j)=max V(i-1,j)V(i-1,j-W(i))+D(i)

This can be solved, but you will find that the submission space will overflow and not satisfied, so we need to optimize it.

g)  Space optimization, each time the value of V(i)(j) changes is only related to V(i-1)(x) {x:1...j}, V(i-1)(x) is the former The value saved by an i loop;

Therefore, V can be reduced to a one-dimensional array to achieve the purpose of optimizing the space, and the state transition equation is converted into  result(j)= max{result(j), result(jW(i))+D(i)} ;

And, for the state transition equation, each derivation of V(i)(j) is derived from V(i-1)(jw(i)), so the scanning order of j in the one-dimensional array should be from large to small (M to 0), otherwise the value saved in the previous loop will be modified, resulting in an error.

h) However, the disadvantage is that although the dynamic programming space is optimized, this method cannot find the solution composition of the optimal solution, because the dynamic programming to find the solution composition must go back to find the solution on the premise of determining the optimal solution. The optimized dynamic programming only uses one-dimensional arrays, and the previous data has been overwritten, so there is no way to find them, so the two methods have their own advantages. But this commit can pass.

 1 #include <iostream>
 2 using namespace std;
 3 #define MAXN 3403
 4 #define MAXM 12881
 5 int w[MAXN];
 6 int d[MAXN];
 7 int result[MAXM];
 8 int main()
 9 {
10     int N;
11     int M;
12     cin >> N >> M;
13 
14     for (int i = 1; i <= N; i++)
15     {
16         cin >> w[i] >> d[i];
 17      }
 18      for ( int tempN = 1 ; tempN <= N; ++tempN) // for the tempNth item 
19      {
 20          for ( int tempM = M; tempM >= w[tempN]; tempM--) // backpack capacity from large to small 
21          {
 22              if (result[tempM] <= result[tempM - w[tempN]] + d[tempN]) // comparison with not loaded value 
23              {
 24                  result[tempM] = result[tempM - w[tempN]] + d[tempN]; // 2D becomes 1D 
25              }
 26         }
27     }
28     cout << result[M] << endl;
29 
30     return 0;
31 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325303406&siteId=291194637