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

题解:这题一看像是背包但是显然背包容量太大了所以可以考虑用dfs+剪枝,贪心得到的不一定是正确答案。当然这题还可以用背包来写,其实这就用到了dp的一些优化就是存状态,递推过程中有些状态是多余的没必要计算这样就可以大大减少空间的利用和时间的浪费

第一份是dfs+剪枝的写法第二份是背包+map存状态的写法。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
struct TnT {
ll val , ti;
double r;
}T[];
ll ans;
int cnt , n;
ll w;
bool cmp(TnT x , TnT y) {
return x.r > y.r;
}
int check(int i , ll sum_val , ll sum_ti) {
for(int j = i ; j < cnt && sum_ti < w ; j++) {
if(sum_ti + T[j].ti <= w) sum_val += T[j].val , sum_ti += T[j].ti;
else {
sum_val += (w - sum_ti) * T[j].r;
break;
}
}
return sum_val > ans;
}
void dfs(int i , ll sum_val , ll sum_ti) {
ans = max(ans , sum_val);
if(i < cnt && check(i , sum_val , sum_ti)) {
if(sum_ti + T[i].ti <= w) dfs(i + , sum_val + T[i].val , sum_ti + T[i].ti);
dfs(i + , sum_val , sum_ti);
}
}
int main() {
while(~scanf("%d%lld" , &n , &w)) {
cnt = ;
for(int i = ; i < n ; i++) {
ll x , y;
scanf("%lld%lld" , &x , &y);
if(x <= w) T[cnt].ti = x , T[cnt].val = y , T[cnt++].r = (y * 1.0) / (x * 1.0);
}
sort(T , T + cnt , cmp);
ans = ;
dfs( , , );
printf("%lld\n" , ans);
}
return ;
}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
struct TnT {
int ti , val;
}T[];
map<int , ll>mmp;
bool cmp(TnT x , TnT y) {
return x.ti < y.ti;
}
int main() {
int n , w;
while(~scanf("%d%d" , &n , &w)) {
for(int i = ; i < n ; i++) {
scanf("%d%d" , &T[i].ti , &T[i].val);
}
mmp.clear();
mmp[] = ;
queue<int>q;
ll ans = ;
sort(T , T + n , cmp);
for(int i = ; i < n ; i++) {
if(T[i].val == || T[i].ti > w) continue;
ll Max = ;
for(map<int , ll> ::iterator it = mmp.begin() ; it != mmp.end() ; it++) {
if(it->first + T[i].ti <= w && Max < it->second + T[i].val) {
Max = max(Max , it->second + T[i].val);
q.push(it->first + T[i].ti);
}
}
while(!q.empty()) {
int gg = q.front();
q.pop();
mmp[gg] = max(mmp[gg] , mmp[gg - T[i].ti] + T[i].val);
ans = max(ans , mmp[gg]);
}
}
printf("%lld\n" , ans);
}
return ;
}

hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)的更多相关文章

  1. HDU 5887 Herbs Gathering(搜索求01背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做, ...

  2. HDU 5887 Herbs Gathering

    背包,$map$,优化. 和普通背包一样,$map$加一个$erase$优化一下就可以跑的很快了. #pragma comment(linker, "/STACK:1024000000,10 ...

  3. HDU 1501 Zipper 【DFS+剪枝】

    HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...

  4. HDU - 5887:Herbs Gathering (map优化超大背包)

    Collecting one's own plants for use as herbal medicines is perhaps one of the most self-empowering t ...

  5. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. NYOJ 1091 超大01背包(折半枚举)

    这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...

  7. HDU 6083 度度熊的午饭时光(01背包+记录路径)

    http://acm.hdu.edu.cn/showproblem.php?pid=6083 题意: 思路: 01背包+路径记录. 题目有点坑,我一开始逆序枚举菜品,然后一直WA,可能这样的话路径记录 ...

  8. HDU 3339 In Action(迪杰斯特拉+01背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...

  9. hdu 1455 Sticks(dfs+剪枝)

    题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...

随机推荐

  1. .Net Core DevOps -免费用Azure四步实现自动化发布(CI/CD)

    前言 linux 大行其道的今天想必大家都已经拥抱 core 了吧,通常的方案都是 gitlab+jenkins+centos,但是这样的方案不适合我这种懒人,一直在寻求简单的解决方案,在寻求方案的过 ...

  2. Ubuntu 执行chmod -R 777 / 挽救方法

    mgj怎么会有堪比rm -rf /*这样神奇的命令,本想着把当前目录下的权限改为777,没想到把整个/目录下全设成777了,直觉告诉我好像哪里有些不对劲,好在一顿xjb折腾最终弄好了,应该没啥大问题, ...

  3. Hadoop 系列(三)—— 分布式计算框架 MapReduce

    一.MapReduce概述 Hadoop MapReduce 是一个分布式计算框架,用于编写批处理应用程序.编写好的程序可以提交到 Hadoop 集群上用于并行处理大规模的数据集. MapReduce ...

  4. MQ如何解决消息的顺序性

    一.消息的顺序性 1.延迟队列:设置一个全局变量index,根据实际情况一次按照index++的逻辑一次给消息队列设置延迟时间段,可以是0.5s,甚至1s; 弊端:如果A,B,C..消息队列消费时间不 ...

  5. Spark 系列(七)—— 基于 ZooKeeper 搭建 Spark 高可用集群

    一.集群规划 这里搭建一个 3 节点的 Spark 集群,其中三台主机上均部署 Worker 服务.同时为了保证高可用,除了在 hadoop001 上部署主 Master 服务外,还在 hadoop0 ...

  6. java学习中碰到的疑惑和解答(二)

    路径问题是一个在平时学习和开发碰到的常见问题,对于初学者是一个比较值得研究的东西.因此对路径问题进行总结. 1. 编写路径为了告诉编译器如何找到其他资源.   2. 路径分类: 相对路径:从当前资源出 ...

  7. CSS3:pointer-events | a标签禁用

    用纯css就能实现取消事件响应的方法,pointer-events,使用起来更加简单,它可以: pointer-events: auto | none | visiblePainted | visib ...

  8. Sql Or NoSql,看完这一篇你就懂了

    前言 你是否在为系统的数据库来一波大流量就几乎打满CPU,日常CPU居高不下烦恼?你是否在各种NoSql间纠结不定,到底该选用那种最好?今天的你就是昨天的我,这也是写这篇文章的初衷. 这篇文章是我好几 ...

  9. 实测win10 efi启动及centos7双系统引导顺序修改

    安装win10 安装win10过程中,系统自动建立esp分区,分区格式为FAT16,目录如下 1,EFI/Boot文件夹保持不动 删除 EFI/Microsoft/boot/ 文件夹下面除BCD文件外 ...

  10. 章节十六、1-TestNG简介

    一.TestNG 介绍 1.TestNG 是一个来自 JUnit 和 NUnit 的测试框架,它具拥有更多的功能,提高了 执行的效率. 2.TestNG 是一个开源的自动化测试框架 去除了老框架的大部 ...