#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#include "dreaming.h"
#endif
const int N = 100'000 + 10;
vector<pair<int, int>> ad[N];
bool mk[N];
int dpin[N], dpout[N];
void dfs1(int u, int p = 0) {
mk[u] = true;
for (const auto& [v, w] : ad[u]) {
if (v == p) continue;
dfs1(v, u);
dpin[u] = max(dpin[u], dpin[v] + w);
}
}
int dfs2(int u, int p = 0) {
array<pair<int, int>, 2> best;
best[0] = best[1] = {-1e9, -1};
auto add = [&](pair<int, int> x) {
if (best[1] < x) swap(best[1], x);
if (best[0] < best[1]) swap(best[0], best[1]);
};
add({dpout[u], u});
for (const auto& [v, w] : ad[u]) if (v != p) add({dpin[v] + w, v});
int ret = u;
for (const auto& [v, w] : ad[u]) {
if (v == p) continue;
dpout[v] = (best[0].second == v ? best[1].first : best[0].first) + w;
int nv = dfs2(v, u);
if (max(dpin[nv], dpout[nv]) < max(dpin[ret], dpout[ret])) ret = nv;
}
return ret;
}
int travelTime(int n, int m, int l, int A[],int B[],int T[]) {
for (int i = 1; i <= m; ++i) {
int u = A[i], v = B[i], w = T[i];
ad[u].push_back({v, w});
ad[v].push_back({u, w});
}
vector<int> ed;
for (int i = 1; i <= n; ++i) {
if (mk[i]) continue;
dfs1(i);
int x = dfs2(i);
ed.push_back(max(dpin[x], dpout[x]));
}
sort(ed.begin(), ed.end(), greater<>());
if (ed.size() == 1) return ed[0];
if (ed.size() == 2) return ed[0] + ed[1] + l;
if (ed.size() >= 3) return max(ed[0] + ed[1] + l, ed[1] + ed[2] + 2 * l);
}
#ifdef LOCAL
int n, m, l;
int A[N], B[N], T[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> l;
for (int i = 1; i <= m; ++i) cin >> A[i] >> B[i] >> T[i];
cout << travelTime(n, m, l, A, B, T) << "\n";
}
#endif
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:51:15: warning: control reaches end of non-void function [-Wreturn-type]
51 | vector<int> ed;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
14112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
14112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
7136 KB |
Output is correct |
2 |
Correct |
13 ms |
7116 KB |
Output is correct |
3 |
Correct |
14 ms |
7072 KB |
Output is correct |
4 |
Correct |
13 ms |
7128 KB |
Output is correct |
5 |
Correct |
15 ms |
7128 KB |
Output is correct |
6 |
Correct |
14 ms |
7648 KB |
Output is correct |
7 |
Correct |
13 ms |
7132 KB |
Output is correct |
8 |
Correct |
12 ms |
7004 KB |
Output is correct |
9 |
Correct |
14 ms |
7040 KB |
Output is correct |
10 |
Correct |
13 ms |
7384 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
3 ms |
5336 KB |
Output is correct |
13 |
Correct |
3 ms |
5336 KB |
Output is correct |
14 |
Correct |
3 ms |
5336 KB |
Output is correct |
15 |
Correct |
5 ms |
5336 KB |
Output is correct |
16 |
Correct |
4 ms |
5336 KB |
Output is correct |
17 |
Correct |
3 ms |
5080 KB |
Output is correct |
18 |
Correct |
3 ms |
5336 KB |
Output is correct |
19 |
Correct |
3 ms |
5336 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
4 ms |
5336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
14112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |