Description
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 …Input
The first line contain a integer T , the number of cases.Output
One integer per line representing the maximum of the total value (this number will be less than 2 31).Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1Sample Output
14 題解:0-1背包問題,dp[i][j]表示i個物品最大價值j#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[1000][1000];
int a[1000],b[1000];
int main()
{
int t,n,v;
cin>>t;
while(t--)
{
cin>>n>>v;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=0;j<=v;j++)
{
dp[i][j]=(i==1?0:dp[i-1][j]);
if(j>=b[i])
dp[i][j]=max(dp[i-1][j],dp[i-1][j-b[i]]+a[i]);
}
}
printf("%d\n",dp[n][v]);
}
return 0;
}