码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-13 06:58:19      阅读:523      评论:0      收藏:0      [点我收藏+]

标签:acm   c   编程   hdoj   动态规划   

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 ?
bubuko.com,布布扣

 

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

我们以题目给的数据为例,运行结果如下:
bubuko.com,布布扣
bubuko.com,布布扣
从图中我们可以看出(结合上面的代码),程序循环五次,每次循环的结果都在动态变化,如果还不能理解,建议自己在草稿子上模拟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,还请指出,不胜感激!

Bone Collector------HDOJ杭电2602(纯01背包问题!!!!!!详解!),布布扣,bubuko.com

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

标签:acm   c   编程   hdoj   动态规划   

原文地址:http://blog.csdn.net/u014231159/article/details/25605411

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!