D. BerDonalds
time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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();
}

最新文章

  1. matlab 采样函数
  2. UNION和UNION ALL
  3. 数论+spfa算法 bzoj 2118 墨墨的等式
  4. 转! java 中“==” 与“ .equals ”比较
  5. Apache CXF多个远程拒绝服务漏洞(CVE-2013-2160)
  6. Cortex-M3 动态加载一(地址无关代码实现)
  7. IP数据包结构
  8. springmvc国际化 基于浏览器语言的国际化配置
  9. 原生javascript-图片按钮切换
  10. AMD与commonJS
  11. Lucene学习笔记1(V7.1)
  12. android studio中使用lambda
  13. 帝国cms中下拉框select的绑定
  14. 003_webpack 配合babel 将es6转成es5
  15. [js]js设计模式-工厂模式
  16. MT【218】交点个数
  17. VirtualBox安装增强功能报错
  18. vue2+webpack 开发环境配置
  19. Pandas数据存取
  20. hibernate ID

热门文章

  1. [OpenCV学习笔记3][图像的加载+修改+显示+保存]
  2. 改进ban冒泡排序
  3. HDU 1979 Red and Black
  4. v-for并判断当前元素是否选中:$set实现响应添加属性
  5. [基础常识]申请免费SSL证书 - 阿里云云盾证书 - Digicert+Symantec 免费型DV SSL
  6. dedecms下的tplcache模板缓存文件过多怎么清理?
  7. Tp框架查询分页显示与全部查询出来显示运行时间快慢有区别吗?
  8. 【开发技术】storyboard和nib的差别
  9. CSS3总结学习(一):CSS3用户界面
  10. Centos7-两台Centos机器间复制文件