Hackerrank--Savita And Friends(最小直径生成树MDST)
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≤MSample 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: Friend1 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)的更多相关文章
- 【学习笔记】最小直径生成树(MDST)
简介 无向图中某一点(可以在顶点上或边上),这个点到所有点的最短距离的最大值最小,那么这个点就是 图的绝对中心. 无向图所有生成树中,直径最小的一个,被称为 最小直径生成树. 图的绝对中心的求法 下文 ...
- bzoj2180: 最小直径生成树
Description 输入一个无向图G=(V,E),W(a,b)表示边(a,b)之间的长度,求一棵生成树T,使得T的直径最小.树的直径即树的最长链,即树上距离最远的两点之间路径长度. Input 输 ...
- bzoj2395[Balkan 2011]Timeismoney最小乘积生成树
所谓最小乘积生成树,即对于一个无向连通图的每一条边均有两个权值xi,yi,在图中找一颗生成树,使得Σxi*Σyi取最小值. 直接处理问题较为棘手,但每条边的权值可以描述为一个二元组(xi,yi),这也 ...
- HDU5697 刷题计划 dp+最小乘积生成树
分析:就是不断递归寻找靠近边界的最优解 学习博客(必须先看这个): 1:http://www.cnblogs.com/autsky-jadek/p/3959446.html 2:http://blog ...
- 【UVA 11354】 Bond (最小瓶颈生成树、树上倍增)
[题意] n个点m条边的图 q次询问 找到一条从s到t的一条边 使所有边的最大危险系数最小 InputThere will be at most 5 cases in the input file.T ...
- 算法提高 最小方差生成树(Kruskal)_模板
算法提高 最小方差生成树 时间限制:1.0s 内存限制:256.0MB 问题描述 给定带权无向图,求出一颗方差最小的生成树. 输入格式 输入多组测试数据.第一行为N,M,依次是 ...
- 【BZOJ2395】【Balkan 2011】Timeismoney 最小乘积生成树
链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...
- Bzoj2395: [Balkan 2011]Timeismoney(最小乘积生成树)
问题描述 每条边两个权值 \(x,y\),求一棵 \((\sum x) \times (\sum y)\) 最小的生成树 Sol 把每一棵生成树的权值 \(\sum x\) 和 \(\sum y\) ...
- 【poj3522-苗条树】最大边与最小边差值最小的生成树,并查集
题意:求最大边与最小边差值最小的生成树.n<=100,m<=n*(n-1)/2,没有重边和自环. 题解: m^2的做法就不说了. 时间复杂度O(n*m)的做法: 按边排序,枚举当前最大的边 ...
随机推荐
- JQuery和JavaScript常用方法的一些区别
jquery 就对javascript的一个扩展,封装,就是让javascript更好用,更简单,为了说明区别,下面与大家分享下JavaScript 与JQuery 常用方法比较 jquery 就 ...
- [USACO2005 nov] Grazing on the Run【区间Dp】
Online Judge:bzoj1742,bzoj1694 Label:区间Dp 题目描述 John养了一只叫Joseph的奶牛.一次她去放牛,来到一个非常长的一片地,上面有N块地方长了茂盛的草.我 ...
- Java实现RSA加密
末尾贴上代码↓↓↓↓↓↓↓↓↓↓↓↓ 1.原理 2.实现过程 3. 公式 4.举例 p=13, q=11 , (p,q互质) N=p*q=143 L=(p-1)*(q-1)=120 E=7 ...
- 安卓手机端微信网页浏览记录清理debugx5.qq.com
最近我们环境从复)星(云切换到阿里云.早上地铁路上就有小伙伴@,一阵搜索.找的如下的方法. 记录一下: 目前只支持安卓手机的微信内置浏览器清理. 由腾讯提供的网址http://debugx5.qq.c ...
- 深入浅出 Java Concurrency (26): 并发容器 part 11 Exchanger[转]
可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给 exchange 方法,与伙伴线程进行匹配,并且在返回时接收其伙伴的对象.Exchanger 可能被视为 Synchro ...
- LintCode刷题笔记-- A+B problem
标签: 位运算 描述 Write a function that add two numbers A and B. You should not use + or any arithmetic ope ...
- 杂项-公司:IBM
ylbtech-杂项-公司:IBM IBM (IT公司-国际商业机器公司) IBM(国际商业机器公司)或万国商业机器公司,简称IBM(International Business Machines C ...
- sshfs实践记录
sshfs是一款利器,可以将远程linux服务器的路径通过ssh协议挂载到本地指定路径.本地的文件一改动,就自动同步到远程服务器中. 本次的实验在centos 6.9中进行. 1.下载.安装所有的依赖 ...
- LINK : fatal error LNK1104: 无法打开文件“ucrtd.lib”
先说解决方案: 选中项目->右键->属性->常规 -->Windows SDK 改成当前系统的SDK版本,我这边是10.0.15063.0,重新生成即可 下载cefsh ...
- 如何 在 jQuery 中的 $.each 循环中使用 break 和 continue
jQuery中each类似于javascript的for循环 但不同于for循环的是在each里面不能使用break结束循环,也不能使用continue来结束本次循环,想要实现类似的功能就只能用ret ...