原题代号:HDU 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
 
题目大意:骨头收集者在收集骨头,有容量为v的袋子,n个骨头,给出每个骨头的价值和体积。问最大能收集的价值是多少。(01背包问题)
 
解法一:时间,空间复杂度均为O(n2)

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; int a[+],b[+],dp[+][+]; int main()
{
int t,n,v;
cin>>t;
while(t--)
{
cin>>n>>v;
FOR(i,,n)cin>>b[i];
FOR(i,,n)cin>>a[i];
mem(dp,);
FOR(i,,n)FOR(j,,v)
{
if(a[i]<=j)dp[i][j]=max(dp[i-][j],dp[i-][j-a[i]]+b[i]);
else dp[i][j]=dp[i-][j];
}
cout<<dp[n][v]<<endl;
}
return ;
}

解法二:时间复杂度O(n2),空间复杂度O(n)

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; int a[+],b[+],dp[+]; int main()
{
int t,n,v;
cin>>t;
while(t--)
{
int ans=;
cin>>n>>v;
FOR(i,,n)cin>>b[i];
FOR(i,,n)cin>>a[i];
mem(dp,);
FOR(i,,n)For(j,v,)
{
if(a[i]<=j)dp[j]=max(dp[j],dp[j-a[i]]+b[i]);
}
cout<<dp[v]<<endl;
}
return ;
}

HDU 2602 Bone Collector (01背包问题)的更多相关文章

  1. HDU 2602 Bone Collector(01背包裸题)

    Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  3. [HDU 2602]Bone Collector ( 0-1背包水题 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 水题啊水题 还给我WA了好多次 因为我在j<w[i]的时候状态没有下传.. #includ ...

  4. HDU 2602 Bone Collector --01背包

    这种01背包的裸题,本来是不想写解题报告的.但是鉴于还没写过背包的解题报告.于是来一发. 这个真的是裸的01背包. 代码: #include <iostream> #include < ...

  5. HDU 2602 Bone Collector (01背包DP)

    题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...

  6. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  7. HDU 2602 Bone Collector 0/1背包

    题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others) ...

  8. HDU 2602 Bone Collector(经典01背包问题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/O ...

  9. hdu 2602 Bone Collector(01背包)模板

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Bone Collector Time Limit: 2000/1000 MS (Java/Ot ...

随机推荐

  1. database 学习

    ref : 什么是NoSQL,为什么要使用NoSQL?

  2. linux 正则表达式 元字符

    \b 单词边界 \bcool\b  只匹配cool字符串 [root@MongoDB ~]# cat test.txt i am mike1 i am mike i am mike12 匹配有mike ...

  3. 【监控实践】【4.2】perfmon监控性能计数器(基于typeperf命令)

    关键词:typeperf typeperf 命令 使用示例: 案例1:#使用typeperf收集windows cpu.内存.硬盘性能 #使用typeperf收集windows cpu.内存.硬盘性能 ...

  4. select count(*) 底层到底干了啥?

    作者:贾春生,http://dwz.win/myg SELECT COUNT( * ) FROM TABLE 是个再常见不过的 SQL 需求了. 在 MySQL 的使用规范中,我们一般使用事务引擎 I ...

  5. AcWing 875. 快速幂

    题目链接:https://www.acwing.com/problem/content/description/877/ 快速幂模板题,计算ab mod p 的值,a,b,p大概1e9左右,可以快速计 ...

  6. 【转】海量数据解决思路之BitMap

    转载(http://zengzhaozheng.blog.51cto.com/8219051/1404108) 一.概述 本文将讲述Bit-Map算法的相关原理,Bit-Map算法的一些利用场景,例如 ...

  7. Node+Express+MySql实现简单增删改查和登录

    var express = require('express'); var mysql = require('mysql'); var app = express(); var bodyParser ...

  8. C#多线程下更新UI的几种方法

    1. 使用UI线程的SynchronizationContext的Post/Send方法,这种情况一般在窗体构造函数或者FormLoad中获取同步上下文: 范例: public partial cla ...

  9. 在JSP中<%= >,<%! %>,<% %>所代表的含义

    <%! %>:是jsp中的声明标签,通常声明全局变量,常量,方法等. <% %>:<% java代码 %>,其中可以包含局部变量,java语句等. <%= % ...

  10. 关于javaweb 项目 ssm框架 启动tomcat服务器同时启动一个socket服务

    1.创建监听类 import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax. ...