#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);
vector<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)> dfs = [&](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) {
dfs(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();
dfs(i, -1);
dfs2(i, -1);
pair<int, int> ans = {INT_MAX, 0};
//cout << "ROOT : " << i << '\n';
for (auto x : cn)
//printf("%d : U %d D %d\n", x, dpu[x], dpb[x]);
ans = min(ans, {max(dpu[x],dpb[x]),x});
cur.emplace_back(ans);
//cout << ans.first << ' ' << ans.second << '\n';
}
pair<int, int> node = cur.back();
cur.pop_back();
for (auto [c, n] : cur) {
//cout << "ADD " << n << ' ' << node.second << '\n';
G[n].emplace_back(node.second, cost);
G[node.second].emplace_back(n, cost);
if (c < node.second)
node.first = max(node.first, c + cost);
else
node.first = max(node.first, c + cost), node.second = n;
}
dpb.assign(n, 0), dpu.assign(n, 0);
pref.assign(n, vector<int>()), suf.assign(n, vector<int>());
dfs(0, -1);
dfs2(0, -1);
for (int i = 0; i < n; ++i)
ans = max(ans, dpu[i] + dpb[i]);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
24572 KB |
Output is correct |
2 |
Correct |
158 ms |
24048 KB |
Output is correct |
3 |
Correct |
76 ms |
18308 KB |
Output is correct |
4 |
Correct |
12 ms |
3780 KB |
Output is correct |
5 |
Correct |
10 ms |
2636 KB |
Output is correct |
6 |
Correct |
22 ms |
5580 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
69 ms |
10720 KB |
Output is correct |
9 |
Correct |
108 ms |
14856 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
126 ms |
18624 KB |
Output is correct |
12 |
Correct |
155 ms |
21312 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
280 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
216 KB |
Output is correct |
20 |
Correct |
1 ms |
216 KB |
Output is correct |
21 |
Correct |
1 ms |
216 KB |
Output is correct |
22 |
Correct |
1 ms |
216 KB |
Output is correct |
23 |
Correct |
1 ms |
216 KB |
Output is correct |
24 |
Correct |
1 ms |
216 KB |
Output is correct |
25 |
Correct |
1 ms |
216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
24572 KB |
Output is correct |
2 |
Correct |
158 ms |
24048 KB |
Output is correct |
3 |
Correct |
76 ms |
18308 KB |
Output is correct |
4 |
Correct |
12 ms |
3780 KB |
Output is correct |
5 |
Correct |
10 ms |
2636 KB |
Output is correct |
6 |
Correct |
22 ms |
5580 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
69 ms |
10720 KB |
Output is correct |
9 |
Correct |
108 ms |
14856 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
126 ms |
18624 KB |
Output is correct |
12 |
Correct |
155 ms |
21312 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 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 |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
288 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
280 KB |
Output is correct |
23 |
Correct |
1 ms |
296 KB |
Output is correct |
24 |
Correct |
1 ms |
296 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
300 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
216 KB |
Output is correct |
33 |
Correct |
1 ms |
216 KB |
Output is correct |
34 |
Correct |
1 ms |
216 KB |
Output is correct |
35 |
Correct |
1 ms |
216 KB |
Output is correct |
36 |
Correct |
1 ms |
216 KB |
Output is correct |
37 |
Correct |
1 ms |
216 KB |
Output is correct |
38 |
Correct |
1 ms |
216 KB |
Output is correct |
39 |
Correct |
135 ms |
24536 KB |
Output is correct |
40 |
Correct |
139 ms |
24056 KB |
Output is correct |
41 |
Correct |
86 ms |
18356 KB |
Output is correct |
42 |
Correct |
12 ms |
3800 KB |
Output is correct |
43 |
Correct |
1 ms |
312 KB |
Output is correct |
44 |
Correct |
1 ms |
216 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
300 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
216 KB |
Output is correct |
49 |
Correct |
0 ms |
208 KB |
Output is correct |
50 |
Correct |
1 ms |
208 KB |
Output is correct |
51 |
Correct |
1 ms |
208 KB |
Output is correct |
52 |
Correct |
0 ms |
300 KB |
Output is correct |
53 |
Correct |
1 ms |
208 KB |
Output is correct |
54 |
Correct |
1 ms |
304 KB |
Output is correct |
55 |
Correct |
1 ms |
304 KB |
Output is correct |
56 |
Correct |
1 ms |
208 KB |
Output is correct |
57 |
Incorrect |
183 ms |
21252 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
78 ms |
19868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
280 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
296 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
216 KB |
Output is correct |
20 |
Correct |
1 ms |
216 KB |
Output is correct |
21 |
Correct |
1 ms |
216 KB |
Output is correct |
22 |
Correct |
1 ms |
216 KB |
Output is correct |
23 |
Correct |
1 ms |
216 KB |
Output is correct |
24 |
Correct |
1 ms |
216 KB |
Output is correct |
25 |
Correct |
1 ms |
216 KB |
Output is correct |
26 |
Correct |
1 ms |
452 KB |
Output is correct |
27 |
Incorrect |
3 ms |
716 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
24572 KB |
Output is correct |
2 |
Correct |
158 ms |
24048 KB |
Output is correct |
3 |
Correct |
76 ms |
18308 KB |
Output is correct |
4 |
Correct |
12 ms |
3780 KB |
Output is correct |
5 |
Correct |
10 ms |
2636 KB |
Output is correct |
6 |
Correct |
22 ms |
5580 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
69 ms |
10720 KB |
Output is correct |
9 |
Correct |
108 ms |
14856 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
126 ms |
18624 KB |
Output is correct |
12 |
Correct |
155 ms |
21312 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 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 |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
288 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
280 KB |
Output is correct |
23 |
Correct |
1 ms |
296 KB |
Output is correct |
24 |
Correct |
1 ms |
296 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
1 ms |
300 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
216 KB |
Output is correct |
33 |
Correct |
1 ms |
216 KB |
Output is correct |
34 |
Correct |
1 ms |
216 KB |
Output is correct |
35 |
Correct |
1 ms |
216 KB |
Output is correct |
36 |
Correct |
1 ms |
216 KB |
Output is correct |
37 |
Correct |
1 ms |
216 KB |
Output is correct |
38 |
Correct |
1 ms |
216 KB |
Output is correct |
39 |
Correct |
135 ms |
24536 KB |
Output is correct |
40 |
Correct |
139 ms |
24056 KB |
Output is correct |
41 |
Correct |
86 ms |
18356 KB |
Output is correct |
42 |
Correct |
12 ms |
3800 KB |
Output is correct |
43 |
Correct |
1 ms |
312 KB |
Output is correct |
44 |
Correct |
1 ms |
216 KB |
Output is correct |
45 |
Correct |
1 ms |
204 KB |
Output is correct |
46 |
Correct |
1 ms |
300 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
216 KB |
Output is correct |
49 |
Correct |
0 ms |
208 KB |
Output is correct |
50 |
Correct |
1 ms |
208 KB |
Output is correct |
51 |
Correct |
1 ms |
208 KB |
Output is correct |
52 |
Correct |
0 ms |
300 KB |
Output is correct |
53 |
Correct |
1 ms |
208 KB |
Output is correct |
54 |
Correct |
1 ms |
304 KB |
Output is correct |
55 |
Correct |
1 ms |
304 KB |
Output is correct |
56 |
Correct |
1 ms |
208 KB |
Output is correct |
57 |
Incorrect |
183 ms |
21252 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |