#include "race.h"
#include<bits/stdc++.h>
#define sz(v) (int)v.size()
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define nl "\n"
using namespace std;
using pii = pair<int, int>;
const int N = (int)2e5 + 7; // make sure this is right
const int inf = (int)1e9;
const ll INF = (ll)3e18 + 7;
const ll MOD = (ll)1e9 + 7; // make sure this is right
pii dir[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, k, d[N], ans = inf;
vector<pii> g[N];
map<ll, int> mn[N];
int comb(map<ll, int> &a, map<ll, int> &b, int q, ll w) {
int res = inf;
if(sz(a) < sz(b)) {
for(auto [x, y] : a) {
if(b.count(k + 2 * w - x)) {
res = min(res, y + b[k + 2 * w - x] - 2 * q);
}
}
for(auto [x, y] : a) {
if(!b.count(x)) {
b[x] = y;
} else b[x] = min(b[x], y);
}
a.swap(b);
b.clear();
} else {
for(auto [x, y] : b) {
if(a.count(k + 2 * w - x)) {
res = min(res, y + a[k + 2 * w - x] - 2 * q);
}
}
for(auto [x, y] : b) {
if(!a.count(x)) a[x] = y;
else a[x] = min(a[x], y);
}
}
return res;
}
void dfs(int v, int pr, ll len) {
for(auto [to, w] : g[v]) {
if(to == pr) continue;
d[to] = d[v] + 1;
dfs(to, v, len + w);
}
for(auto [to, w] : g[v]) {
ans = min(ans, comb(mn[v], mn[to], d[v], len));
}
mn[v][len] = d[v];
if(mn[v].count(len + k)) {
ans = min(ans, mn[v][len + k] - d[v]);
}
}
int best_path(int n_, int k_, int h_[][2], int l_[]) {
n = n_;
k = k_;
for(int i = 0; i < n - 1; ++i) {
h_[i][0]++;
h_[i][1]++;
g[h_[i][0]].pb({h_[i][1], l_[i]});
g[h_[i][1]].pb({h_[i][0], l_[i]});
}
dfs(1, 1, 0);
return (ans == inf ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14320 KB |
Output is correct |
6 |
Correct |
7 ms |
14392 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14404 KB |
Output is correct |
13 |
Correct |
9 ms |
14432 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14400 KB |
Output is correct |
17 |
Correct |
7 ms |
14380 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14320 KB |
Output is correct |
6 |
Correct |
7 ms |
14392 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14404 KB |
Output is correct |
13 |
Correct |
9 ms |
14432 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14400 KB |
Output is correct |
17 |
Correct |
7 ms |
14380 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14512 KB |
Output is correct |
20 |
Correct |
8 ms |
14360 KB |
Output is correct |
21 |
Correct |
8 ms |
14608 KB |
Output is correct |
22 |
Correct |
9 ms |
14608 KB |
Output is correct |
23 |
Correct |
9 ms |
14548 KB |
Output is correct |
24 |
Correct |
9 ms |
14548 KB |
Output is correct |
25 |
Correct |
8 ms |
14624 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14512 KB |
Output is correct |
28 |
Correct |
8 ms |
14548 KB |
Output is correct |
29 |
Correct |
8 ms |
14544 KB |
Output is correct |
30 |
Correct |
8 ms |
14548 KB |
Output is correct |
31 |
Correct |
9 ms |
14548 KB |
Output is correct |
32 |
Correct |
9 ms |
14548 KB |
Output is correct |
33 |
Correct |
9 ms |
14548 KB |
Output is correct |
34 |
Correct |
8 ms |
14548 KB |
Output is correct |
35 |
Correct |
10 ms |
14548 KB |
Output is correct |
36 |
Correct |
11 ms |
14584 KB |
Output is correct |
37 |
Correct |
8 ms |
14536 KB |
Output is correct |
38 |
Correct |
8 ms |
14496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14320 KB |
Output is correct |
6 |
Correct |
7 ms |
14392 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14404 KB |
Output is correct |
13 |
Correct |
9 ms |
14432 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14400 KB |
Output is correct |
17 |
Correct |
7 ms |
14380 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
108 ms |
28888 KB |
Output is correct |
20 |
Correct |
105 ms |
28600 KB |
Output is correct |
21 |
Correct |
107 ms |
29236 KB |
Output is correct |
22 |
Correct |
112 ms |
30428 KB |
Output is correct |
23 |
Correct |
143 ms |
38288 KB |
Output is correct |
24 |
Correct |
122 ms |
37732 KB |
Output is correct |
25 |
Correct |
79 ms |
27632 KB |
Output is correct |
26 |
Correct |
49 ms |
32968 KB |
Output is correct |
27 |
Correct |
179 ms |
40836 KB |
Output is correct |
28 |
Correct |
232 ms |
64380 KB |
Output is correct |
29 |
Correct |
224 ms |
63428 KB |
Output is correct |
30 |
Correct |
180 ms |
40728 KB |
Output is correct |
31 |
Correct |
217 ms |
40776 KB |
Output is correct |
32 |
Correct |
232 ms |
40920 KB |
Output is correct |
33 |
Correct |
183 ms |
34500 KB |
Output is correct |
34 |
Correct |
328 ms |
58384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14320 KB |
Output is correct |
6 |
Correct |
7 ms |
14392 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
9 ms |
14420 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
9 ms |
14404 KB |
Output is correct |
13 |
Correct |
9 ms |
14432 KB |
Output is correct |
14 |
Correct |
8 ms |
14420 KB |
Output is correct |
15 |
Correct |
8 ms |
14420 KB |
Output is correct |
16 |
Correct |
8 ms |
14400 KB |
Output is correct |
17 |
Correct |
7 ms |
14380 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14512 KB |
Output is correct |
20 |
Correct |
8 ms |
14360 KB |
Output is correct |
21 |
Correct |
8 ms |
14608 KB |
Output is correct |
22 |
Correct |
9 ms |
14608 KB |
Output is correct |
23 |
Correct |
9 ms |
14548 KB |
Output is correct |
24 |
Correct |
9 ms |
14548 KB |
Output is correct |
25 |
Correct |
8 ms |
14624 KB |
Output is correct |
26 |
Correct |
8 ms |
14676 KB |
Output is correct |
27 |
Correct |
8 ms |
14512 KB |
Output is correct |
28 |
Correct |
8 ms |
14548 KB |
Output is correct |
29 |
Correct |
8 ms |
14544 KB |
Output is correct |
30 |
Correct |
8 ms |
14548 KB |
Output is correct |
31 |
Correct |
9 ms |
14548 KB |
Output is correct |
32 |
Correct |
9 ms |
14548 KB |
Output is correct |
33 |
Correct |
9 ms |
14548 KB |
Output is correct |
34 |
Correct |
8 ms |
14548 KB |
Output is correct |
35 |
Correct |
10 ms |
14548 KB |
Output is correct |
36 |
Correct |
11 ms |
14584 KB |
Output is correct |
37 |
Correct |
8 ms |
14536 KB |
Output is correct |
38 |
Correct |
8 ms |
14496 KB |
Output is correct |
39 |
Correct |
108 ms |
28888 KB |
Output is correct |
40 |
Correct |
105 ms |
28600 KB |
Output is correct |
41 |
Correct |
107 ms |
29236 KB |
Output is correct |
42 |
Correct |
112 ms |
30428 KB |
Output is correct |
43 |
Correct |
143 ms |
38288 KB |
Output is correct |
44 |
Correct |
122 ms |
37732 KB |
Output is correct |
45 |
Correct |
79 ms |
27632 KB |
Output is correct |
46 |
Correct |
49 ms |
32968 KB |
Output is correct |
47 |
Correct |
179 ms |
40836 KB |
Output is correct |
48 |
Correct |
232 ms |
64380 KB |
Output is correct |
49 |
Correct |
224 ms |
63428 KB |
Output is correct |
50 |
Correct |
180 ms |
40728 KB |
Output is correct |
51 |
Correct |
217 ms |
40776 KB |
Output is correct |
52 |
Correct |
232 ms |
40920 KB |
Output is correct |
53 |
Correct |
183 ms |
34500 KB |
Output is correct |
54 |
Correct |
328 ms |
58384 KB |
Output is correct |
55 |
Correct |
20 ms |
16888 KB |
Output is correct |
56 |
Correct |
14 ms |
15556 KB |
Output is correct |
57 |
Correct |
69 ms |
28108 KB |
Output is correct |
58 |
Correct |
54 ms |
27112 KB |
Output is correct |
59 |
Correct |
81 ms |
39104 KB |
Output is correct |
60 |
Correct |
241 ms |
63532 KB |
Output is correct |
61 |
Correct |
209 ms |
44356 KB |
Output is correct |
62 |
Correct |
182 ms |
40888 KB |
Output is correct |
63 |
Correct |
214 ms |
40860 KB |
Output is correct |
64 |
Correct |
417 ms |
68132 KB |
Output is correct |
65 |
Correct |
449 ms |
84444 KB |
Output is correct |
66 |
Correct |
244 ms |
61464 KB |
Output is correct |
67 |
Correct |
173 ms |
41292 KB |
Output is correct |
68 |
Correct |
358 ms |
59616 KB |
Output is correct |
69 |
Correct |
350 ms |
64452 KB |
Output is correct |
70 |
Correct |
300 ms |
57628 KB |
Output is correct |