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 ?
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 ?
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.
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 2
31).
Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1
Sample Output
14
解题报告:这题就是普通的01背包的模型。没有什么难度,本文的目的是解析01背包的一维数组的递推方程。
关于01背包的二维数组 的解释,网上的非常详细,我就不过多解释了。(百度:01背包)
对于二维的递推方程,dp[i][j]如果定义为前i个物品取出总重量不超过j的价值的最大值。
那么 dp[i][j] = max (dp[i - 1][j], dp[i-1][j-w[i]] + v[i]),其中w为重量,v为价值。j > w[i];
这个很好理解。
但是变成一维数组之后,就需要再做思考。
dp[j] = max(d[j], d[j-w[i]]) + v[i];
如果你简单地认为这个就是01背包的递推式,那你就太想当然了。
因为完全背包的递推公式也是如此。
唯一不同的是第二层循环,01背包是递增,完全背包是递减。
01背包:
for (int i = 0; i < n; i ++)
{
for (int j = W, j >= w[i]; j --)
{
dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
}
}
完全背包:
for (int i = 0; i < n; i ++)
{
for(int j = w[i], j <= W; j ++)
{
dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
}
}
怎么理解这个递增和递减呢?
我们回到二维数组,会发现:
01背包:
dp[i][j ]和 d[i-1][j-w[i]] 有关,而不是与 d[i][j-w[i]]有关。
对于一维dp[j]的状态转移应该在d[j-w[i]]由i-1变成i之前,即与d[i-1][j-w[i]]发生状态转移;
在第i轮,倒序从W开始遍历,即从dp[W]~dp[w[i]]更新,保证了每次更新用到的dp[j-w[i]]是i-1轮的。
理由:j从W~w[i],对应的 j-w[i] 是从 W-w[i]~0。
假如我更新了dp[k], w[i]<k<W,那么这样在更新dp[k+w[i]]时,dp[k+w[i]]必定受影响。
( 因为dp[k]已经是i轮的而不是i-1轮的了,结合dp[k+w[i]] = max(dp[k+w[i]], dp[k] + v[i]) )
可惜dp[k+w[i]]早已经更新过了(k < k+w[i], 倒序遍历j)。
所以,第i的dp[j]都是由第i-1的dp[j']更新的,滚动数组可行。
完全背包:
正序遍历的话,j从 w[i]~W 对应的j-w[i]是 0~W-w[i]。
假如我更新了dp[k], w[i]<k<W,那么这样更新dp[k+w[i]]必定受影响。
由j是递增且每次加1可知,第i轮的dp[j]总是被第i轮的dp[j']影响着。
这符合了完全背包递推式 dp[i][j]=max(dp[i][j],dp[i][j-w[i]]+v[i])。
两种背包的根本区别就是同一个物品能否被多选。
回到本题,只能选一次,01背包,直接贴代码了。
对于一维dp[j]的状态转移应该在d[j-w[i]]由i-1变成i之前,即与d[i-1][j-w[i]]发生状态转移;
在第i轮,倒序从W开始遍历,即从dp[W]~dp[w[i]]更新,保证了每次更新用到的dp[j-w[i]]是i-1轮的。
理由:j从W~w[i],对应的 j-w[i] 是从 W-w[i]~0。
假如我更新了dp[k], w[i]<k<W,那么这样在更新dp[k+w[i]]时,dp[k+w[i]]必定受影响。
( 因为dp[k]已经是i轮的而不是i-1轮的了,结合dp[k+w[i]] = max(dp[k+w[i]], dp[k] + v[i]) )
可惜dp[k+w[i]]早已经更新过了(k < k+w[i], 倒序遍历j)。
所以,第i的dp[j]都是由第i-1的dp[j']更新的,滚动数组可行。
完全背包:
正序遍历的话,j从 w[i]~W 对应的j-w[i]是 0~W-w[i]。
假如我更新了dp[k], w[i]<k<W,那么这样更新dp[k+w[i]]必定受影响。
由j是递增且每次加1可知,第i轮的dp[j]总是被第i轮的dp[j']影响着。
这符合了完全背包递推式 dp[i][j]=max(dp[i][j],dp[i][j-w[i]]+v[i])。
两种背包的根本区别就是同一个物品能否被多选。
回到本题,只能选一次,01背包,直接贴代码了。
#include <stdio.h>
#include <string.h>
int t, n, W, v[1005],w[1005], dp[1005];
int main()
{
scanf("%d", &t);
while(t--){
memset(dp, 0, sizeof(dp));
scanf("%d%d", &n, &W);
for (int i = 0; i < n; i ++){
scanf("%d", v + i);
}
for (int i = 0; i < n; i ++){
scanf("%d", w + i);
}
for (int i = 0; i < n; i ++){
for (int j = W; j >= w[i]; j --){
if (dp[j-w[i]] + v[i] > dp[j]){
dp[j] = dp[j-w[i]] + v[i];
}
}
}
printf("%d\n", dp[W]);
}
return 0;
}