【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). 这个国家的人对火焰有超越宇宙的热情, ...
随机推荐
- Java基础(10)——小结与填坑
前面都写了9篇啦,虽然断断续续发了半个月,写着写着会发现每篇中都有些比较重要的地方没有讲到~这篇还是需要填一填目前我已发现的坑了~ 一. 小结 Java编译命令 javac.运行命令java java ...
- spring历史和哲学
spring 历史: 2004年 Spring Framework 1.0 final 正式问世. 1.在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分 ...
- Hibernate 学习(三)
一.关系映射 实体类之间的关联映射以及表之间的关系是 ORM 的灵魂之处.对象间的关系的子集可以用下列四种方式解释.关联映射可以是单向的也可以是双向的. 映射类型 描述 Many-to-One 使用 ...
- css样式的优先顺序
一.css样式的权重:!important(1000+) > 内联样式( 1000 ) > ID选择器(100 ) > 类选择器(10) > 标签选择器( 1 ) > ...
- NodeJS require路径
项目需要用nodejs,感觉nodejs是前端装逼神器了,是通向全栈工程师的必经之路哇,接下来开始踏上学习nodejs的征程.下面是第一个hello,world的程序. 1.server.js文件,这 ...
- Python入门-再谈编码
一.小数据池 在说小数据池之前. 我们先看一个概念. 什么是代码块: 根据提示我们从官方文档找到了这样的说法: A Python program is constructed from code bl ...
- C语言输入语句scanf与fgets linux下
1.测试使用scanf的一个例子: #include "stdio.h" #include "string.h" int main() { char name[ ...
- 论组织资源 #F315
论组织资源 #F315 2016-02-29 yevon_ou 水库论坛 论组织资源 #F315 首先,让我们看一段长微博. 转:五毛大战美分,基层民猪那破事... 就是小区业委会的事,根据业委会相 ...
- vscode环境配置
"go.goroot": "/home/ken/go", "go.gopath": "/home/ken/gopath" ...
- What’s the difference between a stack and a heap?
http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/ The ...