码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 0/1背包问题


    例题HDU-2602

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    二维数组dp[][]

    以下是代码

    #include
    #include
    using namespace std;
    
    int maxBoneValue(int n,int V,vector<int>& value,vector<int>& volumes)
    {
       
        vector<vector<int>> dp(n+1,vector<int>(V+1,0));
        //i表示前i个骨头
        for(int i = 1;i<=n;i++)
        {
       //j代表当前背包的容量,当j=0时,意味着背包的容量为0
        //dp[i][0]都是0,当我们数组初始化时,已经隐含了j=0的情况,所以从1开始
            for(int j= 1;j<=V;j++)
            {
       
                //不选这个骨头,如果装不下
                if(j<volumes[i-1])
                {
       //这个判断 j < volumes[i-1] 是为了检查当前的背包容量 j 是否足够来装下第 i 个骨头。
                 //volumes[i-1] 是第 i 个骨头的体积(因为数组是从0开始的,所以第 i 个骨头的体积在 volumes 数组中的索引是 i-1)    
                 //j代表当前背包容量  
                   dp[i][j] = dp[i-1][j];
                }
                else{
       
                    //考虑这个骨头情况
                    dp[i]
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    如何在 SAPGUI 里显示上传到 ABAP 服务器的 PDF 文件
    TorchSemiSeg项目调试
    数据仓库作业五:第8章 关联规则挖掘
    算法通关村第十五关青铜挑战——使用位存储 处理海量数据问题之用4KB内存寻找重复元素
    云存储应急演练体系建立及场景设计
    Java JDK安装与配置
    关于mac下ubuntu安装20.04的无桌面版后开机后总是进入引导界面
    一篇文章讲清楚芯片设计全流程及相关岗位划分
    TencentOS3.1系统升级GitLab15.5.3
    Java8函数式编程-lambda表达式与stream流
  • 原文地址:https://blog.csdn.net/yalipf/article/details/133471442
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号