#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll rd[100000];
bool vis[100000];
struct W {
int t;
int w;
};
vector<vector<W>> al;
void calcrd(int v, int p) {
vis[v] = true;
ll mrd = 0;
for(auto a : al[v]) {
if(a.t == p) continue;
calcrd(a.t, v);
mrd = max(mrd, a.w + rd[a.t]);
}
rd[v] = mrd;
}
ll radius(int v, int p, ll tdist) {
int mrv = -1;
ll mr = tdist;
ll mr2 = tdist;
for(auto a : al[v]) {
if(a.t == p) continue;
if(rd[a.t] + a.w >= mr) {
mrv = a.t;
mr2 = mr;
mr = rd[a.t] + a.w;
} else if(rd[a.t] + a.w >= mr2) {
mr2 = rd[a.t] + a.w;
}
}
ll br = mr;
for(auto a : al[v]) {
if(a.t == p) continue;
if(a.t == mrv) {
br = min(br, radius(a.t, v, mr2 + a.w));
} else {
br = min(br, radius(a.t, v, mr + a.w));
}
}
return br;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
al.assign(N, {});
for(int i = 0; i < M; ++i) {
al[A[i]].push_back({B[i], T[i]});
al[B[i]].push_back({A[i], T[i]});
}
vector<ll> rads;
for(int i = 0; i < N; ++i) {
if(vis[i]) continue;
calcrd(i, -1);
rads.push_back(radius(i, -1, 0));
}
sort(begin(rads), end(rads), greater<ll>{});
if(rads.size() == 1) return rads[0];
if(rads.size() == 2) return rads[0] + L + rads[1];
return max(rads[0] + rads[1] + L, rads[1] + 2*L + rads[2]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
11112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
11112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
6244 KB |
Output is correct |
2 |
Correct |
17 ms |
6228 KB |
Output is correct |
3 |
Correct |
20 ms |
6236 KB |
Output is correct |
4 |
Correct |
18 ms |
6272 KB |
Output is correct |
5 |
Correct |
18 ms |
6268 KB |
Output is correct |
6 |
Correct |
20 ms |
7104 KB |
Output is correct |
7 |
Correct |
29 ms |
6428 KB |
Output is correct |
8 |
Correct |
25 ms |
6200 KB |
Output is correct |
9 |
Correct |
18 ms |
6116 KB |
Output is correct |
10 |
Correct |
27 ms |
6508 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
5 ms |
4684 KB |
Output is correct |
13 |
Correct |
5 ms |
4684 KB |
Output is correct |
14 |
Correct |
5 ms |
4556 KB |
Output is correct |
15 |
Correct |
5 ms |
4684 KB |
Output is correct |
16 |
Correct |
5 ms |
4556 KB |
Output is correct |
17 |
Correct |
5 ms |
4428 KB |
Output is correct |
18 |
Correct |
5 ms |
4684 KB |
Output is correct |
19 |
Correct |
6 ms |
4556 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
6 ms |
4684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
11112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |