Input
The first line contain a integer T , the number of cases.
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.
Output
One integer per line representing the maximum of the total value (this number will be less than 231).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
题意:
第一行包含一个整数T,即背包的容量。
后面是T例,每一种情况都是三行,第一行包含两个整数N V(N<=1000,V<=1000)表示骨头的数量和他的袋子的体积。第二行包含N个整数表示每个骨头的价值。第三行包含N个整数,表示每个骨头的体积。
输出能装入骨头的最大价值。
思路:
简单的01背包
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 1e3 + 5;
long long dp[maxn];
int main()
{
int t;
cin >> t;
int N,V,x;
vector<int>A;
vector<int>val;
while(t--) {
cin >> N >> V;
A.clear();
val.clear();
memset(dp,0,sizeof(dp));
for (int i = 0; i < N; ++i) {
cin >> x;
val.push_back(x);
}
for (int i = 0; i < N; ++i) {
cin >> x;
A.push_back(x);
}
long long res = 0;
for (int i = 0; i < N; ++i) {
for (int j = V; j >= 0; --j){
if (j >= A[i])
dp[j] = max(dp[j],dp[j-A[i]] + (long long)val[i]);
res = max(res,dp[j]);
}
}
cout << res << endl;
}
return 0;
}