Description

在一条边上求一个点,使得这个点到所有点的最长的最短距离 最短. \(n \leqslant 10^5\)

Sol

Dijkstra+扫描线+单调队列.

这个好像叫什么最小直径生成树?不过变成了在一个边上...

对于一个点,我们可以求出当分割点在哪个位置的时候,它应该到 \(A\) ,什么时候它应该到 \(B\) .

\(dis_A(i)+t \leqslant dis_B(j)+(dis_{AB}-t)\)

移项得 \(t\leqslant \frac{dis_B(i)-dis_A(i)+dis_{AB}}{2}\)

这样就把线段 \(AB\) 分成了几个区间,枚举区间,然后分别用两个单调队列维护一下到分割点左右到 \(A,B\) 距离的最大值.

对于一个区间,我们可以 \(O(1)\) 的求出在这个区间应该选择哪个点作为分割点,因为两边的点都是确定的最大距离也知道,然后统计答案就可以了.

Code

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<functional>
#include<iostream>
using namespace std; #define mpr(a,b) make_pair(a,b)
#define debug(a) cout<<#a<<"="<<a<<" "
#define Fst first
#define Sec second
typedef long long LL;
typedef pair< LL,int > pr;
const int N = 100005; LL T,n,m,k,A,B,X;
vector<pr> g[N];
LL ds[N],dt[N];
pair< double,int > p[N];double lm[N];
pair< double,double > ans;
int q1[N],q2[N],h1,h2,t1,t2;
priority_queue<pr,vector<pr>,greater<pr> > q; inline LL in(LL x=0,char ch=getchar()){ while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();return x; }
void clr(){
t1=t2=0,h1=h2=1;ans=mpr(1e18,-1);
for(int i=0;i<N;i++) g[i].clear();
}
void Up(int i,int j){
double l=max(min((X+ds[j]+dt[i])/2.0-ds[j],lm[j]),lm[i]),tmp=max(ds[j]+l,X-l+dt[i]);
// debug(i),debug(j),debug(dt[i]),debug(ds[j]),debug((X+ds[j]+dt[i])/2.0-dt[i]),debug(lm[i]),debug(lm[j]),debug(l),debug(tmp)<<endl;
if(tmp < ans.Fst||(tmp == ans.Fst && l<ans.Sec)) ans=mpr(tmp,l);
}
void Dijkstra(int s,LL *d){
static bool b[N];memset(b,0,sizeof(b));for(int i=1;i<=n;i++) d[i]=1000000000000000000LL;
d[s]=0,q.push(mpr(0,s));
for(int u;!q.empty();){
u=q.top().Sec,q.pop();if(b[u]) continue;b[u]=1;
for(int i=0;i<g[u].size();i++){
LL p=g[u][i].Fst;int v=g[u][i].Sec;
if(d[u]+p < d[v]) d[v]=d[u]+p,q.push(mpr(d[v],v));
}
}
}
int main(){
for(T=in();T--;){
clr();
n=in(),m=in(),k=in();
for(int i=1,u,v,x;i<=m;i++){
u=in(),v=in(),x=in();
g[u].push_back(mpr(x,v)),g[v].push_back(mpr(x,u));
if(i == k) A=u,B=v,X=x;
}
Dijkstra(A,ds),Dijkstra(B,dt); // cout<<X<<endl; // for(int i=1;i<=n;i++) cout<<ds[i]<<" ";cout<<endl;
// for(int i=1;i<=n;i++) cout<<dt[i]<<" ";cout<<endl; for(int i=1;i<=n;i++) p[i]=mpr((dt[i]-ds[i]+X)/2.0,i),lm[i]=(dt[i]-ds[i]+X)/2.0;
sort(p+1,p+n+1); // for(int i=1;i<=n;i++) printf("%lf %d\n",p[i].Fst,p[i].Sec); for(int i=1;i<=n;i++){
while(h2<=t2 && ds[q2[t2]] <= ds[p[i].Sec]) t2--;
q2[++t2]=p[i].Sec;
}
// cout<<ds[q2[h2]]+X<<endl;
ans=mpr(ds[q2[h2]],0);
for(int i=1,r;i<=n;i=r+1){
r=i;
while(p[r].Fst == p[r+1].Fst) r++;
/// debug(r)<<endl;
for(int j=i;j<=r;j++){ while(h1<=t1 && dt[q1[t1]] <= dt[p[j].Sec]) t1--;q1[++t1]=p[j].Sec; }
for(int j=i;j<=r;j++) if(q2[h2] == p[j].Sec) h2++;
// debug(q1[h1]),debug(q2[h2])<<endl;
Up(q1[h1],q2[h2]);
}
if(dt[q1[h1]]<ans.Fst) ans=mpr(dt[q1[h1]],X);
printf("%.5lf %.5lf\n",ans.Sec,ans.Fst);
}
return 0;
}

  

