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又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...
随机推荐
- .c和.h文件的区别
.h文件(头文件): 一般写一些函数声明.宏定义.结构体等内容. 其实就是将各个.c文件中重复的声明.宏定义.结构体,枚举变量等提取出来,放进一个新的文件中,便于其他.c文件共享这部分的代码,同时也方 ...
- js中判断一个对象的类型的种种方法
javascript中检测对象的类型的运算符有:typeof.constructor.instanceof. typeof:typeof是一个一元运算符,返回结果是一个说明运算数类型的字符串.如:&q ...
- 【Java例题】7.1 线程题1-时间显示线程
1.时间显示线程.设计一个示线程子类,每秒钟显示一次当前时间:然后编写主类,在主函数中定义一个线程对象,并启动这个线程 package chapter7; import java.text.Simpl ...
- Windows 下安装 Python + Django
Django是Python的一个Web开发框架,以下是介绍的是windows下的安装步骤, 作者的环境是Win10 ,Windows Server 也是一样的 以下是作者整理的步骤,也可以参考官方教程 ...
- [NUnit] discover test finished: 0 found issue
%Temp%\VisualStudioTestExplorerExtensions & restart visual studio
- Element UI系列:Upload图片自定义上传
HTML部分代码 Javascript部分代码 CSS代码 样式部分可以自由调整 主要实现的原理是利用 http-request 的属性对上传进行自定义
- Unity进阶之:Shader渲染
版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...
- koa2+vue实现登陆以及是否登陆控制
这里我们先说说登陆以及登陆状态控制需要的插件jsonwebtoken,jsonwebtoken就可以实现token的生成与反向解密出用户数据.安装步骤: npm install jsonwebtoke ...
- Winform改变Textbox边框颜色
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Javascript实现简单地发布订阅模式
不论是在程序世界里还是现实生活中,发布—订阅模式的应用都非常广泛.我们先看一下现实中的例子. 小明最近看上了一套房子,到了售楼处之后才被告知,该楼盘的房子早已售罄.好在售楼MM告诉小明,不久后还有一些 ...