hdu 5887 Herbs Gathering (dfs+剪枝 or 超大01背包)
题目链接: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背包)的更多相关文章
- HDU 5887 Herbs Gathering(搜索求01背包)
http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做, ...
- HDU 5887 Herbs Gathering
背包,$map$,优化. 和普通背包一样,$map$加一个$erase$优化一下就可以跑的很快了. #pragma comment(linker, "/STACK:1024000000,10 ...
- HDU 1501 Zipper 【DFS+剪枝】
HDU 1501 Zipper [DFS+剪枝] Problem Description Given three strings, you are to determine whether the t ...
- HDU - 5887:Herbs Gathering (map优化超大背包)
Collecting one's own plants for use as herbal medicines is perhaps one of the most self-empowering t ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- NYOJ 1091 超大01背包(折半枚举)
这道题乍一看是普通的01背包,最最基础的,但是仔细一看数据,发现普通的根本没法做,仔细观察数组发现n比较小,利用这个特点将它划分为前半部分和后半部分这样就好了,当时在网上找题解,找不到,后来在挑战程序 ...
- HDU 6083 度度熊的午饭时光(01背包+记录路径)
http://acm.hdu.edu.cn/showproblem.php?pid=6083 题意: 思路: 01背包+路径记录. 题目有点坑,我一开始逆序枚举菜品,然后一直WA,可能这样的话路径记录 ...
- HDU 3339 In Action(迪杰斯特拉+01背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1455 Sticks(dfs+剪枝)
题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...
随机推荐
- 【Android Studio】使用 Genymotion 调试出现错误 INSTALL_FAILED_CPU_ABI_INCOMPATI
RT -- 解决方法参考: https://my.oschina.net/u/242764/blog/375909 http://blog.csdn.net/wjr2012/article/detai ...
- 【iOS】判断苹果的设备是哪种
有时候需要判断苹果的设备是 iPhone 还是 iPad 等其他设备,示例代码如下: if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUs ...
- 【iOS】copy 关键字
以前没注意过 iOS 的 copy, nonatomic, assign, weak, strong 等关键字. 偏偏今天遇到了一个问题,恰恰是关键字的问题,如图: 之前用的是 assign, 没有用 ...
- C# 10分钟完成百度图片提取文字(文字识别)——入门篇
现在图片文字识别已经很成熟了,比如qq长按图片,点击图片识别就可以识别图片的文字,将不认识的.文字数量大的.或者不能赋值的值进行二次可复制功能. 我们现在就基于百度Ai开放平台进行个人文字识别,dem ...
- Android 属性动画实战
什么是属性动画? 属性动画可以通过直接更改 View 的属性来实现 View 动画.例如: 通过不断的更改 View 的坐标来实现让 View 移动的效果: 通过不断的更改 View 的背景来实现让 ...
- 夯实Java基础(十)——抽象类和接口
转载自:http://cmsblogs.com/ 该博主的网站上干货非常!非常!非常多(说三遍),强烈推荐大家前去学习. 接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法 抽象类与接口是 ...
- 神奇的 SQL 之子查询,细节满满 !
前言 开心一刻 有一天,麻雀遇见一只乌鸦. 麻雀问:你是啥子鸟哟 ? 乌鸦说:我是凤凰. 麻雀说:哪有你龟儿子这么黢黑的凤凰 ? 乌鸦说:你懂个铲铲,老子是烧锅炉的凤凰. 子查询 讲子查询之前,我们先 ...
- 如何保证FPGA PCIe唤醒能满足PC的100ms 的时间要求(Autonomous Mode)?
原创By DeeZeng [ Intel FPGA笔记 ] PC 需要PCIe设备在 100ms 内启动,这样PC 才能扫描到PCIe 设备.对于 FPGA PCIe 板卡,同样也需要满足这个时间要 ...
- C#自动计算字符串公式的四种方法
原地址:https://blog.csdn.net/ifu25/article/details/53292134 四种方式 简单粗暴:利用SQL数据库计算 功能强大:利用JavaScript计算 看不 ...
- 关于c++中的复合类型
目录 数组 字符串 结构体 共用体 枚举 指针 数和指针的关系 常见的存储方式 数组替代品 一.数组 存储在每个元素中值的类型 数组名 数组中的元素数 通用格式:typename arrayname ...