CF266D. BerDonalds [图的绝对中心]
5 seconds
256 megabytes
standard input
standard output
BerDonalds, a well-known fast food restaurant, is going to open a cafe in Bertown. The important thing is to choose the new restaurant's location so that it would be easy to get there. The Bertown road system is represented by n junctions, connected by m bidirectional roads. For each road we know its length. We also know that we can get from any junction to any other one, moving along the roads.
Your task is to find such location of the restaurant, that the shortest distance along the roads from the cafe to the farthest junction would be minimum. Note that the restaurant can be located not only on the junction, but at any point of any road.
The first line contains two integers n and m () — the number of junctions and the number of roads, correspondingly. Then m lines follow, describing all Bertown roads. Each road is described by three integers ai, bi, wi(1 ≤ ai, bi ≤ n, ai ≠ bi; 1 ≤ wi ≤ 105), where ai and bi are the numbers of the junctions, connected by the i-th road, and wi is the length of the i-th road.
It is guaranteed that each road connects two distinct junctions, there is at most one road between any two junctions, and you can get from any junction to any other one.
Print a single real number — the shortest distance from the optimal restaurant location to the farthest junction. The answer will be considered correct, if its absolute or relative error doesn't exceed 10 - 9.
比赛的时候想了一种做法;
先floyd,然后枚举每条边,在边上二分一个点使得这个点到其他点最大距离最小
但是一直WA,问题竟然是,这不是个单调函数是个单峰函数应该用三分!!!
然后貌似这个道卡三分了
二分是可以做的,直接二分答案ans,然后也是枚举每条边让其他点向这条边走ans距离,没有走到的地方覆盖,最后全覆盖了就是不可行
但是有复杂度更低的做法O(n^3) 参考http://www.cnblogs.com/lzqxh/archive/2013/03/05/2944834.html#2659117
枚举边后,对于每个点距离是min{d[u][i]+x,d[v][i]+L-x},是单峰函数,并且是一个折,所有点画出来长这样
最优解一定在最上面那条线的最低点取到
那就找所有交点
按照d[u][]大到小排序后,对于先后两个点a和b,他们的图像出现交点的条件就是d[v][a]<d[v][b]
还有一点,这个距离最多是个0.5 (这也解释了为什么本题精度那么奇怪)
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- const int N=,M=2e4+,INF=1e9+;
- inline int read(){
- char c=getchar();int x=,f=;
- while(c<''||c>''){if(c=='-')f=-; c=getchar();}
- while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
- return x*f;
- }
- int n,m,u,v,w;
- struct data{
- int u,v,w;
- }a[M];
- int d[N][N];
- void floyd(){
- for(int k=;k<=n;k++)
- for(int i=;i<=n;i++)
- for(int j=;j<=n;j++)
- d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
- }
- int ans=INF;
- int rnk[N][N],val[N];
- bool cmp(int a,int b){return val[a]<val[b];}
- void solve(){
- floyd();
- for(int i=;i<=n;i++){
- for(int j=;j<=n;j++) rnk[i][j]=j,val[j]=d[i][j];
- sort(rnk[i]+,rnk[i]++n,cmp);
- //for(int j=1;j<=n;j++) printf("rnk %d %d %d\n",i,j,rnk[i][j]);
- }
- for(int i=;i<=n;i++) ans=min(ans,d[i][rnk[i][n]]<<);
- for(int i=;i<=m;i++){
- u=a[i].u,v=a[i].v,w=a[i].w;
- for(int p=n,i=n-;i>=;i--){
- if(d[v][rnk[u][i]]>d[v][rnk[u][p]]){//rnk[u][i] and rnk[u][p] has intersectioin
- ans=min(ans,d[u][rnk[u][i]]+d[v][rnk[u][p]]+w);
- p=i;
- }
- }
- }
- printf("%lf",ans/2.0);
- }
- int main(){
- n=read();m=read();
- for(int i=;i<=n;i++) for(int j=i+;j<=n;j++) d[i][j]=d[j][i]=INF;
- for(int i=;i<=m;i++){
- a[i].u=read(),a[i].v=read(),a[i].w=read();
- d[a[i].u][a[i].v]=d[a[i].v][a[i].u]=a[i].w;
- }
- solve();
- }
CF266D. BerDonalds [图的绝对中心]的更多相关文章
- UML动态模型图简单介绍
UML动态模型图描述了系统动态行为的各个方面,包括用例图.序列图.协作图.活动图和状态图.下面就每种图做一个简单介绍: 用例图 用例图描述系统外部的执行者与系统提供的用例之间的某种联系.所谓用例是指对 ...
- iOS10 UI教程视图的中心位置
iOS10 UI教程视图的中心位置 center表示的是视图的中心位置属性,这个属性在相对的UI层次结构上工作,和frame类似.center属性是一个在父视图上定义视图的位置的简便方法.center ...
- 云计算和大数据时代网络技术揭秘(八)数据中心存储FCoE
数据中心存储演化——FCoE 数据中心三大基础:主机 网络 存储 在云计算推动下,存储基础架构在发生演变 传统存储结构DAS.SAN在发展中遇到了布线复杂.能耗增多的缺点(原生性),需要对架构做根 ...
- [转载] Google数据中心网络技术漫谈
原文: http://www.sdnlab.com/12700.html?from=timeline&isappinstalled=0#10006-weixin-1-52626-6b3bffd ...
- prefuse学习(二)显示一张图
1. 把数据以点连线的方式在画面中显示 2. 数据按照数据的性别属性使用不同的颜色 3. 鼠标左键可以把图在画面中拖动 4. 鼠标右键可以把图放大或者缩小 5. 鼠标单击某个数据上,该数据点 ...
- H3C数据中心虚拟化解决方案技术白皮书
缩略语清单: 缩略语 英文全名 中文解释 IDC Internet Data Center 互联网数据中心 VRF Virtual Router Forwarding 虚拟路由器转发 SMP Symm ...
- 图像Resize中0.5像素中心对齐的问题
目录 0.5像素对齐的问题 0.5像素对齐的问题 1. 问题提出 在进行图像缩放时,偶尔会看到一些比较奇怪的代码,其中有一个就是0.5像素中心对齐的问题,例如在OpenCV线性插值的代码中有类似如下操 ...
- 【GNN】图神经网络小结
图神经网络小结 图神经网络小结 图神经网络分类 GCN: 由谱方法到空域方法 GCN概述 GCN的输出机制 GCN的不同方法 基于谱方法的GCN 初始 切比雪夫K阶截断: ChebNet 一阶Cheb ...
- 思维导图VS金字塔原理
作为常识,思维导图制作的核心元素是关键词,而金字塔原理制作的核心元素则是拓展的概要句子,这两种方式是当今人们常用的思维工具,本文对其做了对比,希望对你的选择有所帮助. 金字塔原理结构:从上到下三角形结 ...
随机推荐
- IntelliJ IDEA 配置 smartGit
教你如何在IntelliJ IDEA中配置smartGit? 一.第一种方式: 1.在启动IDEA工具时,点击下拉按钮"Check out from Version Control" ...
- [国嵌笔记][029][ARM处理器启动流程分析v2]
2440启动流程 启动方式:nor flash启动.nand flash启动 地址布局: 选择nor flash启动时,SROM(nor flash)地址为0x00000000 选择nand flas ...
- TypeScript笔记 1--环境搭建
TypeScript是什么 Typescript是JavaScript的超集,支持ES6特性并且提供了类型系统,可以编译成Javascript.是微软开发且已经在github上开源. ES6(ES20 ...
- NYOJ 1248 海岛争霸(Dijkstra变形——最短路径最大权值)
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=1248 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比 ...
- POJ 1502 MPI Maelstrom(模板题——Floyd算法)
题目: BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distri ...
- java序列化反序列化深入探究
When---什么时候需要序列化和反序列化: 简单的写一个hello world程序,用不到序列化和反序列化.写一个排序算法也用不到序列化和反序列化.但是当你想要将一个对象进行持久化写入文件,或者你想 ...
- Thinkphp+Nginx(PHPstudy)下报的404错误,403错误解决
最近一个TP5的项目说放到Nginx下测试看看,下载个 PHPstudy,放到WWW下,配置好域名,直接给个报个404: 解决方法: 1.先在phpstudy下配置好域名目录指向项目下的public下 ...
- twitter的ID生成器的snowFlake算法的自造版
snowFlake算法在生成ID时特别高效,可参考:https://segmentfault.com/a/1190000011282426 SnowFlake算法生成id的结果是一个64bit大小的整 ...
- 查询A、B表中,A表中B表没有的数据
A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低 ~执 ...
- geotools实现多边形的合并&缓冲区
这算是第一次接触开源工具包,说实话刚开始有点不知所措,中途遇到很多问题的时候也感觉头皮发麻,不过很高兴自己还是坚持下来了. geotools就不做过多的介绍了,想总结一下如何根据开源内容做自己的项目. ...