This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef FEXT
#include "dreaming.h"
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = l; i < r; i++)
#define ForE(i, l, r) for (auto i = l; i <= r; i++)
#define FordE(i, l, r) for (auto i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pair <int, int>>;
using vvi = vector <vector <int>>;
const int N = 1e5 + 5;
int n, m, l;
vpii adj[N];
bool vis[N];
pii dfs_farthest(int u, int p){
vis[u] = 1;
pii ans = make_pair(0, u);
Fora(edge, adj[u]){
int v, w; tie(v, w) = edge;
if (v == p){
continue;
}
pii tans = dfs_farthest(v, u);
tans.fi += w;
ans = max(ans, tans);
}
return ans;
}
int h1[N], h2[N];
void dfs_h(int u, int p, int h[]){
Fora(edge, adj[u]){
int v, w; tie(v, w) = edge;
if (v == p){
continue;
}
h[v] = h[u] + w;
dfs_h(v, u, h);
}
}
void dfs_r(int u, int p, int &r){
r = min(r, max(h1[u], h2[u]));
Fora(edge, adj[u]){
int v, w; tie(v, w) = edge;
if (v == p){
continue;
}
dfs_r(v, u, r);
}
}
int travelTime(int _n, int _m, int _l, int _a[], int _b[], int _t[]){
n = _n; m = _m; l = _l;
For(i, 0, m){
int u = _a[i], v = _b[i], w = _t[i];
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
int ans = 0, ans1 = -1, ans2 = -1, ans3 = -2;
ForE(u, 1, n){
if (vis[u]){
continue;
}
int d1 = dfs_farthest(u, u).se;
int d, d2; tie(d, d2) = dfs_farthest(d1, d1);
ans = max(ans, d);
dfs_h(d1, d1, h1);
dfs_h(d2, d2, h2);
int r = INT_MAX;
dfs_r(u, u, r);
if (ans1 < r){
ans3 = ans2; ans2 = ans1; ans1 = r;
}
else if (ans2 < r){
ans3 = ans2; ans2 = r;
}
else if (ans3 < r){
ans3 = r;
}
}
if (ans1 == -1){
return ans;
}
if (ans2 == -1){
return ans;
}
if (ans3 == -1){
ans = max(ans, ans2 + l + ans1);
}
else{
ans = max(ans, ans2 + l + max(ans1, ans3 + l));
}
return ans;
}
#ifdef FEXT
#define fail(s, x...) do { \
fprintf(stderr, s "\n", ## x); \
exit(1); \
} while(0)
#define MAX_N 100000
static int A[MAX_N];
static int B[MAX_N];
static int T[MAX_N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen("KEK.inp", "r", stdin);
freopen("KEK.out", "w", stdout);
int N, M, L, i;
cin >> N >> M >> L;
for (i = 0; i < M; i++)
cin >> A[i] >> B[i] >> T[i];
int answer = travelTime(N, M, L, A, B, T);
cout << answer << endl;
return 0;
}
#endif
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# | 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... |