<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">01背包问题:</span>
01背包问题是说有一个重量为w的包,有n个物体,第i个物体的重量是w[I],价值是V[I],每个物体最多只能拿一次,每个物体只有一个,怎样尽可能地装满包,使得价值最大,并且总的体重不超过w;
分析:之所以称为01背包是因为物体可以拿也可以不拿。那么分析一下状态,我们需要知道取了第几个物体,以及此时的背包大小最多可装多少价值的物体。
dp[i][j]:i表示取到第i个物体;j表示背包容量为j;dp[i][j]表示最大价值。
状态转移:取到第i个物体背包容量可以装下时,我们可以要,也可以不要,什么时候要这个物体?
————>当要第i个物体时:dp[i][j]=dp[i-1][j-w[i]]+v[i];(为什么是j-w[i]?模拟一下背包放物体的过程我们就知道,当要放第i个物体时,背包容量就为j-w[i],剩余背包尽可能多地装前面i-1个物体。)
————>当第i个物体不装时,那么dp[i][j]=dp[i-1][j];
取两者中的最大值。得到的便是最优解。
例题:
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 29060 Accepted Submission(s): 11871
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 2
31).
Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1
Sample Output
14
Author
Teddy
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1005;
int dp[maxn][maxn];
int va[maxn];
int w[maxn];
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",&va[i]);
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
for(int i=1;i<=n;i++)
{
for(int j=0;j<=v;j++)
{
if(j<w[i])
dp[i][j]=dp[i-1][j];//如果背包的容量比第i个物体小,放不下这个物体,相当于不放。
else
dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+va[i]);//状态转移。
}
}
printf("%d\n",dp[n][v]);
}
}
优化如下:
由于当更新到第i个物体是否可放时,我们只需要用到第i-1个物体的状态,因此只需要用一个一维数组记录背包为j时的最大价值,但是要注意要从最大容量更新,因为我们会用到前面小容量的值,从小容量更新会破坏原始数据。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1005;
int dp[maxn];//容量为j的背包能放的最大价值
int va[maxn];
int w[maxn];
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",&va[i]);
for(int i=1;i<=n;i++)
scanf("%d",&w[i]);
for(int i=1;i<=n;i++)
{
for(int j=v;j>=w[i];j--)//j从最大更新,只需要更新到w[i],因为当背包装不下i时无需更新,已经是最大了。
{
dp[j]=max(dp[j],dp[j-w[i]]+va[i]);
}
}
printf("%d\n",dp[v]);
}
}