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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 简述python变量命名规范_【转】python变量命名规范

    简述python变量命名规范_【转】python变量命名规范python源码和其他一些书籍,命名各种个性,没有一个比较统一的命名规范。于是总结了一些,供参考。模块名:模块应该使用尽可能短的、全小写命名,可以在模块命名时使用下划线以增强可读性。同样包的命名也应该是这样的,虽然其并不鼓励下划线。主要是考虑模块名是与文件夹相对应的,因此需要考虑文件系统的一些命名规则的,比如Unix系统对大小写敏感,而过长的文件名会影响其在Windows\Mac\Dos等系统中的…

    2022年6月18日
    25
  • Delphi中谨慎使用QuotedStr、QuotedString、DequotedString相关的函数

    Delphi中谨慎使用QuotedStr、QuotedString、DequotedString相关的函数//以下测试代码vars,ss:string;begin//1.添加定界符(内容中含定界符的要转义)ss:=QuotedStr(s);//单引号ss:=s.QuotedString(””);//单引号//2.添加指定的定界符(内容中含定界符的要转义)ss:=AnsiQuotedStr(s,'”‘);//双引号ss:=s.QuotedString(‘”‘);//双引号//3.去掉定界符(内容中含连续两个定界符的要反转义)…

    2022年10月18日
    3
  • Harbor私有仓库中如何彻底删除镜像释放存储空间?

    Harbor私有仓库中如何彻底删除镜像释放存储空间?

    2021年6月3日
    193
  • vue v-if 多条件_vue if show

    vue v-if 多条件_vue if showv-if在模板中,可以根据条件进行渲染。条件用到的是v-if、v-else-if以及v-else来组合实现的。示例代码如下:<divid="app"><p

    2022年7月30日
    8
  • python 中文文本分类[通俗易懂]

    python 中文文本分类[通俗易懂]写这篇博文用了很多时间和精力,如果这篇博文对你有帮助,希望您可以打赏给博主相国大人。哪怕只捐1毛钱,也是一种心意。通过这样的方式,也可以培养整个行业的知识产权意识。我可以和您建立更多的联系,并且在相关领域提供给您更多的资料和技术支持。赏金将用于拉萨儿童图书公益募捐手机扫一扫,即可:目标读者:初级入门学生。本文假定,你对python已经有了最基本的掌握。如果你希望能够

    2022年6月13日
    41
  • idea激活码2022.01.13[最新免费获取]2022.01.19

    (idea激活码2022.01.13)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~0…

    2022年3月31日
    86

发表回复

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

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