Hdu 2602 (dp), hdu2602dp
Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2602
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 32499 Accepted Submission (s): 13379
Problem descriptionpolicyears ago, in Teddy's hometown there was a man who was called "Bone Collector ". this man like to collect varies of bones, such as dog's, cow's, also he went to the grave...
The bone collector had a big bag with a volume of V, and along his trip of collecting there are a lot of bones, obviusly, different bone has different value and different volume, now given the each bone's value along his trip, can you calculate out the maximum of the total value the bone collector can get?
InputThe first line contain a integer T, the number of instances.
Followed by T cases, each case three lines, the first line contain two integer N, V, (N <= 1000, V <= 1000) representing the number of bones and the volume of his bag. and the second line contain N integers representing the value of each bone. the third line contain N integers representing the volume of each bone.
OutputOne integer per line representing the maximum of the total value (this number will be less than 231 ).
Sample Input1 5 10 1 2 3 4 5 4 3 2 1
Sample Output14: give you the size of a pack, the value of each bone and the volume occupied, and find the value that can be placed into the maximum value solution. Analysis: the simple 01 backpack is purely a template question. It is also my first question backpack, And I directly paste the code. 1 # include <cstdio> 2 # include <cmath> 3 # include <cstring> 4 # include <algorithm> 5 using namespace std; 6 7 // dp [I] [j] indicates the maximum value of placing the I-th bone and occupying j size, 8 // c [I] indicates the size of the I-th bone 9 // w [I] indicates the value of the I-th bone 10 11 int dp [1111] [1111], c [1111], w [1111]; 12 int T, N, V; 13 14 int main () 15 {16 int I, j; 17 scanf ("% d ", & T); 18 while (T --) 19 {20 scanf ("% d", & N, & V); 21 for (I = 1; I <= N; I ++) 22 scanf ("% d", & w [I]); 23 for (I = 1; I <= N; I ++) 24 scanf ("% d", & c [I]); 25 memset (dp, 0, sizeof (dp); 26 for (I = 1; I <= N; I ++) 27 {28 for (j = 0; j <= V; j ++) 29 {30 dp [I] [j] = dp [I-1] [j]; // here we mainly consider that when j is smaller than c [I], the I-th bone 31 if (j> = c [I]) cannot be placed. 32 dp [I] [j] = max (dp [I-1] [j], dp [I-1] [j-c [I] + w [I]); 33} 34} 35 printf ("% d \ n", dp [N] [V]); 36} 37 return 0; 38}View Code