Many years 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 Input
15 101 2 3 4 55 4 3 2 1
Sample output
14
1 /************************************** * *********************************** 2> File Name: 0-1.cpp 3> author: mercu 4> mail: [email protected] 5> created time:, Sunday, July 20, 2014, 6 ******************************* **************************************** */7 8 # include <iostream> 9 # include <string. h> 10 using namespace STD; 11 12 int t [1005], V [1005]; 13 int f [1005]; 14 int Ozzy (int A, int B) 15 {16 17} 18 int main () 19 {20 int o, A, I, B, C; 21 int maxv; 22 CIN> O; 23 while (o --) 24 {25 Int J, K, L; 26 CIN> A> B; 27 memset (v, 0, sizeof (v); 28 memset (T, 0, sizeof (t); 29 memset (F, 0, sizeof (f); 30 for (I = 1; I <= A; I ++) 31 {32 CIN> V [I]; 33} 34 for (I = 1; I <= A; I ++) 35 {36 CIN> T [I]; 37} 38 39 for (I = 1; I <= A; I ++) 40 {41 for (j = B; j> = T [I]; j --) 42 {43 If (T [I] <= J) 44 f [J] = max (F [J], f [J-T [I] + V [I]); 45} 46} 47 cout <F [B] <Endl; 48} 49 return 0; 50}
This is a very obvious 0-1 backpack problem, that is, a given item has two states: Put, not put, and one item cannot be put multiple times.
1 for(i = 1;i <= a;i++)2 {3 for(j = b;j >= t[i];j--)4 {5 if(t[i] <= j)6 f[j] = max(f[j],f[j - t[i]] + v[i]);7 }8 }
This section is the core code. Pay attention to the for range. What is the first for from 1 to? A is the number of items, and the second for from B to T [I] t [I] is the volume of items. B is the maximum volume, so
1 For 1 --> N // 1/0 depends on the input W [I] V [I] When the subscript 0 starts or 1 starts 2 for B --> W [I] 3 if (W [I] <= B) 4 f [J] = max (F [J], F [J-T [I] + V [I]);
Among them, W [I] is quality, V [I] is value (quality and value = volume and value ).