HDU2602 Bone Collector 【01背包】[通俗易懂]

HDU2602 Bone Collector 【01背包】

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

Bone Collector

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 28365    Accepted Submission(s): 11562
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 ?


HDU2602 Bone Collector 【01背包】[通俗易懂]


 

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

01背包入门题。

#include <stdio.h>
#include <string.h>
#define maxn 1002

int dp[maxn], w[maxn], v[maxn];

int main()
{
    int t, n, val, i, j;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d", &n, &val);
        for(i = 1; i <= n; ++i) scanf("%d", v + i);
        for(i = 1; i <= n; ++i) scanf("%d", w + i);
        memset(dp, 0, sizeof(dp));
        for(i = 1; i <= n; ++i){
            for(j = val; j >= w[i]; --j){
                if(dp[j] < dp[j-w[i]] + v[i]) 
                    dp[j] = dp[j-w[i]] + v[i];
            }
        }
        printf("%d\n", dp[val]);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
上一篇 2022年1月21日 下午9:00
下一篇 2022年1月21日 下午10:00


相关推荐

  • 俄罗斯方块(C语言实现)

    俄罗斯方块(C语言实现)文章目录游戏说明游戏效果展示游戏代码游戏代码详解游戏框架构建隐藏光标光标跳转初始化界面初始化方块信息颜色设置画出方块空格覆盖合法性判断游戏主体逻辑函数判断得分与结束主函数游戏说明俄罗斯方块相信大家都知道,这里就不再介绍什么游戏背景了,我这里对本代码实现的俄罗斯方块作一些说明:按方向键的左右键可实现方块的左右移动。按方向键的下键可实现方块的加速下落。按空格键可实现方块的顺时针旋转。按Esc键可退出游戏。按S键可暂停游戏,暂停游戏后按任意键继续游戏。按R键可重新开始游戏。除此之外,本游戏还

    2022年5月19日
    44
  • BootstrapValidator中文文档手册

    BootstrapValidator中文文档手册BootstrapVal 0 5 3 下载地址 https download csdn net download 目前支持 4 种大的校验方式 分别是 Input 针对 input textarea select 控件 CompareValid AjaxValidato RegexValidat FunctionVali

    2026年3月17日
    2
  • redis 配置密码验证_spring redis配置

    redis 配置密码验证_spring redis配置redis配置密码1.通过配置文件进行配置yum方式安装的redis配置文件通常在/etc/redis.conf中,打开配置文件找到#requirepassfoobared去掉行前的注释,并修改密码为所需的密码,保存文件requirepassmyRedis重启redissudoserviceredisrestart#或者sudoservicerediss

    2025年9月3日
    12
  • 使用 Captcha 扩展包 为 Laravel 5 应用生成验证码

    使用 Captcha 扩展包 为 Laravel 5 应用生成验证码

    2021年10月26日
    46
  • 面向对象多态概念理解

    面向对象多态概念理解1 nbsp 什么是多态一句话概括 父类对象引用子类变量调用的是子类的实现例子 子类 publicclassC publicvoidhe System out println 我是中国人 publicvoidba System out println 我来拜年了

    2026年3月16日
    2
  • 计算机网络协议详解怎么写_自考短线好还是长线好

    计算机网络协议详解怎么写_自考短线好还是长线好计算机网络协议详解:本篇文章主要总结一些常见的网络基础概念以及Linux系统中的网络相关设置方法,具体包括OSI七层协议、TCP/IP协议相关内容。

    2022年10月2日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号