#include <dreaming.h>
#include <bits/stdc++.h>
using namespace std;
int travelTime(int n, int m, int cost, int A[], int B[], int T[]) {
int ans = 0, t = 0;
vector<int> seen(n), dpu(n), dpb(n), cn;
vector<vector<int>> pref(n), suf(n);
deque<pair<int, int>> cur;
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]);
}
function<void(int, int)> dfs1 = [&](int node, int last) {
cn.emplace_back(node);
seen[node] = t;
pref[node].emplace_back(0), suf[node].emplace_back(0);
for (auto x : G[node])
if (x.first != last) {
dfs1(x.first, node);
dpb[node] = max(dpb[node], dpb[x.first] + x.second);
pref[node].emplace_back(dpb[x.first] + x.second);
suf[node].emplace_back(dpb[x.first] + x.second);
}
pref[node].emplace_back(0), suf[node].emplace_back(0);
int s = pref[node].size() - 1;
for (int i = 1; i <= s; ++i)
pref[node][i] = max(pref[node][i], pref[node][i - 1]);
for (int i = s; i; --i)
suf[node][i] = max(suf[node][i], suf[node][i + 1]);
};
function<void(int, int)> dfs2 = [&](int node, int last) {
int ct = 1;
for (auto x : G[node])
if (x.first != last) {
dpu[x.first] = x.second + max(max(pref[node][ct - 1], suf[node][ct + 1]), dpu[node]);
dfs2(x.first, node);
++ct;
}
};
for (int i = 0; i < n; ++i)
if (!seen[i]) {
++t;
cn.clear();
dfs1(i, -1);
dfs2(i, -1);
pair<int, int> ans = {INT_MAX, 0};
for (auto x : cn)
ans = min(ans, {max(dpu[x],dpb[x]),x});
cur.emplace_back(ans);
}
sort(cur.begin(), cur.end());
reverse(cur.begin(), cur.end());
pair<int, int> node = cur.front();
cur.pop_front();
for (auto [c, n] : cur) {
G[n].emplace_back(node.second, cost);
G[node.second].emplace_back(n, cost);
}
vector<int> dp1(n), dp2(n);
function<void(int, int)> dfs = [&](int node, int last) {
for (auto x : G[node])
if (x.first != last) {
dfs(x.first, node);
int cost = dp1[x.first] + x.second;
if (cost >= dp1[node])
dp2[node] = dp1[node], dp1[node] = cost;
else if (cost > dp2[node])
dp2[node] = cost;
}
};
dfs(0, -1);
for (int i = 0; i < n; ++i)
ans = max(ans, dp1[i] + dp2[i]);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
25292 KB |
Output is correct |
2 |
Correct |
104 ms |
24748 KB |
Output is correct |
3 |
Correct |
60 ms |
18788 KB |
Output is correct |
4 |
Correct |
11 ms |
3872 KB |
Output is correct |
5 |
Correct |
8 ms |
2644 KB |
Output is correct |
6 |
Correct |
18 ms |
5808 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
41 ms |
10840 KB |
Output is correct |
9 |
Correct |
58 ms |
15196 KB |
Output is correct |
10 |
Correct |
1 ms |
444 KB |
Output is correct |
11 |
Correct |
92 ms |
18860 KB |
Output is correct |
12 |
Correct |
117 ms |
21860 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
288 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
25292 KB |
Output is correct |
2 |
Correct |
104 ms |
24748 KB |
Output is correct |
3 |
Correct |
60 ms |
18788 KB |
Output is correct |
4 |
Correct |
11 ms |
3872 KB |
Output is correct |
5 |
Correct |
8 ms |
2644 KB |
Output is correct |
6 |
Correct |
18 ms |
5808 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
41 ms |
10840 KB |
Output is correct |
9 |
Correct |
58 ms |
15196 KB |
Output is correct |
10 |
Correct |
1 ms |
444 KB |
Output is correct |
11 |
Correct |
92 ms |
18860 KB |
Output is correct |
12 |
Correct |
117 ms |
21860 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
0 ms |
208 KB |
Output is correct |
20 |
Correct |
1 ms |
208 KB |
Output is correct |
21 |
Correct |
1 ms |
208 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
300 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
208 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
300 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
288 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
300 KB |
Output is correct |
39 |
Correct |
107 ms |
25252 KB |
Output is correct |
40 |
Correct |
107 ms |
24804 KB |
Output is correct |
41 |
Correct |
74 ms |
18788 KB |
Output is correct |
42 |
Correct |
10 ms |
3900 KB |
Output is correct |
43 |
Correct |
1 ms |
300 KB |
Output is correct |
44 |
Correct |
1 ms |
208 KB |
Output is correct |
45 |
Correct |
1 ms |
208 KB |
Output is correct |
46 |
Correct |
1 ms |
208 KB |
Output is correct |
47 |
Correct |
1 ms |
208 KB |
Output is correct |
48 |
Correct |
1 ms |
208 KB |
Output is correct |
49 |
Correct |
1 ms |
336 KB |
Output is correct |
50 |
Correct |
0 ms |
304 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
0 ms |
204 KB |
Output is correct |
53 |
Correct |
1 ms |
208 KB |
Output is correct |
54 |
Correct |
0 ms |
336 KB |
Output is correct |
55 |
Correct |
1 ms |
336 KB |
Output is correct |
56 |
Correct |
1 ms |
208 KB |
Output is correct |
57 |
Correct |
149 ms |
22044 KB |
Output is correct |
58 |
Correct |
136 ms |
21828 KB |
Output is correct |
59 |
Correct |
164 ms |
22252 KB |
Output is correct |
60 |
Correct |
176 ms |
22344 KB |
Output is correct |
61 |
Correct |
152 ms |
22628 KB |
Output is correct |
62 |
Correct |
143 ms |
22592 KB |
Output is correct |
63 |
Correct |
143 ms |
22528 KB |
Output is correct |
64 |
Correct |
137 ms |
22448 KB |
Output is correct |
65 |
Correct |
133 ms |
22784 KB |
Output is correct |
66 |
Correct |
140 ms |
22696 KB |
Output is correct |
67 |
Incorrect |
137 ms |
22380 KB |
Output isn't correct |
68 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
19268 KB |
Output is correct |
2 |
Correct |
65 ms |
19396 KB |
Output is correct |
3 |
Correct |
82 ms |
19220 KB |
Output is correct |
4 |
Correct |
73 ms |
19484 KB |
Output is correct |
5 |
Correct |
65 ms |
19196 KB |
Output is correct |
6 |
Correct |
70 ms |
20392 KB |
Output is correct |
7 |
Correct |
69 ms |
20228 KB |
Output is correct |
8 |
Correct |
66 ms |
18928 KB |
Output is correct |
9 |
Correct |
65 ms |
18860 KB |
Output is correct |
10 |
Correct |
66 ms |
20028 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
50 ms |
20216 KB |
Output is correct |
13 |
Correct |
46 ms |
20240 KB |
Output is correct |
14 |
Correct |
50 ms |
20268 KB |
Output is correct |
15 |
Correct |
49 ms |
20280 KB |
Output is correct |
16 |
Correct |
51 ms |
20280 KB |
Output is correct |
17 |
Correct |
54 ms |
20288 KB |
Output is correct |
18 |
Correct |
45 ms |
20256 KB |
Output is correct |
19 |
Correct |
48 ms |
20184 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
844 KB |
Output is correct |
23 |
Correct |
48 ms |
20280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
0 ms |
208 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
1 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
300 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
288 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
300 KB |
Output is correct |
26 |
Correct |
1 ms |
436 KB |
Output is correct |
27 |
Correct |
2 ms |
716 KB |
Output is correct |
28 |
Correct |
2 ms |
956 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
2 ms |
716 KB |
Output is correct |
31 |
Correct |
2 ms |
844 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
692 KB |
Output is correct |
34 |
Correct |
2 ms |
952 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
1 ms |
204 KB |
Output is correct |
40 |
Correct |
0 ms |
300 KB |
Output is correct |
41 |
Correct |
1 ms |
204 KB |
Output is correct |
42 |
Correct |
0 ms |
204 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
0 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
204 KB |
Output is correct |
47 |
Correct |
1 ms |
296 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
1 ms |
204 KB |
Output is correct |
50 |
Correct |
1 ms |
204 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
0 ms |
204 KB |
Output is correct |
53 |
Correct |
0 ms |
204 KB |
Output is correct |
54 |
Correct |
3 ms |
1100 KB |
Output is correct |
55 |
Correct |
3 ms |
1228 KB |
Output is correct |
56 |
Correct |
2 ms |
848 KB |
Output is correct |
57 |
Correct |
2 ms |
844 KB |
Output is correct |
58 |
Correct |
3 ms |
948 KB |
Output is correct |
59 |
Correct |
3 ms |
872 KB |
Output is correct |
60 |
Correct |
2 ms |
844 KB |
Output is correct |
61 |
Correct |
2 ms |
844 KB |
Output is correct |
62 |
Correct |
3 ms |
844 KB |
Output is correct |
63 |
Incorrect |
3 ms |
844 KB |
Output isn't correct |
64 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
25292 KB |
Output is correct |
2 |
Correct |
104 ms |
24748 KB |
Output is correct |
3 |
Correct |
60 ms |
18788 KB |
Output is correct |
4 |
Correct |
11 ms |
3872 KB |
Output is correct |
5 |
Correct |
8 ms |
2644 KB |
Output is correct |
6 |
Correct |
18 ms |
5808 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
41 ms |
10840 KB |
Output is correct |
9 |
Correct |
58 ms |
15196 KB |
Output is correct |
10 |
Correct |
1 ms |
444 KB |
Output is correct |
11 |
Correct |
92 ms |
18860 KB |
Output is correct |
12 |
Correct |
117 ms |
21860 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
208 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Correct |
0 ms |
208 KB |
Output is correct |
20 |
Correct |
1 ms |
208 KB |
Output is correct |
21 |
Correct |
1 ms |
208 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
300 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
208 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
300 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
288 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
300 KB |
Output is correct |
39 |
Correct |
107 ms |
25252 KB |
Output is correct |
40 |
Correct |
107 ms |
24804 KB |
Output is correct |
41 |
Correct |
74 ms |
18788 KB |
Output is correct |
42 |
Correct |
10 ms |
3900 KB |
Output is correct |
43 |
Correct |
1 ms |
300 KB |
Output is correct |
44 |
Correct |
1 ms |
208 KB |
Output is correct |
45 |
Correct |
1 ms |
208 KB |
Output is correct |
46 |
Correct |
1 ms |
208 KB |
Output is correct |
47 |
Correct |
1 ms |
208 KB |
Output is correct |
48 |
Correct |
1 ms |
208 KB |
Output is correct |
49 |
Correct |
1 ms |
336 KB |
Output is correct |
50 |
Correct |
0 ms |
304 KB |
Output is correct |
51 |
Correct |
1 ms |
204 KB |
Output is correct |
52 |
Correct |
0 ms |
204 KB |
Output is correct |
53 |
Correct |
1 ms |
208 KB |
Output is correct |
54 |
Correct |
0 ms |
336 KB |
Output is correct |
55 |
Correct |
1 ms |
336 KB |
Output is correct |
56 |
Correct |
1 ms |
208 KB |
Output is correct |
57 |
Correct |
149 ms |
22044 KB |
Output is correct |
58 |
Correct |
136 ms |
21828 KB |
Output is correct |
59 |
Correct |
164 ms |
22252 KB |
Output is correct |
60 |
Correct |
176 ms |
22344 KB |
Output is correct |
61 |
Correct |
152 ms |
22628 KB |
Output is correct |
62 |
Correct |
143 ms |
22592 KB |
Output is correct |
63 |
Correct |
143 ms |
22528 KB |
Output is correct |
64 |
Correct |
137 ms |
22448 KB |
Output is correct |
65 |
Correct |
133 ms |
22784 KB |
Output is correct |
66 |
Correct |
140 ms |
22696 KB |
Output is correct |
67 |
Incorrect |
137 ms |
22380 KB |
Output isn't correct |
68 |
Halted |
0 ms |
0 KB |
- |