博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU5887 Herbs Gathering(2016青岛网络赛 搜索 剪枝)
阅读量:6957 次
发布时间:2019-06-27

本文共 1138 字,大约阅读时间需要 3 分钟。

背包问题,由于数据大不容易dp,改为剪枝,先按性价比排序,若剩下的背包空间都以最高性价比选时不会比已找到的最优解更好时则剪枝,即

if(val + (LD)pk[d].val / (LD)pk[d].w * (lim - w) + EPS <= ans){

  return;
}

没想到一发过,0ms

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long LL;typedef long double LD;const int N = 108, INF = 0x3F3F3F3F, EPS = 0.4;struct data{ LL w, val; bool operator<(const data &tp)const{ return val * tp.w > tp.val * w; }}pk[N];int n;LL sum[N], sw[N], lim;LL ans;void dfs(int d, LL w, LL val){ if(val > ans){ ans = val; } if(d >= n){ return; } if(val + (LD)pk[d].val / (LD)pk[d].w * (lim - w) + EPS <= ans){ return; } if(w + pk[d].w <= lim){ dfs(d + 1, w + pk[d].w, val + pk[d].val); } dfs(d + 1, w, val);}int main(){ while(~scanf("%d %I64d", &n, &lim)){ for(int i = 0; i < n; i++){ scanf("%I64d %I64d", &pk[i].w, &pk[i].val); } sort(pk, pk + n); ans = 0; sum[n] = 0; sw[n] = 0; dfs(0, 0, 0); printf("%I64d\n", ans); } return 0;}

  

转载于:https://www.cnblogs.com/IMGavin/p/6034209.html

你可能感兴趣的文章
轰轰烈烈的搭建Spring + Spring MVC + Mybatis
查看>>
MySQL 单机多实例
查看>>
微信小程序入门到实战(二)
查看>>
graphql-java使用手册:part4 订阅(Subscriptions)
查看>>
理解js对象
查看>>
2017-10-07 前端日报
查看>>
Go 领军人物谢孟军:智能制造渴望银弹,首先要摒弃偏见
查看>>
函数式编程与面向对象编程[3]:Scala的OOP-FP混合式编程与抽象代数理论
查看>>
statsd on steroid
查看>>
【mongoDB运维篇③】replication set复制集
查看>>
php中查询mysql如何在IN 中用数组
查看>>
2015 年十佳 HTML5 应用
查看>>
php对象设计进阶
查看>>
python程序的调试
查看>>
启动级别:init 0-6
查看>>
mybatis深入理解(一)之 # 与 $ 区别以及 sql 预编译
查看>>
Java四种引用类型
查看>>
TIOBE 6 月编程语言榜:TypeScript 首次跻身前100
查看>>
Fedora 31 将更新开源 .Net 框架,支持 Mono 5
查看>>
Emulator 29.0.3 Canary 发布,Android 模拟器
查看>>