题目链接

After completing her final semester, Savita is back home. She is excited to meet all her friends. Her N friends live in different houses spread across the city.

There are M roads connecting the houses. The road network formed is connected and does not contain self loops and multiple roads between same pair of houses. Savita and Friends decide to meet.

Savita wants to choose a point(not necessarily an integer) P on the road numbered K, such that, the maximum of dist(i) for all 1≤i≤N is minimised, 
where dist(i) is the shortest distance between the i'th friend and P.

If K'th road connects friend A and friend B you should print distance of chosen point from A. Also, print the max(dist(i)) for all 1≤i≤N. If there is more than one solution, print the one in which the point P is closest to A.

Note:

  • Use scanf/printf instead of cin/cout. Large input files.
  • Order of A and B as given in the input must be maintained. If P is at a distance of 8 from A and 2 from B, you should print 8 and not 2.

Input Format
First line contain T, the number of testcases. 
T testcases follow. 
First Line of each testcase contains 3 space separated integers N,M,K . 
Next M lines contain description of the ith road : three space separated integers A,B,C, where C is the length of road connecting A and B.

Output Format
For each testcase, print two space separated values in one line. The first value is the distance of P from the point A and the second value is the maximum of all the possible shortest paths between P and all of Savita's and her friends' houses. Round both answers to 5 decimal digits and print exactly 5 digits after the decimal point.

Constraints
1≤T≤10 
2≤N,M≤105 
N−1≤M≤N∗(N−1)/2 
1≤A,B≤N 
1≤C≤109 
1≤K≤M

Sample Input

2
2 1 1
1 2 10
4 4 1
1 2 10
2 3 10
3 4 1
4 1 5

Sample Output

5.00000 5.00000
2.00000 8.00000

Explanation

First testcase: 
As K = 1, they will meet at the point P on the road that connects friend 1 with friend 2. If we choose mid point then distance for both of them will be 5. In any other position the maximum of distance will be more than 5.

Second testcase: 
As K = 1, they will meet at a point P on the road connecting friend 1 and friend 2. If we choose point at a distance of 2 from friend 1: Friend

1 will have to travel distance 2. 
Friend 2 will have to travel distance 8. 
Friend 3 will have to travel distance 8. 
Friend 4 will have to travel distance 7. 
So, the maximum will be 8. 
In any other position of point choosen, the maximum distance will be more than 8.

Timelimits 

Timelimits for this problem is 2 times the environment limit.

 
 
 
 
 
 
 
 #include <queue>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define X first
#define Y second
typedef long long LL;
typedef pair<LL , LL> pii;
const LL INF = 1e18;
const int MAX_N = ;
vector<pii> G[MAX_N];
LL d1[MAX_N], d2[MAX_N];
bool done[MAX_N];
int n, m; void dijkstra(int s, LL *d) {
memset(done, false, sizeof(done));
priority_queue<pii, vector<pii>, greater<pii> > Q;
for (int i = ; i <= n; i++) d[i] = INF;
Q.push(pii(, s));
d[s] = ; while (!Q.empty()) {
int u = Q.top().Y; Q.pop();
done[u] = true; for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].X, w = G[u][i].Y;
if (d[v] > d[u] + w) {
d[v] = d[u] + w;
Q.push(pii(d[v], v));
}
}
}
} int main(void) {
//ios::sync_with_stdio(false);
int T;
scanf("%d", &T);
//cin >> T;
while (T--) {
int k, kth, s1, s2;
//cin >> n >> m >> k;
scanf("%d %d %d", &n, &m, &k);
for (int i = ; i <= n; i++) G[i].clear();
for (int i = ; i <= m; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
//cin >> a >> b >> c;
G[a].push_back(pii(b, c));
G[b].push_back(pii(a, c));
if (i == k) s1 = a, s2 = b, kth = c;
}
dijkstra(s1, d1);
dijkstra(s2, d2);
//for (int i = 1; i <= n; i++) cerr << d1[i] << endl; vector<pii> A;
for (int i = ; i <= n; i++) A.push_back(pii(d1[i], d2[i]));
sort(A.begin(), A.end());
vector<pii> B;
LL fst = -, snd = -;
for (int i = n - ; i >= ; i--) {
if (A[i].X <= fst && A[i].Y <= snd) continue;
fst = A[i].X, snd = A[i].Y;
B.push_back(A[i]);
}
double ans, p;
int kk = B.size();
if (B[].X < B[kk - ].Y) ans = B[].X, p = 0.0;
else ans = B[kk - ].Y, p = kth + 0.0;
for (int i = ; i < kk - ; i++) {
double tmp = (B[i].Y - B[i + ].X + kth) * 0.5;
double val = B[i + ].X + tmp;
if (ans > val) ans = val, p = tmp;
else if (ans == val && p > tmp) p = tmp;
}
printf("%.5f %.5f\n", p, ans);
}
return ;
}

