题目链接: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. 【SVN】SVN Working copy is too old

    前天在使用 SVN 客户端 CornerStone 的时候遇到了这个问题,代码不能提交了…… 遇到这个问题的时候怎么办? 解决办法: 找到报错对应的文件夹,里面有个 .svn 的文件夹,去掉再 com ...

  2. eclipse的下载安装配置

    1.在eclipse官网下载与你电脑版本相对应的安装包.链接:https://www.eclipse.org/downloads/eclipse-packages/ 2.下载与eclipse版本相对应 ...

  3. Web项目如何做单元测试

    你可能会用单元测试框架,python的unittest.pytest,Java的Junit.testNG等. 那么你会做单元测试么!当然了,这有什么难的? test_demo.py def inc(x ...

  4. 解决Activiti5.22流程图部署在Windows上正常,但在linux上部署后出现中文变方块的问题

    总结/朱季谦 楼主最近在做公司的工作流平台,发现一个很无语的事情,Activiti5.22的流程图在Windows环境上部署,是可以正常查看的,但发布到公司的Linux服务器上后,在上面进行流程图在线 ...

  5. 想成为顶尖 Java 程序员?请先过了下面这些技术问题。

    一.数据结构与算法基础 说一下几种常见的排序算法和分别的复杂度. 用Java写一个冒泡排序算法 描述一下链式存储结构. 如何遍历一棵二叉树? 倒排一个LinkedList. 用Java写一个递归遍历目 ...

  6. 缓存的有效期和淘汰策略【Redis和其他缓存】【刘新宇】

    缓存有效期与淘汰策略 有效期 TTL (Time to live) 设置有效期的作用: 节省空间 做到数据弱一致性,有效期失效后,可以保证数据的一致性 Redis的过期策略 过期策略通常有以下三种: ...

  7. 原 CNN--卷积神经网络从R-CNN到Faster R-CNN的理解(CIFAR10分类代码)

    1. 什么是CNN 卷积神经网络(Convolutional Neural Networks, CNN)是一类包含卷积计算且具有深度结构的前馈神经网络(Feedforward Neural Netwo ...

  8. Draw.io

    如何给类图增加一个字段? 选中一个字段,然后按 Ctrl +Enter 即可. 参考:Add row to class diagram - stackoverflow

  9. 【JS档案揭秘】第二集 Event loop与执行栈

    我时常在思考关于JS的很多知识在工作中有什么用?是否只能存在于面试这种理论性的东西中,对于我们的业务和工作,它们又能扮演怎样的角色.以后在JS档案揭秘的每一期里,都会加入我对于业务的思考,让这些知识不 ...

  10. spring-boot-plus项目配置文件(四)

    spring-boot-plus项目配置文件 配置文件说明 配置说明 项目中配置文件主要使用yml格式 配置文件位置:spring-boot-plus\src\main\resources\confi ...