#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define pb push_back
#define f first
#define s second
vector<pii>ad[200000];
map<ll,ll>m[200000];
ll ans = (ll)1e16,x;
void dfs(int node,int parent,int depth,ll cost){
long long t = cost * 2 + x;
m[node][cost] = depth;
for (auto i : ad[node])
if(i.f != parent){
dfs(i.f,node,depth + 1,cost + i.s);
if((int)m[i.f].size() > (int)m[node].size()) m[node].swap(m[i.f]);
for (auto v : m[i.f])
if(m[node].find(t - v.f) != m[node].end())
ans = min(ans,m[node][t - v.f] + v.s - 2 * depth);
for (auto v : m[i.f])
if(m[node].find(v.f) != m[node].end()) m[node][v.f] = min(m[node][v.f],v.s);
else m[node].insert(v);
}
}
int best_path(int N, int K, int H[][2], int L[]){
x = K;
for (int i = 0; i < N - 1;++i){
ad[H[i][0]].push_back({H[i][1],L[i]});
ad[H[i][1]].push_back({H[i][0],L[i]});
}
dfs(0,0,0,0);
return ans == (ll)1e16 ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14392 KB |
Output is correct |
3 |
Correct |
7 ms |
14400 KB |
Output is correct |
4 |
Correct |
6 ms |
14344 KB |
Output is correct |
5 |
Correct |
6 ms |
14404 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14336 KB |
Output is correct |
8 |
Correct |
6 ms |
14376 KB |
Output is correct |
9 |
Correct |
8 ms |
14464 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14400 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
6 ms |
14400 KB |
Output is correct |
16 |
Correct |
6 ms |
14420 KB |
Output is correct |
17 |
Correct |
6 ms |
14424 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14392 KB |
Output is correct |
3 |
Correct |
7 ms |
14400 KB |
Output is correct |
4 |
Correct |
6 ms |
14344 KB |
Output is correct |
5 |
Correct |
6 ms |
14404 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14336 KB |
Output is correct |
8 |
Correct |
6 ms |
14376 KB |
Output is correct |
9 |
Correct |
8 ms |
14464 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14400 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
6 ms |
14400 KB |
Output is correct |
16 |
Correct |
6 ms |
14420 KB |
Output is correct |
17 |
Correct |
6 ms |
14424 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
6 ms |
14400 KB |
Output is correct |
20 |
Correct |
6 ms |
14420 KB |
Output is correct |
21 |
Correct |
7 ms |
14548 KB |
Output is correct |
22 |
Correct |
7 ms |
14676 KB |
Output is correct |
23 |
Correct |
8 ms |
14668 KB |
Output is correct |
24 |
Correct |
7 ms |
14676 KB |
Output is correct |
25 |
Correct |
7 ms |
14668 KB |
Output is correct |
26 |
Correct |
7 ms |
14664 KB |
Output is correct |
27 |
Correct |
9 ms |
14548 KB |
Output is correct |
28 |
Correct |
12 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14696 KB |
Output is correct |
30 |
Correct |
8 ms |
14672 KB |
Output is correct |
31 |
Correct |
7 ms |
14676 KB |
Output is correct |
32 |
Correct |
7 ms |
14676 KB |
Output is correct |
33 |
Correct |
7 ms |
14676 KB |
Output is correct |
34 |
Correct |
7 ms |
14672 KB |
Output is correct |
35 |
Correct |
7 ms |
14676 KB |
Output is correct |
36 |
Correct |
7 ms |
14660 KB |
Output is correct |
37 |
Correct |
8 ms |
14660 KB |
Output is correct |
38 |
Correct |
8 ms |
14676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14392 KB |
Output is correct |
3 |
Correct |
7 ms |
14400 KB |
Output is correct |
4 |
Correct |
6 ms |
14344 KB |
Output is correct |
5 |
Correct |
6 ms |
14404 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14336 KB |
Output is correct |
8 |
Correct |
6 ms |
14376 KB |
Output is correct |
9 |
Correct |
8 ms |
14464 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14400 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
6 ms |
14400 KB |
Output is correct |
16 |
Correct |
6 ms |
14420 KB |
Output is correct |
17 |
Correct |
6 ms |
14424 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
97 ms |
37644 KB |
Output is correct |
20 |
Correct |
109 ms |
37636 KB |
Output is correct |
21 |
Correct |
93 ms |
37360 KB |
Output is correct |
22 |
Correct |
95 ms |
36876 KB |
Output is correct |
23 |
Correct |
132 ms |
49812 KB |
Output is correct |
24 |
Correct |
101 ms |
39864 KB |
Output is correct |
25 |
Correct |
91 ms |
39264 KB |
Output is correct |
26 |
Correct |
49 ms |
48216 KB |
Output is correct |
27 |
Correct |
158 ms |
49496 KB |
Output is correct |
28 |
Correct |
261 ms |
94824 KB |
Output is correct |
29 |
Correct |
264 ms |
92732 KB |
Output is correct |
30 |
Correct |
162 ms |
49668 KB |
Output is correct |
31 |
Correct |
164 ms |
49476 KB |
Output is correct |
32 |
Correct |
206 ms |
49768 KB |
Output is correct |
33 |
Correct |
188 ms |
54348 KB |
Output is correct |
34 |
Correct |
265 ms |
87256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14392 KB |
Output is correct |
3 |
Correct |
7 ms |
14400 KB |
Output is correct |
4 |
Correct |
6 ms |
14344 KB |
Output is correct |
5 |
Correct |
6 ms |
14404 KB |
Output is correct |
6 |
Correct |
6 ms |
14420 KB |
Output is correct |
7 |
Correct |
6 ms |
14336 KB |
Output is correct |
8 |
Correct |
6 ms |
14376 KB |
Output is correct |
9 |
Correct |
8 ms |
14464 KB |
Output is correct |
10 |
Correct |
6 ms |
14420 KB |
Output is correct |
11 |
Correct |
6 ms |
14420 KB |
Output is correct |
12 |
Correct |
6 ms |
14420 KB |
Output is correct |
13 |
Correct |
6 ms |
14400 KB |
Output is correct |
14 |
Correct |
6 ms |
14420 KB |
Output is correct |
15 |
Correct |
6 ms |
14400 KB |
Output is correct |
16 |
Correct |
6 ms |
14420 KB |
Output is correct |
17 |
Correct |
6 ms |
14424 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
6 ms |
14400 KB |
Output is correct |
20 |
Correct |
6 ms |
14420 KB |
Output is correct |
21 |
Correct |
7 ms |
14548 KB |
Output is correct |
22 |
Correct |
7 ms |
14676 KB |
Output is correct |
23 |
Correct |
8 ms |
14668 KB |
Output is correct |
24 |
Correct |
7 ms |
14676 KB |
Output is correct |
25 |
Correct |
7 ms |
14668 KB |
Output is correct |
26 |
Correct |
7 ms |
14664 KB |
Output is correct |
27 |
Correct |
9 ms |
14548 KB |
Output is correct |
28 |
Correct |
12 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14696 KB |
Output is correct |
30 |
Correct |
8 ms |
14672 KB |
Output is correct |
31 |
Correct |
7 ms |
14676 KB |
Output is correct |
32 |
Correct |
7 ms |
14676 KB |
Output is correct |
33 |
Correct |
7 ms |
14676 KB |
Output is correct |
34 |
Correct |
7 ms |
14672 KB |
Output is correct |
35 |
Correct |
7 ms |
14676 KB |
Output is correct |
36 |
Correct |
7 ms |
14660 KB |
Output is correct |
37 |
Correct |
8 ms |
14660 KB |
Output is correct |
38 |
Correct |
8 ms |
14676 KB |
Output is correct |
39 |
Correct |
97 ms |
37644 KB |
Output is correct |
40 |
Correct |
109 ms |
37636 KB |
Output is correct |
41 |
Correct |
93 ms |
37360 KB |
Output is correct |
42 |
Correct |
95 ms |
36876 KB |
Output is correct |
43 |
Correct |
132 ms |
49812 KB |
Output is correct |
44 |
Correct |
101 ms |
39864 KB |
Output is correct |
45 |
Correct |
91 ms |
39264 KB |
Output is correct |
46 |
Correct |
49 ms |
48216 KB |
Output is correct |
47 |
Correct |
158 ms |
49496 KB |
Output is correct |
48 |
Correct |
261 ms |
94824 KB |
Output is correct |
49 |
Correct |
264 ms |
92732 KB |
Output is correct |
50 |
Correct |
162 ms |
49668 KB |
Output is correct |
51 |
Correct |
164 ms |
49476 KB |
Output is correct |
52 |
Correct |
206 ms |
49768 KB |
Output is correct |
53 |
Correct |
188 ms |
54348 KB |
Output is correct |
54 |
Correct |
265 ms |
87256 KB |
Output is correct |
55 |
Correct |
17 ms |
17620 KB |
Output is correct |
56 |
Correct |
13 ms |
16328 KB |
Output is correct |
57 |
Correct |
59 ms |
35032 KB |
Output is correct |
58 |
Correct |
45 ms |
27580 KB |
Output is correct |
59 |
Correct |
89 ms |
54408 KB |
Output is correct |
60 |
Correct |
243 ms |
93272 KB |
Output is correct |
61 |
Correct |
183 ms |
53032 KB |
Output is correct |
62 |
Correct |
149 ms |
49560 KB |
Output is correct |
63 |
Correct |
207 ms |
49560 KB |
Output is correct |
64 |
Correct |
369 ms |
104500 KB |
Output is correct |
65 |
Correct |
354 ms |
102468 KB |
Output is correct |
66 |
Correct |
262 ms |
88272 KB |
Output is correct |
67 |
Correct |
134 ms |
42340 KB |
Output is correct |
68 |
Correct |
288 ms |
65988 KB |
Output is correct |
69 |
Correct |
319 ms |
70644 KB |
Output is correct |
70 |
Correct |
267 ms |
63692 KB |
Output is correct |