【luogu P2491 [SDOI2011]消防】 题解
题目链接:https://www.luogu.org/problemnew/show/P2491
题外话:
OI一共只有三种题——会的题,不会的题,二分题。
题解:
step 1 求树的直径,把树的直径上的路径边权都置为0,这样了再求一次其他点最短路。
step 2 在树的直径上二分,具体方法是把树的直径长度用类似前缀和的思想处理后,二分左右端点舍去的距离。
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 300010;
int n, dis[maxn], ans = 0x7fffffff, node, pre[maxn], path[maxn], tot, D, sta[maxn], top, s, S;
bool vis[maxn];
struct edge{
int from, to, next, len;
}e[maxn<<2];
int cnt, head[maxn];
void add(int u, int v, int w)
{
e[++cnt].from = u;
e[cnt].next = head[u];
e[cnt].len = w;
e[cnt].to = v;
head[u] = cnt;
}
queue<int> q;
void bfs(int s)
{
vis[s] = 1;
q.push(s);
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i != -1; i = e[i].next)
{
if(vis[e[i].to] == 0)
{
pre[e[i].to] = now;
dis[e[i].to] = dis[now] + e[i].len;
vis[e[i].to] = 1;
q.push(e[i].to);
if(dis[e[i].to] > D)
{
D = dis[e[i].to];
node = e[i].to;
}
}
}
}
}
void SPFA(int s)
{
memset(vis, 0, sizeof(vis));
memset(dis, 127, sizeof(dis));
dis[s] = 0;
vis[s] = 1;
q.push(s);
while(!q.empty())
{
int now = q.front(); q.pop();
vis[now] = 0;
for(int i = head[now]; i != -1; i = e[i].next)
{
if(dis[e[i].to] > dis[now] + e[i].len)
{
dis[e[i].to] = dis[now] + e[i].len;
if(!vis[e[i].to])
{
q.push(e[i].to);
vis[e[i].to] = 1;
}
}
}
}
}
bool check(int D)
{
int l = 1, r = top;
while(sta[1] - sta[l+1] <= D && l <= top) l++;
while(sta[r-1] <= D && r >= 1) r--;
//cout<<l<<" "<<r<<endl;
return sta[l] - sta[r] <= S;
}
int main()
{
memset(head, -1, sizeof(head));
int u, v, w;
cin>>n>>S;
for(int i = 1; i < n; i++)
{
scanf("%d%d%d",&u, &v, &w);
add(u, v, w);
add(v, u, w);
}
memset(dis, 0, sizeof(dis));
memset(vis, 0, sizeof(vis));
D = 0;
bfs(1);
int s = node;
memset(vis, 0, sizeof(vis));
dis[node] = 0;
D = 0;
bfs(s);
int x = node;
D = dis[x];
sta[++top] = D;
path[++tot] = x;
while(x != s)
{
path[++tot] = pre[x];
sta[++top] = dis[pre[x]];
x = pre[x];
}
//for(int i = 1; i <= n; i++) cout<<path[i]<<" ";
for(int i = 2; i <= tot; i++)
{
add(path[i-1], path[i], 0);
add(path[i], path[i-1], 0);
}
SPFA(x);
int l = 0, r = D;
for(int i = 1; i <= n; i++) l = max(l, dis[i]);
if(S < D)
{
while(l <= r)
{
int mid = (l + r) >> 1;
if(check(mid)) r = mid - 1;
else l = mid + 1;
}
}
//for(int i = 1; i <= n; i++) cout<<dis[i];
printf("%d\n", l);
return 0;
}
附:
sdoi居然出提高组原题(#滑稽 不过是数据扩大了1000倍
据说那个题想怎么暴怎么暴(逃
【luogu P2491 [SDOI2011]消防】 题解的更多相关文章
- [洛谷P2491] [SDOI2011]消防
洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超 ...
- P1099 树网的核 && P2491 [SDOI2011]消防
给定一棵树, 你可以在树的直径上确定一条长度不超过 \(S\) 的链, 使得树上离此链最长的点距离最小, 输出这个距离 P2491 数据范围为 P1099 的 \(1000\) 倍 Solution ...
- 洛谷 P1099 树网的核+P2491 [SDOI2011]消防
写在前面:由于是双倍经验就放一块了,虽然数据范围差的有点大. 题目链接 题意:在树的直径上选择一条长度不超过s的路径使这条路径上的点到树上任意点的最大距离最小. 这题数据好像非常水,我写了上界n^2不 ...
- luogu 2491 [SDOI2011]消防 / 1099 树网的核 单调队列 + 树上问题
Code: #include<bits/stdc++.h> #define ll long long #define maxn 300001 #define inf 1000000000 ...
- [Luogu 2486] SDOI2011 染色
[Luogu 2486] SDOI2011 染色 树剖水题,线段树维护. 详细题解不写了. 我只想说我写的线段树又变漂亮了qwq #include <algorithm> #include ...
- 【BZOJ2282】[Sdoi2011]消防 树形DP+双指针法+单调队列
[BZOJ2282][Sdoi2011]消防 Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这 ...
- [SDOI2011]消防(树的直径)
[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情, ...
- Bzoj 2282: [Sdoi2011]消防(二分答案)
2282: [Sdoi2011]消防 Time Limit: 10 Sec Memory Limit: 512 MB Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条 ...
- [SDOI2011]消防(贪心,图论,树的直径)
[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情, ...
随机推荐
- centos下如何停止ping命令
ctrl + c 或者 Ctrl + d(好像不行) man ping
- Servlet开发(三)之ServletConfig,ServletContext
1. ServletConfig Servlet是开发动态web的技术,而web.xml是Tomcat工程中最基础也最重要的配置文件,Tomcat启动项目的时候会加载并读取这个文件,其中web.xml ...
- 三、Bean的初始化
一.使用构造器实例化Bean:这是最简单的方式,Spring IOC容器既能使用默认空构造器也能使用有参构造器两种方式创建bean 空构造器 <bean name="bean1&quo ...
- zoj 3524(拓扑排序+多重背包)(好题)
http://blog.csdn.net/woshi250hua/article/details/7824773 题目大意:从前有n座山,山里都有一座庙,庙里都有一个老和尚,老和尚专送纪念品,每个纪念 ...
- 合理使用线程池 ThreadPool.QueueUserWorkItem()
//==>自建线程 new Thread(() => { //线程任务 Console.WriteLine(Thread.CurrentThread.ManagedThreadId); } ...
- Maven打包时,不包含jar包
在给Maven项目打war包时,如果不想把依赖中的jar包也包含进去,可以在plugins中加入 <span style="white-space:pre"> < ...
- MySQL root 密码修改
小伙伴要在以前的服务器上装个代码版本控制的软件,要用到数据库,可是想来找去root密码还是忘了,其他已经安装的服务都是用的专用账户配置文件里要找不到root用户的密码.用以下方法将密码强制修改掉: 1 ...
- chrome中常用的快捷键
ctrl+n 新建窗口ctrl+shift+n 无痕模式新建窗口ctrl+t 打开新标签页ctrl+shift+t 打开最近关闭的标签页ctrl+tab 标签页之间切换ctrl+w/ctrl+F4 关 ...
- 子div设置float后会导致父div无法自动撑开
本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 注: 文章部分转载 彩泉 - 博客园 原因:内部的DIV因为float:l ...
- easyui numberbox输入框 编辑不可编辑的切换
背景:申请单里需要选费用类型,费用类型有的有子明细项,有个合计项 当有子明细项的时候,合计项的值是通过弹出的子明细项价格的总和(设置为可编辑没问题,因为点击出来弹框,编辑不了) 没有子明细 ...