A - Bone Collector ( 01 背包问题(一维dp[ ] 的精确运用)(一个物件仅能选择一次时,为01背包问题,01代表,不选与选的抉择)

A - Bone Collector

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 2 31).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14

动态规划原理:是一种将问题实例分解为更小的、相似的子问题,并存储子问题的解而避免计算重复的子问题,以解决最优化问题的算法方法。0/1背包问题可以看作是决策一个序列(x1,x2,x3,x4,….xn),对任一变量xi的决策是决定xi=1,还是xi=0。在对xi-1决策后,已确定了(x1,x2,x3,x4,….xi-1),在决策xi时,问题处于两种状态之一:

背包容量不足以装入物品i,则xi=0,背包不增加价值;

背包容量可以装入物品i,则xi=1,背包价值增加了 vi

 

    这两种情况下背包价值的最大者应该是对xi决策后的背包价值。令v(i,j)表示在前i(1<=i<=n)个物品中能够装入容量为j(1<=j<=C)的背包中的物品的最大值。

 

 

这是经典的01背包问题,就是给定一定量的物件的价值和占用空间,让你选出最大价值的方法!经典的dp[ ]使用,最好是使用一维dp[ ],省时间省空间,棒棒哒!!!

(当然也可以使用dfs递归的方法,但是麻烦耗时,吃力不讨好,花钱买罪受,dfs递归,每个物件都有选和不选之分。递归执行下去一直max就行)

最重要的是寻找出状态转换方程,dp[i]=max( dp[ i-volume[j] ] + value[j] , dp[i] ),但是这要求 i 从最大到最小的迭代!为什么呢?(dp[0]=0;没有空间装,价值为0)(个在初始化时已经定义好了,不用多加关注)

比如上面的例子,刚刚开始时,dp[10]是全零的,当寻找到第一个值时也就是volume为5,value为1的值时,第一次循环得dp[10-->1]={1,1,1,1,1,1,0,0,0,0}

 for(int i=0; i<N; i++)//循环一边,选和不选都max了一遍
        {
            for(int j=V; j>=volume[i]; j--)
            {   //从最大开始到最小,根据上一次max,方便下一次max,

 

(不能从小到大,从小到大是叠加的过程,也就是从多次选择同一个类型,一个叠加一个,到最大值时,相当于叠加了很多个一样大小空间类型的物件,这就是完全背包问题!!!!!!!!!!!!!!!!!

 

这是逆序是实现,接着进行第二次,dp[10-4]即为dp[6]时,我们看见已经进行了赋值,方便下一次得迭代运行!!!!!!!!所以,我们只需判断是dp[i]原值大,还是dp[ i-volume[j] ] + value[j] 这是加值大就行,dp[ i-volume[j] ]在上一次进行了赋值,方便这次进行运行!!!!!

再加上每次都进行max最大值得选择,所以每个dp值都是最大的最优化的选择!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                         变化过程

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;


int dp[1006];
int value[1006];
int volume[1006];

int main()
{
    int num;
    cin>>num;
    while(num--)
    {
        int N,V;
        cin>>N>>V;
        memset(value,0,sizeof(value));
        memset(volume,0,sizeof(volume));
        memset(dp,0,sizeof(dp));
        for(int i=0; i<N; i++)
        {
            cin>>value[i];
        }
        for(int i=0; i<N; i++)
        {
            cin>>volume[i];
        }
        for(int i=0; i<N; i++)
        {
            for(int j=V; j>=volume[i]; j--)
            {
                    dp[j]=max(dp[ j-volume[i] ]+value[i], dp[j] );

            }
        }

        cout<<dp[V]<<endl;

    }
    return 0;
}
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;


int dp[1006];
int value[1006];
int volume[1006];

int main()
{
    int num;
    cin>>num;
    while(num--)
    {
        int N,V;
        cin>>N>>V;
        memset(value,0,sizeof(value));
        memset(volume,0,sizeof(volume));
        memset(dp,0,sizeof(dp));
        for(int i=0; i<N; i++)
        {
            cin>>value[i];
        }
        for(int i=0; i<N; i++)
        {
            cin>>volume[i];
        }
        for(int a=V; a>=0; a--)
        {
            cout<<"["<<a<<"]"<<" ";
        }
        cout<<endl;
        for(int i=0; i<N; i++)
        {
            for(int j=V; j>=volume[i]; j--)
            {
                dp[j]=max(dp[ j-volume[i] ]+value[i], dp[j] );



            }
            for(int a=V; a>=0; a--)
            {
                cout<<dp[a]<<" ";
            }
            cout<<endl;
        }

        cout<<dp[V]<<endl;

    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值