#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int INF = 1e9;
int f[N], g[N];
bool visit[N];
vector<int> vec;
vector< pair<int, int> > G[N];
void dfs1(int u, int p) {
visit[u] = 1, vec.push_back(u);
for (auto i : G[u]) {
int v = i.first, w = i.second;
if (v == p) continue;
dfs1(v, u);
f[u] = max(f[u], f[v] + w);
}
}
void dfs2(int u, int p) {
int v0 = -INF, v1 = -INF;
for (auto i : G[u]) {
int v = i.first, w = i.second;
if (v == p) continue;
int tmp = f[v] + w;
if (v0 < tmp) v1 = v0, v0 = tmp;
else if (v1 < tmp) v1 = tmp;
}
for (auto i : G[u]) {
int v = i.first, w = i.second;
if (v == p) continue;
g[v] = g[u] + w;
if (v0 == f[v] + w) g[v] = max(g[v], v1 + w);
else g[v] = max(g[v], v0 + w);
dfs2(v, u);
}
}
int travelTime(int n, int m, int L, int A[], int B[], int T[]) {
for (int i = 0; i < m; ++i) {
G[A[i]].push_back({B[i], T[i]});
G[B[i]].push_back({A[i], T[i]});
}
vector<int> vec2;
for (int i = 0; i < n; ++i) {
if (visit[i]) continue;
vec.clear(), dfs1(i, i), dfs2(i, i);
int mn = INF;
for (auto j : vec) mn = min(mn, max(f[j], g[j]));
vec2.push_back(mn);
}
sort(vec2.begin(), vec2.end(), greater<int>());
if (vec2.size() == 1) {
return vec2[0];
}
if (vec2.size() == 2) {
return vec2[0] + vec2[1] + L;
}
return max(vec2[0] + vec2[1] + L, vec2[1] + vec2[2] + 2 * L);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
11892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
11892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
11892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
6268 KB |
Output is correct |
2 |
Correct |
26 ms |
6252 KB |
Output is correct |
3 |
Correct |
24 ms |
6272 KB |
Output is correct |
4 |
Correct |
29 ms |
6392 KB |
Output is correct |
5 |
Correct |
27 ms |
6272 KB |
Output is correct |
6 |
Correct |
31 ms |
6648 KB |
Output is correct |
7 |
Correct |
29 ms |
6400 KB |
Output is correct |
8 |
Correct |
27 ms |
6136 KB |
Output is correct |
9 |
Correct |
26 ms |
6140 KB |
Output is correct |
10 |
Correct |
40 ms |
6396 KB |
Output is correct |
11 |
Correct |
4 ms |
2688 KB |
Output is correct |
12 |
Correct |
7 ms |
4092 KB |
Output is correct |
13 |
Correct |
7 ms |
4092 KB |
Output is correct |
14 |
Correct |
8 ms |
4092 KB |
Output is correct |
15 |
Correct |
8 ms |
4092 KB |
Output is correct |
16 |
Correct |
7 ms |
3964 KB |
Output is correct |
17 |
Correct |
7 ms |
3708 KB |
Output is correct |
18 |
Correct |
7 ms |
4092 KB |
Output is correct |
19 |
Correct |
8 ms |
3964 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
4 ms |
2688 KB |
Output is correct |
22 |
Correct |
3 ms |
2688 KB |
Output is correct |
23 |
Correct |
7 ms |
3964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
11892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
11892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |