# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1012429 | DorostWef | Dreaming (IOI13_dreaming) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma once
#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
const int N = 100005;
vector <pair <int, int>> g[N];
int dis[2][N];
vector <int> v;
bool vis[N];
void dfs (int x, int in) {
v.push_back(x);
vis[x] = true;
for (auto [y, w] : g[x]) {
if (!vis[y]) {
dis[in][y] = dis[in][x] + w;
dfs (y, in);
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0; i < M; i++) {
g[A[i]].push_back(mk(B[i], T[i]));
g[B[i]].push_back(mk(A[i], T[i]));
}
vector <int> wef;
int mx = 0;
for (int i = 0; i < N; i++) {
if (!vis[i]) {
v.clear();
dis[0][i] = 0;
dfs(i, 0);
int x = i;
for (int w : v) {
if (dis[0][w] > dis[0][x])
x = w;
vis[w] = false;
}
v.clear();
dis[0][x] = 0;
dfs (x, 0);
int y = x;
for (int w : v) {
if (dis[0][w] > dis[0][y])
y = w;
vis[w] = false;
}
v.clear();
dis[1][y] = 0;
dfs (y, 1);
mx = max(mx, dis[1][x]);
int mn = mx;
for (int i : v) {
mn = min(mn, max (dis[0][i], dis[1][i]));
}
wef.push_back(mn);
}
}
sort (wef.begin(), wef.end());
reverse (wef.begin(), wef.end());
wef.push_back(0);
wef.push_back(0);
wef.push_back(0);
mx = max(mx, wef[0] + wef[1] + L);
mx = max(mx, wef[1] + wef[2] + L + L);
return mx;
}