#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll infl = 1e18;
template<typename T>
int len(T &a){
return a.size();
}
int travelTime(int n, int m, int L, int a[], int b[], int t[]) {
vector<vector<pair<int,int>>> g(n);
for(int i = 0; i < m; i ++){
g[a[i]].emplace_back(b[i], t[i]);
g[b[i]].emplace_back(a[i], t[i]);
}
vector<ll> disa(n);
vector<bool> vis(n);
int mx = 0;
auto dfs = [&](auto &dfs, int i, int p = -1)->void{
vis[i] = 1;
for(auto [u, w] : g[i]){
if(u == p) continue;
disa[u] = disa[i] + w;
dfs(dfs, u, i);
}
if(disa[mx] < disa[i]) mx = i;
};
ll mxx = infl;
auto dfs2 = [&](auto &dfs2, int i, int p = -1, ll d = 0)->void{
vis[i] = 1;
mxx = min(mxx, max(d, disa[i]));
for(auto [u, w] : g[i]){
if(u == p) continue;
dfs2(dfs2, u, i, d + w);
}
};
ll ans = 0;
vector<ll> dis;
for(int i = 0; i < n; i ++){
if(vis[i]) continue;
mx = i;
mxx = infl;
dfs(dfs, i);
disa[mx] = 0;
dfs(dfs, mx);
ans = max(ans, disa[mx]);
dfs2(dfs2, mx);
dis.push_back(mxx);
}
if(len(dis) == 1){
return max(ans, dis[0]);
}
if(len(dis) == 2){
return max(ans, dis[0] + dis[2] + L);
}
sort(dis.begin(), dis.end());
n = dis.size();
ans = max({ans, dis[n - 1] + dis[n - 2] + L, dis[n - 2] + dis[n - 3] + 2 * L});
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
10836 KB |
Output is correct |
2 |
Correct |
33 ms |
10832 KB |
Output is correct |
3 |
Correct |
22 ms |
7512 KB |
Output is correct |
4 |
Correct |
5 ms |
1884 KB |
Output is correct |
5 |
Correct |
4 ms |
1368 KB |
Output is correct |
6 |
Correct |
9 ms |
2688 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
10836 KB |
Output is correct |
2 |
Correct |
33 ms |
10832 KB |
Output is correct |
3 |
Correct |
22 ms |
7512 KB |
Output is correct |
4 |
Correct |
5 ms |
1884 KB |
Output is correct |
5 |
Correct |
4 ms |
1368 KB |
Output is correct |
6 |
Correct |
9 ms |
2688 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
6740 KB |
Output is correct |
2 |
Correct |
16 ms |
6620 KB |
Output is correct |
3 |
Correct |
16 ms |
6756 KB |
Output is correct |
4 |
Correct |
15 ms |
6628 KB |
Output is correct |
5 |
Correct |
15 ms |
6624 KB |
Output is correct |
6 |
Correct |
19 ms |
7520 KB |
Output is correct |
7 |
Correct |
24 ms |
7392 KB |
Output is correct |
8 |
Correct |
15 ms |
6624 KB |
Output is correct |
9 |
Correct |
15 ms |
6612 KB |
Output is correct |
10 |
Correct |
16 ms |
6872 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
4564 KB |
Output is correct |
13 |
Correct |
4 ms |
4564 KB |
Output is correct |
14 |
Correct |
4 ms |
4564 KB |
Output is correct |
15 |
Correct |
4 ms |
4564 KB |
Output is correct |
16 |
Correct |
4 ms |
4564 KB |
Output is correct |
17 |
Correct |
4 ms |
4564 KB |
Output is correct |
18 |
Correct |
5 ms |
4820 KB |
Output is correct |
19 |
Correct |
4 ms |
4564 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
444 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
5 ms |
4564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
10836 KB |
Output is correct |
2 |
Correct |
33 ms |
10832 KB |
Output is correct |
3 |
Correct |
22 ms |
7512 KB |
Output is correct |
4 |
Correct |
5 ms |
1884 KB |
Output is correct |
5 |
Correct |
4 ms |
1368 KB |
Output is correct |
6 |
Correct |
9 ms |
2688 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |