BZOJ2282: [Sdoi2011]消防
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2282
答案一定是在直径上的一段,然后答案一定不会小于不在直径上的点到直径的距离(要是可以的话那当前这条直径就不是直径了)
然后二分一遍,当前段的最长答案只可能在s->l,r->t取到,其他都是取不到的所以判断这两个就可以了。
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<set>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define maxn 300500
#define inf int(1e9)
#define mm 1000000007
typedef long long ll;
using namespace std;
struct data{int obj,pre;ll c;
}e[maxn*];
int dis[maxn],num[maxn],D,l,r,mid,S;
int n,tot,cnt,s,t,st[maxn],head[maxn],fa[maxn],mark[maxn];
int read(){
int x=,f=; char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-; ch=getchar();}
while (isdigit(ch)){x=x*+ch-''; ch=getchar();}
return x*f;
}
void insert(int x,int y,int z){
e[++tot].obj=y; e[tot].pre=head[x]; e[tot].c=z; head[x]=tot;
}
void bfs(int s){
rep(i,,n) dis[i]=-; dis[s]=;
queue<int> q; q.push(s);
while (!q.empty()){
int u=q.front(); q.pop();
for (int j=head[u];j;j=e[j].pre){
int v=e[j].obj;
if (dis[v]==-) {
fa[v]=u;
if (!mark[v]) dis[v]=dis[u]+e[j].c; else dis[v]=dis[u];
q.push(v);
}
}
}
}
bool jud(int D){
int l=,r=cnt;
while (l<cnt&&num[]-num[l+]<=D) l++;
while (r>&&num[r-]<=D) r--;
return num[l]-num[r]<=S;
}
int main(){
// freopen("in.txt","r",stdin);
n=read(); S=read();
rep(i,,n-){
int x=read(),y=read(); int z=read();
insert(x,y,z); insert(y,x,z);
}
bfs(); rep(i,,n) if (dis[i]>dis[s]) s=i;
bfs(s); rep(i,,n) if (dis[i]>dis[t]) t=i;
D=dis[t];
int now=t; st[++cnt]=t; num[cnt]=dis[t];
while (now!=s){
now=fa[now]; mark[now]=;
st[++cnt]=now;
num[cnt]=dis[now];
}
bfs(s);
rep(i,,n) l=max(l,dis[i]); r=D;
if (S<D){
while (l<=r){
ll mid=(l+r)/;
if (jud(mid)) r=mid-;
else l=mid+;
}
}
printf("%d\n",l);
return ;
}
BZOJ2282: [Sdoi2011]消防的更多相关文章
- BZOJ2282 SDOI2011消防/NOIP2007树网的核(二分答案+树形dp)
要求最大值最小容易想到二分答案.首先对每个点求出子树中与其最远的距离是多少,二分答案后就可以标记上一些必须在所选择路径中的点,并且这些点是不应存在祖先关系的.那么如果剩下的点数量>=3,显然该答 ...
- NOIP2007 树网的核 && [BZOJ2282][Sdoi2011]消防
NOIP2007 树网的核 树的直径的最长性是一个很有用的概念,可能对一些题都帮助. 树的直径给定一棵树,树中每条边都有一个权值,树中两点之间的距离定义为连接两点的路径边权之和.树中最远的两个节点之间 ...
- 【BZOJ2282】[Sdoi2011]消防 树形DP+双指针法+单调队列
[BZOJ2282][Sdoi2011]消防 Description 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这 ...
- [洛谷P2491] [SDOI2011]消防
洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有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). 这个国家的人对火焰有超越宇宙的热情, ...
- BZOJ1999或洛谷1099&BZOJ2282或洛谷2491 树网的核&[SDOI2011]消防
一道树的直径 树网的核 BZOJ原题链接 树网的核 洛谷原题链接 消防 BZOJ原题链接 消防 洛谷原题链接 一份代码四倍经验,爽 显然要先随便找一条直径,然后直接枚举核的两个端点,对每一次枚举的核遍 ...
- 【bzoj2282】[Sdoi2011]消防
两次bfs可得直径,答案一定不会小于所有点到直径的距离最大值,只要把直径上的边权设为0,任选直径上一点bfs可得将最大值作为二分下界,二分直径左右端点的舍弃部分 #include<algorit ...
随机推荐
- 解决IOS iframe不滚动问题
.frameBox{ position: fixed; top: 0; left: 0; right: 0; bottom: 0; -webkit-overflow-scrolling: touch; ...
- iOS MJRefresh上拉加载更多
1.导入MJRefresh包 2.在类中引入:#import "MJRefresh.h" 3.添加footerView 添加加载更多的UI样式: MJRefreshAutoNorm ...
- Cat 跨线程之 TaggedTransaction 用法和原理分析
代码 package com.dianping.cat.message.internal; import com.dianping.cat.Cat; import com.dianping.cat.m ...
- Oracle 用户操作表权限
grant select any table to xxx 将使得xxx用户能够查看到所有用户的表:正确的授权不能是这样: 用户是隔离表的schema,授权时..
- 自动生成getter,setter方法的插件lombok
1.在InteiliJ IDEA上安装lombok插件,并重启 . 2.在pom.xml文件中添加依赖 <dependency> <groupId>org.project ...
- Django__RBAC
RBAC : 基于角色的权限访问控制(Role-Based Access Control) RBAC 模型作为目前最为广泛接受的权限模型 角色访问控制(RBAC)引入了Role的概念,目的是为了隔离U ...
- webpack之loader实践
初识前端模板概念的开发者,通常都使用过underscore的template方法,非常简单好用,支持赋值,条件判断,循环等,基本可以满足我们的需求. 在使用Webpack搭建开发环境的时候,如果要使用 ...
- 安卓电量优化之AlarmManager使用全部解析
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.AlarmManager概述 AlarmManager是安卓系统中一种系统级别的提示服务,可以在我们设定时间或者周期性的执行一个intent,这 ...
- Ubuntu16.04安装配置sublime text3
1.安装Sublime Text 3 首先添加sublime text 3的仓库: sudo add-apt-repository ppa:webupd8team/sublime-text-3 根据提 ...
- onoffswitch-checkbox
@foreach (EmailSubscription es in Model) { if(true){ <div class="onoffswitch"> ...