#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>
#include "dreaming.h"
using namespace std;
const int inf = 7 + 1e9;
vector <int> vis, f, g;
vector <vector <II>> adj;
void dfs(int u) {
vis[u] = 1;
for (auto z : adj[u]) {
int v = z.st, w = z.nd;
if (vis[v])
continue;
dfs(v);
f[u] = max(f[u], f[v] + w);
}
}
int dfs_(int u, int p) {
int ans = max(f[u], g[u]);
vector <II> V;
for (auto z : adj[u])
if (z.st != p)
V.push_back(z);
vector <int> l(V.size(), 0), r(V.size(), 0);
for (int i = 1; i < V.size(); i ++)
l[i] = max(l[i - 1], f[V[i - 1].st] + V[i - 1].nd);
for (int i = (int)V.size() - 2; i >= 0; i --)
r[i] = max(r[i + 1], f[V[i + 1].st] + V[i + 1].nd);
for (int i = 0; i < V.size(); i ++) {
int v = V[i].st, w = V[i].nd;
g[v] = max({l[i], r[i], g[u]}) + w;
ans = min(ans, dfs_(v, u));
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
adj.resize(N);
for (int i = 0; i < M; i ++) {
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
vis.assign(N, 0);
f.assign(N, 0);
g.assign(N, 0);
vector <int> c;
for (int i = 0; i < N; i ++)
if (!vis[i]) {
dfs(i);
int val = dfs_(i, 0);
c.push_back(val);
}
if (c.size() == 1)
return c[0];
if (c.size() == 2)
return c[0] + c[1] + L;
vector <vector <int>> l(c.size(), vector <int> (2, 0)), r(c.size(), vector <int> (2, 0));
for (int i = 1; i < c.size(); i ++) {
l[i][0] = l[i - 1][0];
l[i][1] = l[i - 1][1];
if (c[i - 1] > l[i][0]) {
l[i][1] = l[i][0];
l[i][0] = c[i - 1];
}
else if (c[i - 1] > l[i][1])
l[i][1] = c[i - 1];
}
for (int i = (int)c.size() - 2; i >= 0; i --) {
r[i][0] = r[i + 1][0];
r[i][1] = r[i + 1][1];
if (c[i + 1] > r[i][0]) {
r[i][1] = r[i][0];
r[i][0] = c[i + 1];
}
else if (c[i + 1] > r[i][1])
r[i][1] = c[i + 1];
}
int ans = inf;
for (int i = 0; i < c.size(); i ++) {
int ma1 = l[i][0], ma2 = l[i][1];
for (int j = 0; j < 2; j ++)
if (r[i][j] > ma1) {
ma2 = ma1;
ma1 = r[i][j];
}
else if (r[i][j] > ma2)
ma2 = r[i][j];
ans = min(ans, max(ma1 + c[i] + L, ma1 + ma2 + 2 * L));
}
return ans;
}
Compilation message
dreaming.cpp: In function 'int dfs_(int, int)':
dreaming.cpp:35:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i = 1; i < V.size(); i ++)
| ~~^~~~~~~~~~
dreaming.cpp:39:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 0; i < V.size(); i ++) {
| ~~^~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:68:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i = 1; i < c.size(); i ++) {
| ~~^~~~~~~~~~
dreaming.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i = 0; i < c.size(); i ++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
45 ms |
22868 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 |
45 ms |
22868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
13532 KB |
Output is correct |
2 |
Correct |
25 ms |
13532 KB |
Output is correct |
3 |
Correct |
22 ms |
13568 KB |
Output is correct |
4 |
Correct |
21 ms |
13504 KB |
Output is correct |
5 |
Correct |
21 ms |
13332 KB |
Output is correct |
6 |
Correct |
22 ms |
14376 KB |
Output is correct |
7 |
Correct |
23 ms |
14048 KB |
Output is correct |
8 |
Correct |
21 ms |
13280 KB |
Output is correct |
9 |
Correct |
20 ms |
13272 KB |
Output is correct |
10 |
Correct |
23 ms |
14032 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
16 ms |
15304 KB |
Output is correct |
13 |
Correct |
15 ms |
15168 KB |
Output is correct |
14 |
Correct |
13 ms |
15312 KB |
Output is correct |
15 |
Correct |
14 ms |
15316 KB |
Output is correct |
16 |
Correct |
13 ms |
15312 KB |
Output is correct |
17 |
Correct |
13 ms |
15568 KB |
Output is correct |
18 |
Correct |
13 ms |
15324 KB |
Output is correct |
19 |
Correct |
13 ms |
15144 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
15 ms |
15316 KB |
Output is correct |
# |
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 |
45 ms |
22868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |