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 ?
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 231).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
用子问题定义状态:即dp[i][v]表示前i件物品恰放入一个容量为v的背包可以获得的最大价值。则其状态转移方程便是:
dp[i][v]=max{dp[i-1][v],dp[i-1][v-vi[i]]+val[i]}
这个方程非常重要,基本上所有跟背包相关的问题的方程都是由它衍生出来的。所以有必要将它详细解释一下:
“将前i件物品放入容量为v的背包中”这个子问题 若只考虑第i件物品的策略(放或不放),那么就可以转化为一个只牵扯前i-1件物品的问题。
如果不放第i件物品,那么问题就转化为“前i-1件物品放入容量为v的背包中”,价值为dp[i-1][v];
如果放第i件物品,那么问题就转化为“前i-1件物品放入剩下的容量为v-vi[i]的背包中”,此时能获得的最大价值就是dp[i-1][v-vi[i]]再加上通过放入第i件物品获得的价值val[i]。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[1000][1001];
int vi[10001];
int val[10001];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,v;
scanf("%d%d",&n,&v);
for(int i=1;i<=n;i++)
{
scanf("%d",&val[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&vi[i]);
}
dp[0][0]=0;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=v;j++)
{
if(vi[i]<=j)
dp[i][j]=max(dp[i-1][j],dp[i-1][j-vi[i]]+val[i]);//放入第i个物品
else
dp[i][j]=dp[i-1][j];
}
}
printf("%d
",dp[n][v]);
}
return 0;
}
下面是对上面的优化我们可以用一维数组来代替二维数组
dp[i][v]是由dp[i-1][v]和dp[i-1][v-vi[i]]两个子问题递推而来,为了保证在推dp[i][v]时(也即在第i次主循环中推dp[v]时)能够得到dp[i-1][v]和dp[i-1][v-vi[i]]的值 可以在每次i主循环中以v——>0求出dp[v]
状态转移方程:dp[v]=max(dp[v],dp[v-vi[i]+val[i]);
注意:这种解法只能由V--0,不能反过来,如果反过来就会造成物品重复放置!
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[1000];
int vi[10001];
int val[10001];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
int n,v;
scanf("%d%d",&n,&v);
for(int i=1;i<=n;i++)
{
scanf("%d",&val[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&vi[i]);
}
dp[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=v;j>=vi[i];j--)
{
dp[j]=max(dp[j],dp[j-vi[i]]+val[i]);
}
}
printf("%d
",dp[v]);
}
return 0;
}