• HDU 2602 Bone Collector


    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

    Author
    Teddy
     
    Source
    HDU 1st “Vegetable-Birds Cup” Programming Open Contest

    Problem analysis

    这是一个0-1背包问题,每个骨头只取一次(我觉得这篇文章解释0-1背包问题解释的非常清楚,有不懂得可以去看一下)。

    然后就是要注意骨头的体积可以取零!!!

    code implementation

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int w[1001],a[1001],dp[1001][1001];
     4 int main()
     5 {
     6     int n,v,k;
     7     cin>>k;
     8     while(k--)
     9     {
    10         memset(dp,0,sizeof(dp));
    11         cin>>n>>v;
    12         for(int i=1;i<=n;i++)
    13         {
    14             cin>>w[i];//价值 
    15         }
    16         for(int i=1;i<=n;i++)
    17         {
    18             cin>>a[i];    //体积 
    19         }
    20         
    21         for(int i=1;i<=n;i++)
    22         {
    23             for(int j=0;j<=v;j++)//这里一定要用j=0,虽然我也不知道为啥骨头的体积可以为零,都没了还捡啥 
    24             {
    25                 if(j>=a[i])
    26                 {
    27                     dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i]]+w[i]);//i-1!!
    28                 }
    29                 else{
    30                     dp[i][j]=dp[i-1][j];
    31                 }
    32             }
    33         }
    34         cout<<dp[n][v]<<endl;
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    114. Flatten Binary Tree to Linked List 把二叉树变成链表
    426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表
    微服务之如何建模微服务
    我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=3t37r4hauhq8c
    剑指offer之面试题2:实现Singleton模式
    微服务之演化式架构师(二)
    ASP.NET Core 框架本质学习
    java之maven之maven的使用
    java之maven之初识maven
    java之mybatis整合spring
  • 原文地址:https://www.cnblogs.com/TheZealous/p/14491692.html
Copyright © 2020-2023  润新知