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)的做法: 按边排序,枚举当前最大的边 ...
随机推荐
- 基于VGGnet的人脸识别系统-ubuntu 系统下的Caffe环境搭建(CPU)
对于caffe的系统一般使用linux系统,当然也有windows版本的caffe,不过如果你一开始使用了windows下面的caffe,后面学习的过程中,会经常遇到各种错误,网上下载的一些源码.模型 ...
- 【BZOJ4805】欧拉函数求和
题面 Description 给出一个数字N,求\(\sum\limits_{i=1}^n\varphi(i)\)i,1<=i<=N Input 正整数N.N<=2*10^9 Out ...
- LoadRunner函数的介绍
LoadRunner函数的介绍 LoadRunner函数 一:通用函数 LoadRunner的通用函数以lr为前缀,可以在任何协议中使用.可以如下分类: 信息相关的函数: lr_error_messa ...
- C++模拟实现Objective-C协议和代理模式
Objective-C的协议和代理是运用最多的特性之一,可以说在苹果系列开发中无处不在.事实上很多人都不知道其背后的原理.事实上简单点说,这就是设计模式中的代理模式的经典运用.代理模式简单点说就是为其 ...
- Python-进程(2)
目录 进程互斥锁 队列 堆栈 IPC(进程间通信) 生产者与消费者模型 进程互斥锁 通过之前的学习,我们千方百计的实现了程序的异步,让多个任务可以同时在几个进程中并发处理 他们之间的运行没有顺序,一旦 ...
- System.Web.Mvc.ViewResultBase.cs
ylbtech-System.Web.Mvc.ViewResultBase.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Pub ...
- 转:链表相交有环 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯
源地址:http://blog.csdn.net/sj13051180/article/details/6754228 1.判断单链表是否有环,要求空间尽量少(2011年MTK) 如何找出环的连接点在 ...
- python随机生成图片
#-*-coding:utf-8-*- import tensorflow as tf import numpy as np import cv2 image = tf.random_uniform( ...
- Leetcode958. Check Completeness of a Binary Tree二叉树的完全验证性
给定一个二叉树,确定它是否是一个完全二叉树. 百度百科中对完全二叉树的定义如下: 若设二叉树的深度为 h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集 ...
- 廖雪峰Java10加密与安全-4加密算法-2口令加密算法
对称加密key是一个byte数组,如AES256算法的key是一个32字节的数组,普通的加密软件由用户输入加密口令.如果由用户输入口令,进行加密/解密,需要用到PBE算法. 1.PBE:Passwor ...