hdu2126 class 01 backpack (two-dimensional array of three-dimensional space optimization)

Subject description:

For n given items, each item has a price p [i], m dollars you have, up to the number of items required to buy, and how many different schemes have

Topic analysis:

Similar subject backpack 01, 01 general knapsack problem we encountered was seeking the n items, has a capacity of m, each with w [i] takes the maximum value in the calculated capacity range, the dynamic transfer equation as dp [i] [j] = max (dp [i-1] [j], dp [i-1] [jw [i]] + value [i]), dp [i] [j] before storing i kinds of goods, the maximum capacity value of j, then the optimization space is dp [j] = max (dp [j], dp [jw [i]] + value [i]), and the problem with this analogy to the backpack 01 the context of the topic, this time to every souvenir prices are as of 1, we request that method with the greatest value of a given capacity of m

Originally only require a maximum value is how much more than the equivalent number of storage with a dimension of each i corresponds to a two-dimensional array, when updating the current i-th two-dimensional array, the first i-1 is based on a two dimensional array of the updated values, so in this case need to use the three-dimensional array is stored, the space can be optimized by two-dimensional array, and the second and third layers are required descending cycle, to avoid interference

Apply 01 backpack derivation of the dynamic transfer equation : before optimization, DP [i] [j] [k] denotes the front i items, when there are several dollars j (j is not more than dollars), you can purchase items of k , DP [i] [J] [K] = DP [i-. 1] [J] [K] + DP [i-. 1] [JP [i]] [K-. 1] , (front i-th article j element, several buy k-th item is a front i-1 th item j membered buy k-th (i-th not buy) or the i-1 th article JP [i] yuan for several k-1 items of ( Buy i-th) ), optimized space dp [j] [k] = dp [j] [k] + dp [jp [i]] [k-1]

Note that the initialization dp [i] [0] is 1, which means that i 0 kinds of dollars for options article is not to buy one species

Code:

 1 #include<iostream>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<cmath>
 6 using namespace std;
 7 
 8 int p[35];
 9 int dp[505][35];
10 
11 int main(){
12     int t;
13     scanf("%d", &t);
14     while(t--){
15         int n, m;
16         scanf("%d%d", &n, &m);
17         for(int i = 1; i <= n; i++) scanf("%d", &p[i]);
18         sort(p+1, p+n+1);
19         int sum = 0;
20         int ma = 0;
21         for(int i = 1; i <= n; i++){
22             if(sum + p[i] <= m){
23                 sum += p[i];
24                 ma = i;
25             }
26         }
27         memset(dp, 0, sizeof(dp));
28         for(int i = 0; i <= m; i++) dp[i][0] = 1;
29         for(int i = 1; i <= n; i++){
30             for(int j = m; j >= p[i]; j--){
31                 for(int k = ma; k >= 1; k--){
32                     dp[j][k] += dp[j-p[i]][k-1];
33                 }
34             }
35         }
36         if(ma != 0){
37             printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n", dp[m][ma], ma);
38         }else{
39             printf("Sorry, you can't buy anything.\n");
40         }
41     }
42     return 0;
43 }

 

Guess you like

Origin www.cnblogs.com/findview/p/11888388.html
Recommended