前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Bone Collector——HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)

Bone Collector——HDOJ杭电2602(纯01背包问题!!!!!!具体解释!)

作者头像
全栈程序员站长
发布2022-07-10 13:26:37
发布2022-07-10 13:26:37
30200
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是全栈君。

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

Sample Input

代码语言:javascript
代码运行次数:0
复制
    1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

代码语言:javascript
代码运行次数:0
复制
    14

先输入一个t,代表有t个样例。然后输入n和v,n代表有多少骨头。v代表背包体积,每样东西仅仅有一个,也就是说仅仅能取一次,刚開始看这道题的时候全然不知道怎么做。感觉会非常麻烦的样子,学长是作为例题给我们讲的。刚開始有点头绪,但详细还是全然不懂。后来自己专门刷这方面的专题。还算理解的比較好。动态规划的思想得要理解好,这题是用空间换时间。过程中会产生大量之间数据,嗯,就是这样。好了,回到这道题。我们用一维数组来存储数据,可是有些pdf文档比方背包九讲就是用二维数组来解说,都能够的。

如今进入正题:输入前面讲过了,如今讲核心代码

代码语言:javascript
代码运行次数:0
复制
for(i=1;i<=n;i++)
   for(j=v;j>=c[i];j--)
        liu[j]=max(liu[j],liu[j-c[i]]+w[i]);

这三行就是核心。就像背包九讲里面说的,这个状态转移方程很重要,一定要理解,它联系了上一个状态和这一个状态,所以叫做状态转移方程!

!!!

!!

为了更加清楚描写叙述执行过程中数组每一个值的详细变化,我在这里加了几行代码:

代码语言:javascript
代码运行次数:0
复制
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的时候数组变化的情况,就会非常好理解了。动态规划给我的感觉就是代码非常短,可是理解之后就非常easy了!!!

!!!

好了。讲完了,近期在做dp专题,会不定时更新做题的心得还有题解,大家一起学习。

以下贴下AC代码:

代码语言:javascript
代码运行次数:0
复制
#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,还请指出,不胜感激!

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115497.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年2月3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档