This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |