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


相关推荐

  • Linux(centos7)历史命令UP/DOWN自动补全

    Linux(centos7)历史命令UP/DOWN自动补全

    2021年5月14日
    170
  • C++ map 根据value找key、 根据key找value

    C++ map 根据value找key、 根据key找value根据value找key    有可能找到多个结果根据key找value    、、、、、、运行效果:代码很简单,如下:#include&lt;iostream&gt;#include&lt;map&gt;#include&lt;string&gt;usingnamespacestd;intmain(intargc,char*…

    2022年7月23日
    12
  • hive like与rlike的区别

    hive like与rlike的区别like与rlike的区别:like不是正则,而是通配符。这个通配符可以看一下SQL的标准,例如%代表任意多个字符。rlike是正则,正则的写法与java一样。’\’需要使用’\\’,例如’\w’需要使用’\\w’hive>select”aaaaa”like”%aaa%”fromtest_structlimit10;Totaljobs=1…OK

    2022年7月26日
    9
  • sendip linux发包工具

    sendip linux发包工具第一步:安装sendip工具sudoapt-getupdatesudoapt-getinstallsendipmansendip//可以查看sendip的使用方法第二步:使用开启两台虚拟机,在其中一台执行sendip命令,在另一台抓包分析sendip命令格式:sendip网络层传输层数据domainsendip-v-p***-is***-id***-p***-f/-d***

    2025年9月12日
    5
  • 从零开始学WEB前端——HTML理论讲解

    从零开始学WEB前端——HTML理论讲解????项目介绍先做个自我介绍,本人是一个没人写前端所以就自学前端的后端程序员????。在此项目中我会和大家一起从零基础开始学习前端,从后端程序员的视角来看前端,受限于作者的水平本项目暂时只会更新到前端框架VUE,不会涉及node.js。该项目适合零基础的小白或者和我一样开发网站没人写前端所以自学前端的后端程序员????。该项目的学习顺序是按照我自己学习时总结出来的,其中的每个知识点都是我认真去理解的,同时也查了很多的资料,所有的参考资料我都放在了文章末尾。尊重开源,尊重知识产权。每一个案例我都亲手写过

    2022年5月3日
    43
  • 虚拟ip设置 – Keepalived详解

    虚拟ip设置 – Keepalived详解linux机器可以设置虚拟ip来实现双机热备功能

    2022年10月12日
    4

发表回复

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

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