zoukankan      html  css  js  c++  java
  • HDU 2602

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602

    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 ? 

    InputThe 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. OutputOne integer per line representing the maximum of the total value (this number will be less than 2 31). Sample Input

    1
    5 10
    1 2 3 4 5
    5 4 3 2 1

    Sample Output

    14

    题解:

    01背包模板题。


    AC代码:

    二维数组写法:

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 
     5 unsigned long max_weight[1003][1003];//max_weight[i][j]:前i个骨头中,容量为j的背包里能放的下的最大重量 
     6 struct type{
     7     int v;
     8     int w;
     9 }bone[1003];
    10 
    11 int main()
    12 {
    13     int n,V;
    14     int t;scanf("%d",&t);
    15     while(t--){
    16         scanf("%d%d",&n,&V);
    17         for(int i=1;i<=n;i++) scanf("%d",&bone[i].w);
    18         for(int i=1;i<=n;i++) scanf("%d",&bone[i].v);
    19         
    20         for(int j=0;j<=V;j++){
    21             if(bone[1].v <= j) max_weight[1][j]=bone[1].w;
    22             else max_weight[1][j]=0;
    23         }
    24         
    25         for(int i=2;i<=n;i++){
    26             for(int j=0;j<=V;j++){
    27                 if(j<bone[i].v) max_weight[i][j]=max_weight[i-1][j];
    28                 else max_weight[i][j]=max( max_weight[i-1][j] , max_weight[i-1][ (j-bone[i].v) ] + bone[i].w );
    29             }
    30         }
    31         
    32         printf("%d
    ",max_weight[n][V]);
    33     }
    34 }

    减小空间复杂度,滚动一维数组写法:

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 
     5 unsigned long max_weight[1003];
     6 struct type{
     7     int v;
     8     int w;
     9 }bone[1003];
    10 
    11 int main()
    12 {
    13     int n,V;
    14     int t;scanf("%d",&t);
    15     while(t--){
    16         scanf("%d%d",&n,&V);
    17         for(int i=1;i<=n;i++) scanf("%d",&bone[i].w);
    18         for(int i=1;i<=n;i++) scanf("%d",&bone[i].v);
    19         
    20         for(int j=0;j<=V;j++){
    21             if(bone[1].v <= j) max_weight[j]=bone[1].w;
    22             else max_weight[j]=0;
    23         }
    24         
    25         for(int i=2;i<=n;i++){
    26             for(int j=V;j>=0;j--){
    27                 if(j<bone[i].v) max_weight[j]=max_weight[j];
    28                 else max_weight[j]=max( max_weight[j] , max_weight[ (j-bone[i].v) ] + bone[i].w );
    29             }
    30         }
    31         
    32         printf("%d
    ",max_weight[V]);
    33     }
    34 }

    空间复杂度的对比:

  • 相关阅读:
    Spring MVC与JAX-RS比较与分析
    JDK历史版本下载
    第六篇:为多态基类声明虚析构函数
    第五篇:明确拒绝不想编译器自动生成的拷贝构造函数和赋值运算符重载函数
    第四篇:了解 C++ 默默编写并调用的函数
    第三篇:确保对象在被使用前的初始化
    poj 2125(最小割)
    hdu 4704(费马小定理)
    hdu 4705(树形DP)
    poj 3469(网络流模版)
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804178.html
Copyright © 2011-2022 走看看