HackerRank savita-and-friends的更多相关文章

  1. 日常小测:颜色 && Hackerrank Unique_colors

    题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...

  2. HackerRank "Square Subsequences" !!!

    Firt thought: an variation to LCS problem - but this one has many tricky detail. I learnt the soluti ...

  3. HackerRank "Minimum Penalty Path"

    It is about how to choose btw. BFS and DFS. My init thought was to DFS - TLE\MLE. And its editorial ...

  4. HackerRank "TBS Problem" ~ NPC

    It is marked as a NPC problem. However from the #1 code submission (https://www.hackerrank.com/Charl ...

  5. HackerRank Extra long factorials

    传送门 今天在HackerRank上翻到一道高精度题,于是乎就写了个高精度的模板,说是模板其实就只有乘法而已. Extra long factorials Authored by vatsalchan ...

  6. HackerRank "Lucky Numbers"

    Great learning for me:https://www.hackerrank.com/rest/contests/master/challenges/lucky-numbers/hacke ...

  7. HackerRank "Playing with numbers"

    This is 'Difficult' - I worked out it within 45mins, and unlocked HackerRank Algorithm Level 80 yeah ...

  8. HackerRank "The Indian Job"

    A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...

  9. HackerRank "Array and simple queries" !

    The most interesting, flexible and juicy binary tree problem I have ever seen. I learnt it from here ...

  10. HackerRank "Self Balancing Tree"

    Something to learn: Rotation ops in AVL tree does not require recursion. https://github.com/andreima ...

随机推荐

  1. url下载网页的三种方法

    # -*- coding: utf-8 -*- import cookielib import urllib2 url = "http://www.baidu.com" print ...

  2. VM安装linux

      看图简单流程即可.注意磁盘空间至少30G,实用oracle数据库时需要更大,可以后期增加.   静待安装完成即可.一定记得创建的用户名及密码,及root用户的密码.

  3. Blue tooth

    一 . nordic BLE4.0 1.开发nordic的应用需要安装支持keil的pack库和插件 2.nordic的SDK很完整,实例涵盖了几乎所有的应用 https://www.nordicse ...

  4. Windows溢出提权小结

    1.  查看系统打补丁情况:systeminfo 2.  查看KB-EXP表: KB2360937 MS10-084 KB2478960 MS11-014 KB2507938 MS11-056 KB2 ...

  5. struts2进阶篇(2)

    一.Action与MVCstruts2是一个基于MVC的web应用框架,它将应用程序分为三个组件:模型,视图,控制器.模型:包含了应用程序的业务逻辑和业务数据,由封装数据和处理业务的javaBean组 ...

  6. Java中符号位扩展

    第一个例子: byte b=-100;b在内存中是以补码的形式存贮的:1001 1100 如果执行char c=(char)b;如3楼企鹅先生所说:b要先变为int,这时增加的位全要用b的符号位填充( ...

  7. [MongoDB]count,gourp,distinct

    摘要 上篇文章介绍了CRUD的操作,会了这些,基本上可以完成很多工作了.但如果遇到统计类的操作,那么就需要学习下本篇的内容了. 相关文章 [MongoDB]入门操作 [MongoDB]增删改查 cou ...

  8. 关于标准C语言的预定义宏【转】

    标准C语言预处理要求定义某些对象宏,每个预定义宏的名称一两个下划线字符开头和结尾,这些预定义宏不能被取消定义(#undef)或由编程人员重新定义.下面预定义宏表,被我抄了下来. __LINE__  当 ...

  9. asp.net mvc 实现博客的时间分类管理

    先看效果 这个其实用c#实现起来比较简单: Sides = bllSession.IArticleBLL.GetList("") .Select(a => a.Time) . ...

  10. jquery 选择器 -高级使用 新的 心得

    jQuery的each函数: each函数等同于c语言中的for函数: 里面每次循环的 "context 上下文" == 当前的dom ,可以使用this, 也可以使用$(this ...