zoukankan      html  css  js  c++  java
  • HDU 2602 (0-1背包)

    Bone Collector

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 35815    Accepted Submission(s): 14753


    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
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1203 2159 2955 1171 2191 
     

    题目大意:

      就是说,给你一个N和V和N个物品的v[i]和w[i],让你求出,并且每次向背包中放入一个物品,让你求出最多能放多少个物品(物品的体积<=V)所能带来的最大的价值。

    解题思路:

      直接走0-1背包的模型:

        定义状态:dp[i+1][j]表示,从前i个物品中选出来总重不超过j的物品所能带来的最大价值.

        初始状态:dp[0][j]==0.

        状态转移方程: dp[i+1][j] = dp[i][j] ,当j < w[i]时。

                dp[i+1][j] = max ( dp[i][j],dp[i][j-w[i]]+v[i]); 其他

     1 # include<cstdio>
     2 # include<iostream>
     3 
     4 using namespace std;
     5 
     6 const int MAX = 1000+4;
     7 
     8 int dp[MAX][MAX];
     9 int w[MAX];
    10 int v[MAX];
    11 
    12 
    13 int main(void)
    14 {
    15     int t;cin>>t;
    16     while ( t-- )
    17     {
    18         int n,vv;
    19         cin>>n>>vv;
    20         for ( int i = 0;i < n;i++ )
    21         {
    22             cin>>v[i];
    23         }
    24         for ( int i = 0;i < n;i++ )
    25         {
    26             cin>>w[i];
    27         }
    28         for ( int i = 0;i < n;i++ )
    29         {
    30             for ( int j = 0;j <= vv;j++ )
    31             {
    32                 dp[0][j] = 0;
    33                 if ( j < w[i] )
    34                 {
    35                     dp[i+1][j] = dp[i][j];
    36                 }
    37                 else
    38                 {
    39                     dp[i+1][j] = max(dp[i][j],dp[i][j-w[i]]+v[i] );
    40                 }
    41             }
    42         }
    43         cout<<dp[n][vv]<<endl;
    44     }
    45 
    46 
    47     return 0;
    48 }

    代码:

  • 相关阅读:
    JS基础学习四:绑定事件
    常用JS事件对象
    jq 使用手册
    access数据库根据指定日期进行查询
    IP地址变动后,https://localhost:1158/em无法访问解决办法
    结构体对齐方式
    宏得到当前函数的名字
    std::list保存大量数据时,类型即是无析构函数,该list析构时会占用大量CPU
    装了vs2010 SP1后,开机速度慢
    查询SQL Server版本号
  • 原文地址:https://www.cnblogs.com/wikioibai/p/4429323.html
Copyright © 2011-2022 走看看