http://poj.org/problem?id=3624
Charm Bracelet
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 29444 |
|
Accepted: 13198 |
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
This is a simple 01 backpack.
Start your backpack Journey
The normal backpack is
I 1....N
J m ..... 0
Dp[i][j]=max (Dp[i-1][j],dp[i-1][j-w[i]]+v[i]);
But this range is bigger.
If you use two-dimensional hyper-memory
can be transformed into one-dimensional
Dp[j]=max (Dp[j],dp[j-w[i]]+v[i]);
#include <stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<algorithm>#include<iostream>using namespacestd;#defineN 3500intdp[13000];intMain () {intN,m,w[n],v[n]; while(SCANF ("%d%d", &n,&m)! =EOF) { for(intI=1; i<=n;i++) scanf ("%d%d",&w[i],&V[i]); Memset (DP,0,sizeof(DP)); for(intI=1; i<=n;i++) { for(intj=m;j>=0; j--) { if(j>=W[i]) dp[j]=max (dp[j],dp[j-w[i]]+V[i]); }} printf ("%d\n", Dp[m]); } return 0;}
Charm bracelet-poj3624 (01 backpack)