首页 > 代码库 > Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!详解!)

Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!详解!)

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
 
先输入一个t,代表有t个例子,然后输入n和v,n代表有多少骨头,v代表背包体积,每样东西只有一个,也就是说只能取一次,刚开始看这道题的时候完全不知道怎么做,感觉会很麻烦的样子,学长是作为例题给我们讲的,刚开始有点头绪,但具体还是完全不懂,后来自己专门刷这方面的专题,还算理解的比较好,动态规划的思想得要理解好,这题是用空间换时间,过程中会产生大量之间数据,嗯,就是这样,好了,回到这道题,我们用一维数组来存储数据,但是有些pdf文档比如背包九讲就是用二维数组来讲解,都可以的。
现在进入正题:输入前面讲过了,现在讲核心代码
for(i=1;i<=n;i++)
   for(j=v;j>=c[i];j--)
        liu[j]=max(liu[j],liu[j-c[i]]+w[i]);
            
           

这三行就是核心,就像背包九讲里面说的,这个状态转移方程非常重要,一定要理解,它联系了上一个状态和这一个状态,所以叫做状态转移方程!!!!!!
为了更加清楚描述运行过程中数组每个值的具体变化,我在这里加了几行代码:
for(i=1;i<=n;i++)
        {
            for(j=v;j>=c[i];j--)
            {
                liu[j]=max(liu[j],liu[j-c[i]]+w[i]);
            }
            for(k=1;k<=v;k++)
                printf("%d ",liu[k]);
            printf("\n");
        }

我们以题目给的数据为例,运行结果如下:


从图中我们可以看出(结合上面的代码),程序循环五次,每次循环的结果都在动态变化,如果还不能理解,建议自己在草稿子上模拟i=1,i=2的时候数组变化的情况,就会很好理解了,动态规划给我的感觉就是代码很短,但是理解之后就很简单了!!!!!!
好了,讲完了,最近在做dp专题,会不定时更新做题的心得还有题解,大家一起学习!
下面贴下AC代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
int main()
{
    int i,j,k;
    int t,n,m,v;
    int liu[1006],c[1006],w[1006];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&v);
        for(i=1;i<=n;i++)
            scanf("%d",&w[i]);
        for(i=1;i<=n;i++)
            scanf("%d",&c[i]);
        memset(liu,0,sizeof(liu));
        for(i=1;i<=n;i++)
        {
            for(j=v;j>=c[i];j--)
            {
                liu[j]=max(liu[j],liu[j-c[i]]+w[i]);
            }
            for(k=1;k<=v;k++)
                printf("%d ",liu[k]);
            printf("\n");
        }
        printf("%d\n",liu[v]);
    }
    return 0;
}

写代码能力有限,如有编程爱好者发现bug,还请指出,不胜感激!