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 }

    空间复杂度的对比:

  • 相关阅读:
    C#获取根目录的方法集合
    C# 获取文件MD5值的方法
    VS2015 搭建 Asp.net core 开发环境
    【Networking】容器网络大观 && SDN 资料汇总
    【Networking】flannel,pipework,weave,udp,vxlan,ovs等资料
    【K8s】Kubernetes 最近正在看的资料
    【GoLang】go 微服务框架 && Web框架学习资料
    【Eclipse】eclipse che 协作开发
    【Kubernetes】两篇文章 搞懂 K8s 的 fannel 网络原理
    【SpringBoot】SpringBoot 入门示例
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804178.html
Copyright © 2011-2022 走看看