zoukankan      html  css  js  c++  java
  • HDU-2602 Bone Collector

    http://acm.hdu.edu.cn/showproblem.php?pid=2602

    01背包:用二维数组实现。

    c[n][m]表示n种物品,背包容量为m的最大价值。

    状态方程为:f(n,m)=max{f(n-1,m), f(n-1,m-w[n])+P(n,m)}这就是书本上写的动态规划方程.

                 Bone Collector

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 24513    Accepted Submission(s): 9911

    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
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 using namespace std;
     5 int c[1010][1010];
     6 int p[2000],w[2000];
     7 int main()
     8 {
     9     int t,n,m,i,j;
    10     cin>>t;
    11     while(t--)
    12    {
    13         memset(p,0,sizeof(p));
    14         memset(w,0,sizeof(w));
    15        memset(c,0,sizeof(c));
    16         cin>>n>>m;
    17       for(i=1;i<=n;i++)
    18         cin>>p[i];
    19       for(i=1;i<=n;i++)
    20         cin>>w[i];
    21     for(i=1;i<n+1;i++)
    22     for(j=0;j<m+1;j++)
    23     {
    24         if(w[i]<=j)
    25         {
    26              if(p[i]+c[i-1][j-w[i]]>c[i-1][j])
    27                  c[i][j]=p[i]+c[i-1][j-w[i]];
    28              else
    29                  c[i][j]=c[i-1][j];
    30         }else
    31              c[i][j]=c[i-1][j];
    32      }
    33       printf("%d
    ",c[n][m]);
    34 
    35     }
    36     return 0;
    37 }
     
  • 相关阅读:
    改动ScrollView的滑动速度和解决ScrollView与ViewPager的冲突
    SpringMVC 学习笔记(五) 基于RESTful的CRUD
    86/88汇编代码的执行调试
    HTML5+CSS3设计界面
    导入gradle项目
    Gradle安装 Gradle效率提升 eclipse安装gradle插件 【我】
    Eclipse集成Gradle 【Eclipse在线安装Gradle插件方法】
    gradle下载的依赖包位置 及 修改
    idea 普通 web项目配置启动【我】
    理解 IntelliJ IDEA 的项目配置和Web部署
  • 原文地址:https://www.cnblogs.com/cancangood/p/3567696.html
Copyright © 2011-2022 走看看