#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) {
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();
dis2[nd2] = 0;
dfs1(nd2, -1, dis2);
int mnVal = 1e9;
for (auto &x:nds) {
mnVal = min(mnVal, max(dis1[x], dis2[x]));
}
nds.clear();
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];
if (vals.size() == 2) return vals[0] + vals[1] + L;
return max(vals[0] + vals[1] + L, vals[0] + vals[2] + 2*L);
}
Compilation message
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 |
1 |
Incorrect |
30 ms |
13268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
13268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
5976 KB |
Output is correct |
2 |
Incorrect |
18 ms |
6104 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
13268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |