poj3624 01 Backpack dp+ scrolling Array charm bracelet
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 25458 |
|
Accepted: 11455 |
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
Test instructions: 01 Backpack Entry Questions
Idea: DP, with two-dimensional array will mle, because each state is only related to the previous state, it can open the rolling array of compressed space
If the decision to place the item I (which can be placed or not) has a capacity of J and a total value of dp[i][j], then
DP[I][J]={DP[I-1][J],DP[I-1][W[I]-J]} (w[i]-j>0)
Boundary Control:
Dp[i][j]=0 (i==0| | j==0)
DP[I][J]=DP[I-1][J] (w[j]-j>0)
/*knapsack Problem dp+ scrolling array*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<ctype.h>using namespacestd;Const intmaxn=14100;Const intInf= (1<< -);intn,m;intW[MAXN],V[MAXN];intdp[2][MAXN];intMain () {scanf ("%d%d",&n,&M); for(intI=1; i<=n;i++) scanf ("%d%d",&w[i],&V[i]); Memset (DP,0,sizeof(DP)); for(intI=0; i<=n;i++){ for(intj=0; j<=m;j++){ if(i==0|| j==0) dp[i%2][j]=0; Else if(j-w[i]<0) dp[i%2][j]=dp[(i+1)%2][j]; Elsedp[i%2][j]=max (dp[(i+1)%2][j],dp[(i+1)%2][j-w[i]]+V[i]); }} printf ("%d\n", dp[n%2][m]); return 0;}
dp+ Scrolling Array
poj3624 01 Backpack Getting Started dp+ scrolling array