hdu-2602-Bone Collector

简介: hdu-2602-Bone Collector



Bone Collector

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


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 2 31).

 


Sample Input

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

 


Sample Output

      14      

 


Author

Teddy

 


Source

HDU 1st “Vegetable-Birds Cup” Programming Open Contest

 


Recommend

lcy   |   We have carefully selected several similar problems for you:   1203  2159  2955  1171  2191

 




题目分析:

就是简单的背包,但是要读清题,第一行是 石头数(也就是价值)  第二行是 需要的背包体积。但是一定要搞清楚 石头数可以是 0  背包体积也可以是 0.而且石头数(就是价值)可以不是0 ,但消耗的体积可以0

5  0

1 2 3 4 5

0 0 0 1 0

结果是: 11  


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[1010][1010];
int cost[1010],val[1010];
int main()
{
  int t;
  scanf("%d",&t);
  while(t--)
  {
    int n,v;
    
    scanf("%d%d",&n,&v);
    memset(f,0,sizeof(f));
    
    cost[0]=val[0]=0;
    for(int i=1;i<=n;i++)
        scanf("%d",&val[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&cost[i]);
    for(int i=1;i<=n;i++)
      for(int j=0;j<=v;j++)  // 记住要从0开始  石头是可以不消耗体积的
      {
        f[i][j]=f[i-1][j];
        if(j>=cost[i])
          f[i][j]=max(f[i-1][j],f[i-1][j-cost[i]]+val[i]);
      }
      printf("%d\n",f[n][v]);
  }
  return 0;
}


一维数组优化代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[1010];
int cost[1010],val[1010];
int main()
{
  int t;
  scanf("%d",&t);
  while(t--)
  {
    int n,v;
    
    scanf("%d%d",&n,&v);
    memset(f,0,sizeof(f));
    
    cost[0]=val[0]=0;
    for(int i=1;i<=n;i++)
        scanf("%d",&val[i]);
    for(int i=1;i<=n;i++)
        scanf("%d",&cost[i]);
    for(int i=1;i<=n;i++)
      for(int j=v;j>=0;j--)//  记者要到 0 
      {
        if(j>=cost[i])
          f[j]=max(f[j],f[j-cost[i]]+val[i]);
      }
      printf("%d\n",f[v]);
  }
  return 0;
}









目录
相关文章
|
9月前
|
Web App开发 存储 机器学习/深度学习
欢迎使用CSDN-markdown编辑器
欢迎使用CSDN-markdown编辑器
57 0
|
9月前
hdu-2066-一个人的旅行(dijkstra)
hdu-2066-一个人的旅行(dijkstra)
56 0
|
9月前
|
Java
HDU-1551-Cable master
HDU-1551-Cable master
35 0
|
9月前
|
Java
HDU-2199-Can you solve this equation?
HDU-2199-Can you solve this equation?
51 0
|
Java 测试技术 Linux
|
8月前
|
C++ 开发者 开发工具
面向 C++ 的现代 CMake 教程(四)(2)
面向 C++ 的现代 CMake 教程(四)
90 0
|
9月前
|
存储 C语言
C语言变量的内存地址深入探究
C语言变量的内存地址深入探究
142 0
|
9月前
|
Oracle Java 关系型数据库
实时计算 Flink版操作报错合集之本地打成jar包,运行报错,idea运行不报错,是什么导致的
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
113 0
|
8月前
|
消息中间件 Apache RocketMQ
《阿里云产品四月刊》—Apache RocketMQ ACL 2.0 全新升级(5)
阿里云瑶池数据库云原生化和一体化产品能力升级,多款产品更新迭代
159 0
《阿里云产品四月刊》—Apache RocketMQ ACL 2.0 全新升级(5)
|
9月前
|
存储 安全 网络协议
游戏服务器:构建与运行的艺术
游戏服务器:构建与运行的艺术
141 1

热门文章

最新文章