题目链接: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. 【linux日志】【日志分析】linux系统各日志文件的含义

    前段时间太忙,没有来得及管博客,最近时间充裕了,开始更新博客. 因为最近在看linux日志相关内容,把心得分享给大家 linux系统日志文件默认存放路径/var/log/ ls查看此路径下有哪些日志文 ...

  2. oracle一条语句插入多个值的方法

    今天在实践过程中遇到一个问题, 我想往数据库插入多条数据时,使用了如下语句: insert into 表1 (字段1,字段2) values (1,2),(2,3),(3,4); 这条语句在mysql ...

  3. 统计学习方法6—logistic回归和最大熵模型

    目录 logistic回归和最大熵模型 1. logistic回归模型 1.1 logistic分布 1.2 二项logistic回归模型 1.3 模型参数估计 2. 最大熵模型 2.1 最大熵原理 ...

  4. 夯实Java基础(六)——包装类

    1.包装类简介 我们都知道Java是面向对象编程语言,包含了8种基本数据类型,但是这8种基本数据类型并不支持面向对象的特征,它们既不是类,也不能调用方法.这在实际使用时存在很多的不便,比如int类型需 ...

  5. Winform DataGridView 取消默认选中行

    困境 网上有很多解决方法,可是很多读者照做并不生效.追究其原因,问题出现在许多博主没有搞清楚DataGridView绑定与当前触发事件的关系. 复现 private void Frm_Load(obj ...

  6. fatal: remote origin already exists.解决方法

    git remote add origin1 http://github.com/xxx/xxx.git origin名字冲突,换一个名字 遇到这种问题时表示已经有一个origin,冲突了,可能原因是 ...

  7. Flink 源码解析 —— Standalone session 模式启动流程

    Standalone session 模式启动流程 https://t.zsxq.com/EemAEIi 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0 ...

  8. python对接常用数据库,快速上手!

    python对接常用数据库,快速上手! 很多同学在使用python进行自动化测试的时候,会涉及到数据库数据校验的问题,因为不知道如何在python中如何对数据库,这个时候会一脸茫然,今天在这里给大家汇 ...

  9. python的魔术方法大全

    在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”(魔术方法),例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中有相应描述,这 ...

  10. 2019Hexo博客Next主题深度美化 打造一个炫酷博客(2)-奥怪的小栈

    219/8/1 更新 本文转载于:奥怪的小栈 这篇文章告诉你在搭建好博客后,面对网上千篇一律的美化教程怎么才能添加自己独特点,使人眼前一亮. 本站基于HEXO+Github搭建. 所以你需要准备好HE ...