• 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 }

    空间复杂度的对比:

  • 相关阅读:
    Nginx中的Rewrite的重定向配置与实践
    分析IP所用的网络
    转载理解inode
    windows server 2016 active directory 域控 BDC 转换 为 PDC 副域控 升级 为 主 域控 (适用于 主备 域控 在线状态)
    debian 安装 supervisor
    debian 系统安装配置chrony
    二维字典新增数据
    快速读取大文件的最后一行
    获取当前系统时间
    python3 的 filter 与 map
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804178.html
Copyright © 2020-2023  润新知