• 题解报告:hdu 2602 Bone Collector(01背包)


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

    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

    解题思路:简单的01背包(dp)。

    AC代码一:(二维数组实现)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn=1005;
     4 int t,n,W,v[maxn],w[maxn],dp[maxn][maxn];
     5 int main(){
     6     while(cin>>t){
     7         while(t--){
     8             cin>>n>>W;
     9             for(int i=1;i<=n;++i)cin>>v[i];
    10             for(int i=1;i<=n;++i)cin>>w[i];
    11             memset(dp,0,sizeof(dp));
    12             for(int i=1;i<=n;++i){
    13                 for(int j=0;j<=W;++j){
    14                     if(j<w[i])dp[i][j]=dp[i-1][j];//无法挑选这个物品
    15                     else dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]);//拿和不拿的两种情况都试一下
    16                 }
    17             }
    18             cout<<dp[n][W]<<endl;
    19         }
    20     }
    21     return 0;
    22 }

    AC代码二:(一维数组实现)

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int value[1005],weight[1005],dp[1005];//dp数组始终记录当前体积的最大价值
     4 int main()
     5 {
     6     int T,N,V;
     7     cin>>T;
     8     while(T--){
     9         cin>>N>>V;
    10         for(int i=0;i<N;i++)cin>>value[i];//输入价值
    11         for(int i=0;i<N;i++)cin>>weight[i];//输入体积
    12         memset(dp,0,sizeof(dp));//初始化
    13         for(int i=0;i<N;i++){    //个数
    14             for(int j=V;j>=weight[i];j--) //01背包
    15                 dp[j]=max(dp[j],dp[j-weight[i]]+value[i]); //比较放入i物体后的价值与不放之前的价值,记录大的值
    16         }
    17         cout<<dp[V]<<endl;//输出总体积的最大价值
    18     }
    19     return 0;
    20 }
  • 相关阅读:
    微服务架构:自动扩展简介
    作为软件开发人员需要的技术技能
    NetStat
    IntegerCache缓存占用堆、栈、常量池的问题,自动拆装箱的基本概念,Integer==int时的问题说明
    Docker常用命令速查手册(华贵铂金版)
    深入剖析Windows专业版安装Docker引擎和Windows家庭版Docker引擎安装的区别
    一个有趣的现象,既然是知识产出还是有必要声明下原创最好【虾扯蛋系列】
    MySql CPU彪高到百分之1000的排查思路
    准备一个大菜
    常见的 由于未调整服务器 ulimit 而引起的内存溢出问题
  • 原文地址:https://www.cnblogs.com/acgoto/p/8830553.html
Copyright © 2020-2023  润新知