#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 |
1 |
Correct |
51 ms |
14412 KB |
Output is correct |
2 |
Correct |
49 ms |
14248 KB |
Output is correct |
3 |
Correct |
32 ms |
10444 KB |
Output is correct |
4 |
Correct |
7 ms |
4360 KB |
Output is correct |
5 |
Correct |
7 ms |
3552 KB |
Output is correct |
6 |
Correct |
17 ms |
5236 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
14412 KB |
Output is correct |
2 |
Correct |
49 ms |
14248 KB |
Output is correct |
3 |
Correct |
32 ms |
10444 KB |
Output is correct |
4 |
Correct |
7 ms |
4360 KB |
Output is correct |
5 |
Correct |
7 ms |
3552 KB |
Output is correct |
6 |
Correct |
17 ms |
5236 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
6128 KB |
Output is correct |
2 |
Correct |
25 ms |
6236 KB |
Output is correct |
3 |
Correct |
20 ms |
6168 KB |
Output is correct |
4 |
Correct |
27 ms |
6224 KB |
Output is correct |
5 |
Correct |
18 ms |
6132 KB |
Output is correct |
6 |
Correct |
22 ms |
6352 KB |
Output is correct |
7 |
Correct |
19 ms |
6380 KB |
Output is correct |
8 |
Correct |
18 ms |
6124 KB |
Output is correct |
9 |
Correct |
16 ms |
6100 KB |
Output is correct |
10 |
Correct |
18 ms |
6364 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
4 ms |
3432 KB |
Output is correct |
13 |
Correct |
4 ms |
3440 KB |
Output is correct |
14 |
Correct |
4 ms |
3412 KB |
Output is correct |
15 |
Correct |
4 ms |
3412 KB |
Output is correct |
16 |
Correct |
4 ms |
3284 KB |
Output is correct |
17 |
Correct |
4 ms |
2900 KB |
Output is correct |
18 |
Correct |
5 ms |
3564 KB |
Output is correct |
19 |
Correct |
4 ms |
3284 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
4 ms |
3284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
14412 KB |
Output is correct |
2 |
Correct |
49 ms |
14248 KB |
Output is correct |
3 |
Correct |
32 ms |
10444 KB |
Output is correct |
4 |
Correct |
7 ms |
4360 KB |
Output is correct |
5 |
Correct |
7 ms |
3552 KB |
Output is correct |
6 |
Correct |
17 ms |
5236 KB |
Output is correct |
7 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |