zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    新闻发布系统之 登陆注销
    readonly和const的区别
    c#中文件流的读写
    解决jsp两种提交方式乱码 的方法
    B/S 和 C/S两种架构
    App.config的典型应用
    接口实现多态
    Spring概念
    在Spring的核心配置文件applicationContext.xml中配置事务,主要配置三大方面:事务管理器、事务通知和定义事务性切面。
    乱码问题解决方案
  • 原文地址:https://www.cnblogs.com/TheZealous/p/14491692.html
Copyright © 2011-2022 走看看