#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define num long long
#define next first
#define cost second
#define t_road pair<int, num>
#define t_roads vector<t_road>
#define graph vector<t_roads>
#define ndata vector<num>
graph roads;
// here, max is refered as the first maximal
// mix is refered as the second maximal
ndata max_dist;
ndata visit_id; num stage = 0;
num computeDistance (int node, int parent, num cst) {
max_dist[node] = cst;
visit_id[node] = stage;
for (t_road road : roads[node]) {
if (road.next == parent) continue ;
max_dist[node] = max(
max_dist[node],
computeDistance(road.next, node, cst + road.cost)
);
}
max_dist[node] -= cst;
return max_dist[node] + cst;
}
num excentricity (int node, int parent, num incoming) {
num local = max(incoming, max_dist[node]);
visit_id[node] = stage;
num max = 0;
num mix = 0;
for (t_road road : roads[node]) {
if (road.next == parent) continue ;
num next_d = road.cost + max_dist[road.next];
if (next_d > max) {
mix = max;
max = next_d;
} else if (next_d > mix) mix = next_d;
}
for (t_road road : roads[node]) {
if (road.next == parent) continue ;
num next_i = max == max_dist[road.next] + road.cost ? mix : max;
if (next_i < incoming) next_i = incoming;
local = min(local, excentricity(road.next, node, road.cost + next_i));
}
return local;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
roads.resize(N);
visit_id.resize(N, -1);
max_dist.resize(N);
for (int i = 0; i < M; i ++) {
roads[A[i]].push_back({ B[i], T[i] });
roads[B[i]].push_back({ A[i], T[i] });
}
for (int i = 0; i < N; i ++)
if (visit_id[i] != stage)
computeDistance(i, -1, 0);
stage ++;
vector<num> values;
for (int i = 0; i < N; i ++)
if (visit_id[i] != stage)
values.push_back(excentricity(i, -1, 0));
sort(values.rbegin(), values.rend());
if (values.size() == 1) return values[0];
if (values.size() == 2) return L + values[0] + values[1];
return L + values[1] + max(values[0], values[2] + L);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
13168 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
13168 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
6860 KB |
Output is correct |
2 |
Correct |
30 ms |
7360 KB |
Output is correct |
3 |
Correct |
23 ms |
7300 KB |
Output is correct |
4 |
Correct |
25 ms |
7392 KB |
Output is correct |
5 |
Correct |
23 ms |
7224 KB |
Output is correct |
6 |
Correct |
25 ms |
8204 KB |
Output is correct |
7 |
Correct |
35 ms |
7544 KB |
Output is correct |
8 |
Correct |
29 ms |
7244 KB |
Output is correct |
9 |
Correct |
33 ms |
7104 KB |
Output is correct |
10 |
Correct |
29 ms |
7544 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
5 ms |
5324 KB |
Output is correct |
13 |
Correct |
9 ms |
5324 KB |
Output is correct |
14 |
Correct |
7 ms |
5452 KB |
Output is correct |
15 |
Correct |
7 ms |
5420 KB |
Output is correct |
16 |
Correct |
5 ms |
5324 KB |
Output is correct |
17 |
Correct |
6 ms |
5324 KB |
Output is correct |
18 |
Correct |
8 ms |
5452 KB |
Output is correct |
19 |
Correct |
7 ms |
5324 KB |
Output is correct |
20 |
Correct |
1 ms |
308 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
9 ms |
5416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
72 ms |
13168 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |