Bone Collectorproblem descriptionmany years ago, in Teddy ' s hometown there were a man who was called "Bone Collector". 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 he trip of collecting there is a lot of bones, obviously , different bone have different value and different volume, now given the each bone's value along his trips, can you CALCU Late out the maximum of the total value the bone collector can get?
Inputthe first line contain a integer T, the number of cases.
Followed by T cases, each case three lines, the first line contain both integer n, V, (N <=, v <=) repr Esenting 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 for each bone. Outputone integer per line representing the maximum of the total value (this number would be is less than 231). Sample INPUT1
5 10
1 2 3) 4 5
5 4 3 2 1 Sample Output14 Answer01 backpack, see note.
#include <cstdio>#include<iostream>#include<string>#include<sstream>#include<cstring>#include<stack>#include<queue>#include<algorithm>#include<cmath>#include<map>#definePI ACOs (-1.0)#defineMS (a) memset (A,0,sizeof (a))#defineMSP Memset (Mp,0,sizeof (MP))#defineMSV memset (vis,0,sizeof (VIS))using namespacestd;//#defineLOCALstructnode{intVal; intVol;} v[ +];intdp[ +][ +];intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin); //freopen ("OUT.txt", "w", stdout);#endif //LOCALIos::sync_with_stdio (false); intN; CIN>>N; while(n--) { intn,m; CIN>>n>>m; MS (DP); for(intI=1; i<=n;i++) Cin>>V[i].val; for(intI=1; i<=n;i++) Cin>>V[i].vol; for(intI=1; i<=n;i++)//each item { for(intj=0; j<=m;j++)//put in a backpack with a capacity of [0,m] (item weight can be 0) { if(V[I].VOL<=J)//If you can putDp[i][j]=max (dp[i-1][j],dp[i-1][j-v[i].vol]+v[i].val); //1. If the previous one is large, do not put//Q: Why does the later add a small one that might be smaller than the original? //A:dp[i-1][j-v[i].vol] means the first i-1 items, put in J-v[i].vol's backpack,//if it doesn't fit, then this n-1 item is not put in,//That is, if you put the nth item, the backpack has only this item,//It may be smaller than n-1 an item before putting it on. //2. After a large, put Elsedp[i][j]=dp[i-1][J];//If you cannot put}} printf ("%d\n", Dp[n][m]);//the maximum value obtained from the output [putting the nth item into the backpack of M] } return 0;}
View Code
HDU 2602 Bone Collector (01 backpack)