Hackerrank--Savita And Friends(最小直径生成树MDST)的更多相关文章

  1. 【学习笔记】最小直径生成树(MDST)

    简介 无向图中某一点(可以在顶点上或边上),这个点到所有点的最短距离的最大值最小,那么这个点就是 图的绝对中心. 无向图所有生成树中,直径最小的一个,被称为 最小直径生成树. 图的绝对中心的求法 下文 ...

  2. bzoj2180: 最小直径生成树

    Description 输入一个无向图G=(V,E),W(a,b)表示边(a,b)之间的长度,求一棵生成树T,使得T的直径最小.树的直径即树的最长链,即树上距离最远的两点之间路径长度. Input 输 ...

  3. bzoj2395[Balkan 2011]Timeismoney最小乘积生成树

    所谓最小乘积生成树,即对于一个无向连通图的每一条边均有两个权值xi,yi,在图中找一颗生成树,使得Σxi*Σyi取最小值. 直接处理问题较为棘手,但每条边的权值可以描述为一个二元组(xi,yi),这也 ...

  4. HDU5697 刷题计划 dp+最小乘积生成树

    分析:就是不断递归寻找靠近边界的最优解 学习博客(必须先看这个): 1:http://www.cnblogs.com/autsky-jadek/p/3959446.html 2:http://blog ...

  5. 【UVA 11354】 Bond (最小瓶颈生成树、树上倍增)

    [题意] n个点m条边的图 q次询问 找到一条从s到t的一条边 使所有边的最大危险系数最小 InputThere will be at most 5 cases in the input file.T ...

  6. 算法提高 最小方差生成树(Kruskal)_模板

     算法提高 最小方差生成树   时间限制:1.0s   内存限制:256.0MB        问题描述 给定带权无向图,求出一颗方差最小的生成树. 输入格式 输入多组测试数据.第一行为N,M,依次是 ...

  7. 【BZOJ2395】【Balkan 2011】Timeismoney 最小乘积生成树

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  8. Bzoj2395: [Balkan 2011]Timeismoney(最小乘积生成树)

    问题描述 每条边两个权值 \(x,y\),求一棵 \((\sum x) \times (\sum y)\) 最小的生成树 Sol 把每一棵生成树的权值 \(\sum x\) 和 \(\sum y\) ...

  9. 【poj3522-苗条树】最大边与最小边差值最小的生成树,并查集

    题意:求最大边与最小边差值最小的生成树.n<=100,m<=n*(n-1)/2,没有重边和自环. 题解: m^2的做法就不说了. 时间复杂度O(n*m)的做法: 按边排序,枚举当前最大的边 ...

随机推荐

  1. JavaScript特效源码(4、鼠标特效)

    1.鼠标感应--渐现特效 鼠标感应渐显图片[平时很模糊的图片鼠标一放上就显示清楚] [修改图片名称即可][共2步] ====1.将以下代码加入HTML的<head></head> ...

  2. grep 强大的文本搜索工具

    1.grep -r "History folder does't exist:" * :中间是要搜索的文本,* 表示全部显示出来

  3. python 请求测试环境的https接口地址报SSL错误验证,访问不了

    解决文案: response = requests.post(url, data=payload, json=None, headers=headers,verify=False)print(resp ...

  4. mysql on windows的安装

    1.去官网下载合适的压缩包 网址:https://dev.mysql.com/downloads/file/?id=476233 (拉到最下面点击 No thanks,just start my do ...

  5. Ubuntu Service说明与使用方法

    1 什么是Ubuntu的Service 网上很多资料说, service就是linux中随开机自启动的, 并且在后台运行的程序. 个人认为, 至少对于Ubuntu来说, 这个说法是不太准确的, 这只不 ...

  6. 廖雪峰Java11多线程编程-3高级concurrent包-1ReentrantLock

    线程同步: 是因为多线程读写竞争资源需要同步 Java语言提供了synchronized/wait/notify来实现同步 编写多线程同步很困难 所以Java提供了java.util.concurre ...

  7. 线性dp——cf1032

    升维来保存第i位按j是否可行,然后枚举i-1个的状态,用5*5n就可以完成递推 /* dp[i][j]==0表示第i步按j不可行 */ #include<bits/stdc++.h> us ...

  8. MyEclipse使用总结——将原有的MyEclipse中的项目转成maven项目[转]

    前面一篇文章中我们了解了 在myeclipse中新建Maven框架的web项目 那么如果我们原来有一些项目现在想转成maven项目应该怎么做呢 我收集到了三种思路: 一.新建一个maven项目,把原项 ...

  9. win7+64位笔记本安装TensorFlow CPU版

    最近要用到Keras框架,而Keras是基于Theano或Tensorflow框架安装的,所以首先要准备底层框架的搭建. 在网上看了一大堆教程头昏脑涨,随便挑了个试一试,竟然捣鼓成功了,记录一下安装过 ...

  10. Spring MVC(五)--控制器通过注解@RequestParam接受参数

    上一篇中提到,当前后端命名规则不一致时,需要通过注解@RequestParam接受参数,这个注解是作用在参数上.下面通过实例说明,场景如下: 在页面输入两个参数,控制器通过注解接受,并将接受到的数据渲 ...