POJ3624 0-1 Backpack (dp+ scrolling array)

Source: Internet
Author: User

Charm Bracelet
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 47440 Accepted: 20178

Description

Bessie have gone to the mall ' s jewelry store and spies a charm bracelet. Of course, she ' d like-to-fill it with the best charms possible from the N (1≤ n ≤3,402) available Char Ms. Each charm I in the supplied list has a weight wi (1≤ wi ≤400), a ' desirability ' factor C5>di (1≤ Di ≤100), and can be used at the most once. Bessie can only support a charm bracelet whose weight are no more than m (1≤ m ≤12,880).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the MA Ximum possible sum of ratings.

Input

* Line 1:two space-separated integers: N and M
* Lines 2. N+1:line i+1 describes charm I with II space-separated integers: Wi and Di

Output

* Line 1: A single integer which is the greatest sum of charm desirabilities so can be achieved given the weight Constrai Nts

Sample Input

4 61 42 63 122 7

Sample Output

23

If using Dp[n][m]: The first n items do not exceed the maximum value of volume m, the state transfer equation is:

DP[I][J] = 0 (i==0orj==0)//no items, backpack volume of the largest value is also 0, and more items, backpack volume of 0 are not fit.

DP[I][J]=DP[I-1][J] (J<d[i])//Current backpack plug no more than the first item, the maximum value and the first i-1 items not greater than the maximum value of volume J is the same.

Dp[i][j]=max (Dp[i-1][j],dp[i-1][j-d[i]]+w[i])//Current backpack If you can load the first item, then the maximum value after loading the article I and the maximum value of the item I do not fit the maximum value

However, because the range of N and M is too large, open two-dimensional array will burst memory, the analysis solution found that the solution Dp[i][j] is only a row above it and a value to the left of the previous row, that is, only the row above it, you can use a scrolling one-dimensional array solution, the new value to the upper position, Note, however, that the order must be right-to-left, or it will overwrite useful values.

1#include <iostream>2#include <cstring>3#include <algorithm>4 using namespacestd;5 intMain ()6 {7     intN, M;8     intd[13010], w[13010];9     intdp[13010];//since solving the row is only related to the previous row, you can use a scrolling array, dp[j] to indicate the maximum value of the first I item without exceeding the volume JTenCIN >> N >>m; One      for(inti =1; I <= N; ++i) A     { -CIN >> D[i] >>W[i]; -     } theMemset (DP,0,sizeof(DP)); -      for(inti =1; I <= N; ++i) -     { -          for(intj = m; J >=1; --J)//solve from right to left, save the newly calculated value in the upper position, because the overridden value is only relevant to the right of the next and lower line +         { -             if(J >=D[i]) +DP[J] = max (Dp[j], Dp[j-d[i]] +w[i]); A             Else       //if j-d[i]<0, which indicates that the capacity of the current backpack is not enough to put the first item, that the maximum value of the pre-I item with the maximum value of the pre-i-1 goods is equal, at                  Break;//also because the left side of J is only smaller because it is evaluated from right to left, so you can skip saving time -                          -         } -     } -cout << Dp[m] <<Endl; -     return 0; in}

POJ3624 0-1 Backpack (dp+ scrolling array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.