01揹包問題之HDU——2602 Bone Collector

<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 ?
 

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.
 

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
 

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]);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章