板子题,0-1背包的裸板子,只需在板子基础上加上控制样例输入个数的while循环即可。
本人AC代码:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MaxN = 1e3 + 5;
int T;
int n, m;
int f[MaxN][MaxN];
int v[MaxN], w[MaxN];
int main() {
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) scanf("%d", &w[i]);
for(int i = 1; i <= n; i++) scanf("%d", &v[i]);
memset(f, 0, sizeof(f));
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= m; j++) {
if(j >= v[i]) f[i][j] = max(f[i - 1][j], f[i - 1][j - v[i]] + w[i]);
else f[i][j] = f[i - 1][j];
}
}
printf("%d\n", f[n][m]);
}
return 0;
}