标签:des style blog http color os io strong for
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.
1 #include <queue>
2 #include <cstdio>
3 #include <iomanip>
4 #include <vector>
5 #include <cstring>
6 #include <iostream>
7 #include <algorithm>
8 using namespace std;
9
10 #define X first
11 #define Y second
12 typedef long long LL;
13 typedef pair<LL , LL> pii;
14 const LL INF = 1e18;
15 const int MAX_N = 100050;
16 vector<pii> G[MAX_N];
17 LL d1[MAX_N], d2[MAX_N];
18 bool done[MAX_N];
19 int n, m;
20
21 void dijkstra(int s, LL *d) {
22 memset(done, false, sizeof(done));
23 priority_queue<pii, vector<pii>, greater<pii> > Q;
24 for (int i = 1; i <= n; i++) d[i] = INF;
25 Q.push(pii(0, s));
26 d[s] = 0;
27
28 while (!Q.empty()) {
29 int u = Q.top().Y; Q.pop();
30 done[u] = true;
31
32 for (int i = 0; i < G[u].size(); i++) {
33 int v = G[u][i].X, w = G[u][i].Y;
34 if (d[v] > d[u] + w) {
35 d[v] = d[u] + w;
36 Q.push(pii(d[v], v));
37 }
38 }
39 }
40 }
41
42 int main(void) {
43 //ios::sync_with_stdio(false);
44 int T;
45 scanf("%d", &T);
46 //cin >> T;
47 while (T--) {
48 int k, kth, s1, s2;
49 //cin >> n >> m >> k;
50 scanf("%d %d %d", &n, &m, &k);
51 for (int i = 1; i <= n; i++) G[i].clear();
52 for (int i = 1; i <= m; i++) {
53 int a, b, c;
54 scanf("%d %d %d", &a, &b, &c);
55 //cin >> a >> b >> c;
56 G[a].push_back(pii(b, c));
57 G[b].push_back(pii(a, c));
58 if (i == k) s1 = a, s2 = b, kth = c;
59 }
60 dijkstra(s1, d1);
61 dijkstra(s2, d2);
62 //for (int i = 1; i <= n; i++) cerr << d1[i] << endl;
63
64 vector<pii> A;
65 for (int i = 1; i <= n; i++) A.push_back(pii(d1[i], d2[i]));
66 sort(A.begin(), A.end());
67 vector<pii> B;
68 LL fst = -1, snd = -1;
69 for (int i = n - 1; i >= 0; i--) {
70 if (A[i].X <= fst && A[i].Y <= snd) continue;
71 fst = A[i].X, snd = A[i].Y;
72 B.push_back(A[i]);
73 }
74 double ans, p;
75 int kk = B.size();
76 if (B[0].X < B[kk - 1].Y) ans = B[0].X, p = 0.0;
77 else ans = B[kk - 1].Y, p = kth + 0.0;
78 for (int i = 0; i < kk - 1; i++) {
79 double tmp = (B[i].Y - B[i + 1].X + kth) * 0.5;
80 double val = B[i + 1].X + tmp;
81 if (ans > val) ans = val, p = tmp;
82 else if (ans == val && p > tmp) p = tmp;
83 }
84 printf("%.5f %.5f\n", p, ans);
85 }
86 return 0;
87 }
Hackerrank--Savita And Friends(最小直径生成树MDST)
标签:des style blog http color os io strong for
原文地址:http://www.cnblogs.com/Stomach-ache/p/3932537.html