【Luogu】P2491消防(单调队列)
首先可以想到路径一定是在直径上的。
然后对每个点dfs出不经过直径的以它开始的路径最大长度,记为dis
然后就可以求出直径之后枚举左右端点,设左端点l右端点r,直径上点距离直径上起点的距离用sum[]表示
则此时的$ans=max(max(sum[l],sum[End]-sum[r]),max(dis[i])l<=i<=r))$
右面那个玩意可以单调队列维护。
(然后我单调队列打反了变成维护最小值,GG。)
#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cstring>
#include<cstdlib>
#define maxn 600030
using namespace std;
inline long long read(){
long long num=,f=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-;
ch=getchar();
}
while(isdigit(ch)){
num=num*+ch-'';
ch=getchar();
}
return num*f;
} struct Edge{
int next,to,val;
}edge[maxn*];
int head[maxn],num;
inline void add(int from,int to,int val){
edge[++num]=(Edge){head[from],to,val};
head[from]=num;
} int dis[maxn];
bool vis[maxn];
int stack[maxn],top;
int sum[maxn];
int End;
int n,m; void find(int x,int fa){
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(to==fa) continue;
dis[to]=dis[x]+edge[i].val;
find(to,x);
if(dis[End]<dis[to]) End=to;
}
} void record(int x,int fa){
if(x==End){
stack[++top]=x;
vis[x]=;
return;
}
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(to==fa) continue;
record(to,x);
if(vis[to]){
stack[++top]=x;
sum[top]=sum[top-]+edge[i].val;
vis[x]=;
}
}
} void calc(){
find(,);
dis[End]=;
int Start=End;
find(End,End);
record(Start,Start);
} int dfs(int x,int fa){
int ans=;
for(int i=head[x];i;i=edge[i].next){
int to=edge[i].to;
if(to==fa||vis[to]==) continue;
ans=max(ans,dfs(to,x)+edge[i].val);
}
return ans;
} int que[maxn],h=,t; int main(){
n=read(),m=read();
for(int i=;i<n;++i){
int from=read(),to=read(),val=read();
add(from,to,val);
add(to,from,val);
}
calc();
for(int i=;i<=top;++i) dis[i]=dfs(stack[i],stack[i]);
int rig=;int ans=0x7fffffff;
for(int i=;i<=top;++i){
int le=sum[i];
while(sum[rig+]-sum[i]<=m&&rig<top){
rig++;
while(h<=t&&dis[que[t]]<=dis[rig]) t--;
que[++t]=rig;
while(h<=t&&que[h]<i) h++;
int now=max(le,max(sum[top]-sum[rig],dis[que[h]]));
ans=min(ans,now);
}
}
printf("%d\n",ans);
return ;
}
【Luogu】P2491消防(单调队列)的更多相关文章
- [SDOI2011]消防(单调队列,树的直径,双指针)
消防 2011年 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 某个国家有n个城市,这n个城市中任意两个都连通且有 ...
- luogu 2827 蚯蚓 单调队列/优先队列
易知可利用优先队列选取最大值: 但是通过分析可知,先取出的蚯蚓分开后仍然要比后分的长,所以可直接利用单调队列找队头即可,分三个单调队列,分别找未切割,切割,切割2三种情况 #include<bi ...
- luogu 2569 股票交易 单调队列dp
注意转移方程 分1.凭空买 2.不买不卖 3.在原来基础上买 4.在原来基础上卖 四种情况 head=1,tail=0;再判断一下head<=tail也可以 #include<bits/s ...
- Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列)
Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列) Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺 ...
- 【BZOJ2282】[Sdoi2011]消防 树形DP+双指针法+单调队列
[BZOJ2282][Sdoi2011]消防 Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这 ...
- Luogu【P1725】琪露诺(单调队列,DP)
本文是笔者第二篇解题报告.从现在开始,会将练的一些题发到博客上并归类到"解题报告"标签中. 琪露诺是这样一道题 这道题可以用纯DP做,但是据说会超时.(为什么?看起来过河这题比它数 ...
- BZOJ 2806 Luogu P4022 [CTSC2012]Cheat (广义后缀自动机、DP、二分、单调队列)
题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=2806 (luogu) https://www.luogu.org/pro ...
- [luogu P1776] 宝物筛选 解题报告(单调队列优化DP)
题目链接: https://www.luogu.org/problemnew/show/P1776 题目: 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF ...
- luogu 3084 单调队列+dp
注意处理出两个数组: r[i] 能覆盖i点的区间的左端点最小值(覆盖左侧最远处) l[i] i不能覆盖的区间的左端点左端点最大值 在该区间内寻找用来更新f[i] 答案的 j 即 l[i]<= j ...
随机推荐
- BSGS算法初探
前言 \(BSGS\)算法,全称\(Baby\ Step\ Giant\ Step\),即大小步算法.某些奆佬也称其为拔(Ba)山(Shan)盖(Gai)世(Shi)算法. 它的主要作用是求解形式如\ ...
- gearmand 编译 Unable to find libevent
如果出现configure: error: Unable to find libevent,则输入命令:yum -y install libevent libevent-devel然后重新config ...
- shell脚本,awk实现跳过文件里面的空行。
1.用awk '{if(!NF ){next}}1' file11 实现对文件里面的空行进行跳过操作,并输出结果. 2. awk '{if(!NF || /^#/){next}}1' file11 实 ...
- Springboot 入门创建hello world1!
1.首先使用工具是Eclipse,安装插件,点击“Help”-“Eclipse Marketplace...”, 一步步直接Ok,等待安装完成 2.创建Springboot项目 到此 就创建成功了 3 ...
- 二十三、MySQL 事务
MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息,如信箱,文章等等,这样,这些数 ...
- 水平垂直居中图片及文字(兼容IE6+)实例
直接看代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...
- 网站动态加载JS脚本
Demo_1 function loadJS(url, fn) { var ss = document.getElementsByName('script'), loaded = false; for ...
- 739. Daily Temperatures
https://leetcode.com/problems/daily-temperatures/description/ class Solution { public: vector<int ...
- May I see you again?【我可以再见到你吗?】
May I see you again "May I see you again?" he asked. There was an endearing nervousness in ...
- The Tower of Babylon UVA - 437 DAG上的动态规划
题目:题目链接 思路:每个方块可以用任意多次,但因为底面限制,每个方块每个放置方式选一个就够了,以x y为底 z 为高,以x z为底 y 为高,以y z为底 x为高,因为数据量很小,完全可以把每一种当 ...