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)的做法: 按边排序,枚举当前最大的边 ...
随机推荐
- 廖雪峰Java11多线程编程-4线程工具类-1ThreadLocal
多线程是Java实现多任务的基础: Thread ExecutorService ScheduledThreadPool Fork/Join Thread对象代表一个线程:调用Tread.curren ...
- 「题解」:[POJ2942]Knights of the Round Table
问题 E: Knights of the Round Table 时间限制: 1 Sec 内存限制: 256 MB 题面 题目描述 作为一名骑士是一个非常有吸引力的职业:寻找圣杯,拯救遇难的少女,与 ...
- Python-函数基础(1)
目录 函数定义 什么是函数? 定义函数三种形式 函数定义的特性 函数调用 函数返回值 return的特性: 函数的参数 有参函数 形参 位置形参 默认形参 实参 位置实参 关键字实参 可变长参数 形参 ...
- wiki方法能在H5页面上
1. wiki 方法能在h5网页上判断当前手机上是否安装了汽车之家app,有的话,打开软件,并且能跳到相应页面,没有安装的话,能跳到主软下载页面? Android有个 applink,但是不知道支持得 ...
- Windows Server 2008 R2 部署服务
Windows Server 2008 R2 部署服务 部分参考: Windows Server 2008 R2 部署服务 - 马睿的技术博客 - 51CTO技术博客http://marui.blog ...
- 菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t[转]
菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...
- Sql Server 中查询存储过程的修改时间
1.按最近修改排序所有存储过程 SELECT [name], [create_date], [modify_date] FROM [sys].[objects] WHERE [type] = 'P' ...
- Android之RelativeLayout相对布局
1.相关术语解释 1.基本属性 gravity :设置容器内组件的对齐方式 ignoreGravity : 设置该属性为true的组件,将不受gravity属性的影响 2.根据父容器定位 layout ...
- 数据库insert和update
1.当使用insert时不能使用where id=?,这是要使用update语句 2.只对一些列插入数据或者更新数据: insert是: insert tb(column1,column2..)val ...
- 02_springmvc处理器映射器和适配器(补充)
一.非注解的处理器映射器 HandlerMapping 负责根据request请求找到对应的Handler处理器及Interceptor拦截器,将它们封装在HandlerExecutionChain ...