Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 93273 Accepted Submission(s): 38144
Problem 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 …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , 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 ?
问题描述
许多年前,在泰迪的家乡,有一个人被称为“骨头收藏家”。这个男人喜欢收集各种各样的骨头,比如狗的,牛的,还有他去了坟墓…骨收集器有一个体积为V的大袋子,沿着他的收集之旅有很多骨头,显然,不同的骨骼有不同的价值和不同的体积,现在根据他的行程给出每个骨头的值,你能计算吗?超出骨收集器可以获得的总价值的最大值?
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.
输入
第一行包含整数T,个案数。接下来是T个案例,每个案例有三行,第一行包含两个整数N,V,(N <= 1000,V <= 1000)表示骨骼的数量和他的包的体积。第二行包含表示每个骨骼值的N个整数。第三行包含表示每个骨骼体积的N个整数。
Output
One integer per line representing the maximum of the total value (this number will be less than 231).
输出
每行一个整数表示总值的最大值(此数字将小于231)。
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
个人心得:第一次忘记加else后面语句,第二次里面嵌套的for循环j要不止小于还要等于bagmax,第三次忘记要有while(test–)导致一直wa。
ac代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1005;
int val[maxn], v[maxn], dp[maxn][maxn];
int main()
{
int test, n, bagmax;
scanf_s("%d", &test);
while (test--)
{
scanf_s("%d%d", &n, &bagmax);
memset(dp, 0, sizeof(dp));//正确示范
for (int i = 1; i <= n; i++)
{
scanf_s("%d", &val[i]);
}
for (int i = 1; i <= n; i++)
{
scanf_s("%d", &v[i]);
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= bagmax; j++)
{
if (j >= v[i])
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - v[i]] + val[i]);
else
dp[i][j] = dp[i - 1][j];
}
}
cout << dp[n][bagmax] << endl;
}
return 0;
}