• HDOJ_ACM_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
     
    Code
    View Code
     1 #include <stdio.h>
     2 #include <algorithm>
     3 using namespace std;
     4 #define N 1000
     5 struct Bone
     6 {
     7     int value;
     8     int vol;
     9 }bones[N + 5];
    10 int f[N + 5][N + 5];
    11 int main()
    12 {
    13     int T, n, bagVol, i, j;
    14     scanf("%d", &T);
    15     while (T--)
    16     {
    17         //input and gain the radio which is the answer of value dividing volume
    18         scanf("%d %d", &n, &bagVol);
    19         for (i = 1; i <= n; i++)
    20             scanf("%d", &bones[i].value);
    21         for (i = 1; i <= n; i++)
    22             scanf("%d", &bones[i].vol);
    23         //handle
    24         for (i = 1; i <= n; i++)
    25             for (j = 0; j <= bagVol; j++)
    26                 if (bones[i].vol <= j)
    27                     f[i][j] = max(f[i - 1][j], f[i - 1][j - bones[i].vol] + bones[i].value);
    28                 else
    29                     f[i][j] = f[i - 1][j];
    30         //print
    31         printf("%d\n", f[n][bagVol]);
    32     }
    33     return 0;
    34 }
    View Code
    Key Points
    Firstly, you must realize it's a knapsack question. 
    For those question, you should know the function f(n, v) = max(f(n - 1, v), f(n - 1, v - vol[n]) + value[n]).
    f(n, v) means the greatest value of putting n things into knapsack whose volume is v.
    vol[n] means the volume array.
    value[n] means the value array.
    If you don't choose the nth thing, it's f(n - 1, v), if you chooose the nth thing, it's f(n - 1, v - vol[n]) plus the nth thing's value.
    then the bigger answer is the f(n, v).
    Of course, you can using form to deepen understanding.
    For example, if you input those number as follows.
    1
    2 5
    3 4
    2 3
      0 1 2 3 4 5
    0 0 0 0 0 0 0
    1 0 0 3 3 3 3
    2 0 0 3 4 4 7
     
    Secondly, I provide simpler program.
    If you understand the first kind, I think it's easy for you. 
    What I want to express is that the second circulation should begin with max, or you will be wrong. Because you put the same thing again.
     
  • 相关阅读:
    BZOJ1077 并查集
    linux(fedora) 第三课
    hdu 4513 吉哥系列故事——完美队形II(manacher)
    hdu 3294 Girls' research(manacher)
    FZU
    蓝桥杯试题 k倍区间(dp)
    蓝桥杯 带分数(全排列+枚举)
    天梯赛 L2-001 紧急救援
    蓝桥杯[2017年第八届真题]分考场 (图着色问题)
    hdu 3068 最长回文(manacher)
  • 原文地址:https://www.cnblogs.com/chuanlong/p/2779204.html
Copyright © 2020-2023  润新知