This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define pii array<int,2>
vector<bool> vis;
vector<vector<pii>> g;
vector<int> nds;
vector<int> dis1, dis2;
void dfs1(int node, int parent, vector<int> &dis) {
vis[node] = true;
nds.push_back(node);
for (auto [w, t] : g[node]) {
if (w == parent) continue;
dis[w] = dis[node] + t;
dfs1(w, node, dis);
}
}
int cmp(int start) {
nds.clear();
dis1[start] = 0;
dfs1(start, -1, dis1);
// find node with maximum distance
int nd1 = nds[0];
for (auto &x:nds) {
if (dis1[x] > dis1[nd1]) nd1 = x;
}
dis1[nd1] = 0;
nds.clear();
dfs1(nd1, -1, dis1);
int nd2 = nds[0];
for (auto &x:nds) {
if (dis1[x] > dis1[nd2]) nd2 = x;
}
nds.clear();
dfs1(nd2, -1, dis2);
int mnVal = 1e9;
for (auto &x:nds) {
mnVal = min(mnVal, max(dis1[x], dis2[x]));
}
return mnVal;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vis.assign(N, false);
dis1.assign(N, 0);
dis2.assign(N, 0);
g.resize(N);
for (int i=0;i<M;i++) {
g[A[i]].push_back({B[i], T[i]});
g[B[i]].push_back({A[i], T[i]});
}
vector<int> vals;
for (int i=0;i<N;i++) {
if (vis[i]) continue;
vals.push_back(cmp(i));
}
sort(vals.begin(), vals.end(), greater<int>());
if (vals.size() == 1) return vals[0];
return vals[0] + vals[1] + L;
}
Compilation message (stderr)
dreaming.cpp: In function 'void dfs1(int, int, std::vector<int>&)':
dreaming.cpp:17:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
17 | for (auto [w, t] : g[node]) {
| ^
# | 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... |