zoukankan      html  css  js  c++  java
  • 【hdu 2602 Bone Collector(动态规划、01背包)】

    Bone Collector

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


    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
     
     
     1 // Project name : 2602 ( Bone Collector ) 
     2 // File name    : main.cpp
     3 // Author       : Izumu
     4 // Date & Time  : Sat Jul 14 10:03:36 2012
     5 
     6 
     7 #include <iostream>
     8 #include <stdio.h>
     9 #include <string>
    10 #include <cmath>
    11 #include <algorithm>
    12 using namespace std;
    13 
    14 #define MAXN 1010
    15 
    16 int value[MAXN];
    17 int cost[MAXN];
    18 
    19 int dp[MAXN][MAXN];
    20 
    21 int n, v;
    22 
    23 int max(int a, int b)
    24 {
    25     return (a > b)? a: b;
    26 }
    27 
    28 void DynamicProgramming()
    29 {
    30     // init for dp[][]
    31     for (int i = 0; i <= n; i++)
    32     {
    33         for (int j = 0; j <= v; j++)
    34         {
    35             dp[i][j] = 0;
    36         }
    37     }
    38 
    39     for (int i = 1; i <= n; i++)
    40     {
    41         for (int j = 0; j <= v; j++)
    42         {
    43             if (cost[i] <= j)
    44             {
    45                 dp[i][j] = max(
    46                             dp[i-1][j],
    47                             dp[i-1][j-cost[i]] + value[i]
    48                         );
    49             }
    50             else
    51             {
    52                 dp[i][j] = dp[i-1][j];
    53             }
    54         }
    55     }
    56     
    57     
    58 }
    59 int main()
    60 {
    61     int t;
    62     cin >> t;
    63     while (t--)
    64     {
    65         cin >> n >> v;
    66         for (int i = 1; i <= n; i++)
    67         {
    68             cin >> value[i];
    69         }
    70         for (int i = 1; i <= n; i++)
    71         {
    72             cin >> cost[i];
    73         }
    74 
    75         DynamicProgramming();
    76 
    77         cout << dp[n][v] << endl;
    78     }
    79     return 0;
    80 }
    81 
    82 // end 
    83 // ism 
  • 相关阅读:
    [轉]javascript 的 location 各種用法
    [轉]PHP命名空间规则解析及高级功能
    [轉]虚拟机随谈(一):解释器,树遍历解释器,基于栈与基于寄存器
    [轉]深入理解SQL Server 2005 中的 COLUMNS_UPDATED函数
    [連接]JavaScript中链式调用之研习
    [轉]安装SQL SERVER 2008时,提示:服务SQLBrowser启动请求失败
    AS支除兩邊的空格
    [轉]详解UML六大关系(依赖、类属(继承)、关联、实现、聚合和组合)
    [轉]jQuery UI 关闭父窗口打开的Dialog
    [轉]让PHP支持像jQuery那样的链式操作
  • 原文地址:https://www.cnblogs.com/ismdeep/p/2591211.html
Copyright © 2011-2022 走看看