#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define ll int
#define endl '\n'
const int N = 1e5 + 5;
ll dp[N], res, vis[N], lg, cur, d[N];
vector<pair<ll, ll>> node[N];
vector<ll> mem;
void FindFirstNode(ll u, ll p, ll dist){
mem.push_back(u);
if (dist > lg){
lg = dist;
cur = u;
}
for (auto j : node[u]){
if (j.first == p) continue;
FindFirstNode(j.first, u, dist + j.second);
}
}
void FindDia(ll u, ll p, ll dist){
d[u] = dist;
if (dist > lg){
lg = dist;
cur = u;
}
for (auto j : node[u]){
if (j.first == p) continue;
FindDia(j.first, u, dist + j.second);
}
}
void DiaRev(ll u, ll p, ll dist){
dp[u] = dist;
for (auto j : node[u]){
if (j.first == p) continue;
DiaRev(j.first, u, dist + j.second);
}
}
int travelTime(int N, int M, int L, int A[],int B[],int T[]){
for (int i = 1; i <= M; i++){
node[A[i - 1] + 1].push_back({B[i - 1] + 1, T[i - 1]});
node[B[i - 1] + 1].push_back({A[i - 1] + 1, T[i - 1]});
}
vector<ll> pq;
ll ans = 0;
for (int i = 1; i <= N; i++){
if (!vis[i]){
lg = -1;
mem.clear();
FindFirstNode(i, i, 0);
lg = -1;
FindDia(cur, cur, 0);
DiaRev(cur, cur, 0);
ll mn = 1e9;
for (auto j : mem){
mn = min(mn, max(d[j], dp[j]));
}
ans = max(ans, lg);
pq.push_back(mn);
}
}
sort(pq.begin(), pq.end(), greater<ll>());
if (pq.size() == 1) return ans;
if (pq.size() == 2) return max(ans, pq[0] + pq[1] + L);
return max(max(pq[1] + pq[2] + 2 * L, pq[0] + pq[1] + L), ans);
}
/*
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("VietCT.INP", "r")){
freopen("VietCT.INP", "r", stdin);
freopen("VietCT.OUT", "w", stdout);
}
int n, m, l;
cin >> n >> m >> l;
vector<int> a, b, t;
for (int i = 1; i <= m; i++){
int u, v, c;
cin >> u >> v >> c;
a.push_back(u + 1);
b.push_back(v + 1);
t.push_back(c);
}
cout << travelTime(n, m, l, a, b, t);
}
/*
6 2 1000
0 1 10
2 3 10
*/
Compilation message
dreaming.cpp:89:1: warning: "/*" within comment [-Wcomment]
89 | /*
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1099 ms |
13436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1099 ms |
13436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1099 ms |
13436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
6388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1099 ms |
13436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1099 ms |
13436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |