题目链接: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]消防】 题解的更多相关文章

  1. [洛谷P2491] [SDOI2011]消防

    洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超 ...

  2. P1099 树网的核 && P2491 [SDOI2011]消防

    给定一棵树, 你可以在树的直径上确定一条长度不超过 \(S\) 的链, 使得树上离此链最长的点距离最小, 输出这个距离 P2491 数据范围为 P1099 的 \(1000\) 倍 Solution ...

  3. 洛谷 P1099 树网的核+P2491 [SDOI2011]消防

    写在前面:由于是双倍经验就放一块了,虽然数据范围差的有点大. 题目链接 题意:在树的直径上选择一条长度不超过s的路径使这条路径上的点到树上任意点的最大距离最小. 这题数据好像非常水,我写了上界n^2不 ...

  4. luogu 2491 [SDOI2011]消防 / 1099 树网的核 单调队列 + 树上问题

    Code: #include<bits/stdc++.h> #define ll long long #define maxn 300001 #define inf 1000000000 ...

  5. [Luogu 2486] SDOI2011 染色

    [Luogu 2486] SDOI2011 染色 树剖水题,线段树维护. 详细题解不写了. 我只想说我写的线段树又变漂亮了qwq #include <algorithm> #include ...

  6. 【BZOJ2282】[Sdoi2011]消防 树形DP+双指针法+单调队列

    [BZOJ2282][Sdoi2011]消防 Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这 ...

  7. [SDOI2011]消防(树的直径)

    [SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情, ...

  8. Bzoj 2282: [Sdoi2011]消防(二分答案)

    2282: [Sdoi2011]消防 Time Limit: 10 Sec Memory Limit: 512 MB Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条 ...

  9. [SDOI2011]消防(贪心,图论,树的直径)

    [SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情, ...

随机推荐

  1. 2019 Java面试题

    马上又是一个金九银十的招聘旺季,小编在这里给大家整理了一套各大互联网公司面试都喜欢问的一些问题或者一些出场率很高的Java面试题,给在校招或者社招路上的你一臂之力. 首先我们需要明白一个事实,招聘的一 ...

  2. 微服务-分布式日志系统Logstash部署

    参考资料: 1 .Logstash中文官网 2. 阿里云Elasticsearch> 最佳实践 > logstash部署 3. logstash.elasticsearch.kibana搭 ...

  3. db缓存设计

    http://www.cnblogs.com/herm/archive/2012/11/11/2773887.html

  4. 第5章 css与背景相关的样式background

    background-origin 设置元素背景图片的原始起始位置. 语法: background-origin : border-box | padding-box | content-box; 参 ...

  5. FLASK实现上传下载功能

    #!-*-coding=utf-8-*- # from flask import Flask # # app = Flask(__name__) # # # @app.route('/') # def ...

  6. SQLServer导入大sql文件报错 对 COM 组件的调用返回了错误 HRESULT E_FAIL。 (mscorlib)

    打开cmd执行(d:\script.sql为sql文件位置):  sqlcmd -S 127.0.0.1 -U sa -P sa -i d:\script.sql    From:https://ww ...

  7. Windows API 编程----EnumWindows()函数的用法

    1. 函数原型: BOOL WINAPI EnumWindows( _In_ WNDENUMPROC lpEnumFunc, _In_ LPARAM lParam); lpEnumFunc: 应用程序 ...

  8. 01_NIO基本概念

    [NIO的几个概念] Buffer(缓冲区) Channel(通道,管道) Selector(选择器,多路复用器) [Buffer] Buffer是一个对象,它包括一些要写入或者要读取的数据.在NIO ...

  9. sprintf详解

    原文:http://www.cnblogs.com/wqlblogger/archive/2007/01/09/615525.html 转摘声明:选自<CSDN 社区电子杂志——C/C++杂志& ...

  10. Raft协议--中文论文介绍

    本篇博客为著名的 RAFT 一致性算法论文的中文翻译,论文名为<In search of an Understandable Consensus Algorithm (Extended Vers ...