题目链接: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. 域对象中属性变更及感知session绑定的事件监听器

    域对象中属性的变更的时间监听器就是用来监听ServletContext,HttpSession,HttpServletRequest这三个对象中的属性变更信息事件的监听器.这三个监听器接口分别是Ser ...

  2. node.js内存缓存的性能情况

    1. WEB 服务性能测试和优化 1.1   测试环境搭建 网络环境:内网 压力测试服务器: 服务器系统:Linux 2.6.18 服务器配置:Intel® Xeon™ CPU 3.40GHz 4 C ...

  3. Javascript获取页面表格中的数据

    var main=mygrid.gettable("11"); //表示获取非固定列的表格 var main1=mygrid.gettable("01");// ...

  4. Python中@修饰符的作用。

    '@'符号用作函数修饰符是python2.4新增加的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.也就是说@A def f(): 是非法的. 只可以在模块或类定义层内对函数进行修饰, ...

  5. 【转】JSON.parse()与JSON.stringify()的区别

    JSON.parse()[从一个字符串中解析出json对象] 例子: //定义一个字符串 var data='{"name":"goatling"}' //解析 ...

  6. Android 低版本sdk中没有getSupportedPreviewSizes和getSupportedPictureSizes函数怎么办?

    在做camera和SurfaceView做摄像头程序时,需要获取camera支持的相片大小,在低版本sdk中没有getSupportedPictureSizes函数,怎么办呢,请参阅下面的关键代码: ...

  7. ie6 浏览器的bug

    1.IE6不支持连续类的交集选择器 1 #box.box.box1{ 2             width: 200px; 3             height: 200px; 4       ...

  8. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    viewport:移动设备上用来显示网页的那部分区域,但其大小并不局限于那部分可视区域 width设置layout viewport  的宽度,为一个正整数,或字符串"device-widt ...

  9. jdk1.8 对数组及arrays类对数组的操作与增强

    数组的初始化有两种方式 静态初始化: 初始化时由程序员显示置顶每个数组的初始值,由系统决定数组长度.如: int[] a1 = new int[] {1,2,3,4}; 动态初始化:初始化时由程序员只 ...

  10. dubbo学习总结二 服务端

    服务端主要执行对底层数据库的操作 主要分层为 api +dao+ filter+ util+... 首先 dubbo 服务端有一个dubbo配置文件 dubbo:application 定义应用名称 ...