#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
#define oo 1e9
const int MAX = 2e5 + 5;
vector<pii> g[MAX];
int n, k;
map<ll, int> mp[MAX];
int ans = oo;
void dfs(int node, int p, ll d, int h){
for(pii to : g[node]){
if(to.first == p) continue;
dfs(to.first, node, d + to.second, h + 1);
if(mp[to.first].size() > mp[node].size()){
swap(mp[node], mp[to.first]);
}
}
for(pii to : g[node]){
if(to.first == p) continue;
for(auto a : mp[to.first]){
if(!a.second) continue;
ll need = d + (k - (a.first - d));
if(need <= d || mp[node].find(need) == mp[node].end()) continue;
ans = min(ans, (a.second - h) + (mp[node][need] - h));
}
for(auto a : mp[to.first]){
if(!a.second) continue;
if(mp[node].find(a.first) == mp[node].end()){
mp[node][a.first] = a.second;
}
else{
mp[node][a.first] = min(a.second, mp[node][a.first]);
}
}
}
mp[node][d] = h;
if(mp[node].find(d + k) != mp[node].end()){
ans = min(ans, mp[node][d + k] - h);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for(int i = 0; i < n; i++){
g[H[i][0]].push_back({H[i][1], L[i]});
g[H[i][1]].push_back({H[i][0], L[i]});
}
dfs(0, 0, 0, 0);
if(ans == oo) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18108 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
5 ms |
18012 KB |
Output is correct |
5 |
Correct |
4 ms |
18012 KB |
Output is correct |
6 |
Correct |
4 ms |
18176 KB |
Output is correct |
7 |
Correct |
5 ms |
18012 KB |
Output is correct |
8 |
Correct |
5 ms |
18172 KB |
Output is correct |
9 |
Correct |
4 ms |
18204 KB |
Output is correct |
10 |
Correct |
4 ms |
18012 KB |
Output is correct |
11 |
Correct |
4 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18012 KB |
Output is correct |
13 |
Correct |
4 ms |
18100 KB |
Output is correct |
14 |
Correct |
4 ms |
18012 KB |
Output is correct |
15 |
Correct |
4 ms |
18172 KB |
Output is correct |
16 |
Correct |
4 ms |
18012 KB |
Output is correct |
17 |
Correct |
3 ms |
18012 KB |
Output is correct |
18 |
Correct |
4 ms |
18012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18108 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
5 ms |
18012 KB |
Output is correct |
5 |
Correct |
4 ms |
18012 KB |
Output is correct |
6 |
Correct |
4 ms |
18176 KB |
Output is correct |
7 |
Correct |
5 ms |
18012 KB |
Output is correct |
8 |
Correct |
5 ms |
18172 KB |
Output is correct |
9 |
Correct |
4 ms |
18204 KB |
Output is correct |
10 |
Correct |
4 ms |
18012 KB |
Output is correct |
11 |
Correct |
4 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18012 KB |
Output is correct |
13 |
Correct |
4 ms |
18100 KB |
Output is correct |
14 |
Correct |
4 ms |
18012 KB |
Output is correct |
15 |
Correct |
4 ms |
18172 KB |
Output is correct |
16 |
Correct |
4 ms |
18012 KB |
Output is correct |
17 |
Correct |
3 ms |
18012 KB |
Output is correct |
18 |
Correct |
4 ms |
18012 KB |
Output is correct |
19 |
Correct |
4 ms |
18012 KB |
Output is correct |
20 |
Correct |
5 ms |
18168 KB |
Output is correct |
21 |
Correct |
4 ms |
18268 KB |
Output is correct |
22 |
Correct |
5 ms |
18452 KB |
Output is correct |
23 |
Correct |
6 ms |
18268 KB |
Output is correct |
24 |
Correct |
5 ms |
18268 KB |
Output is correct |
25 |
Correct |
5 ms |
18264 KB |
Output is correct |
26 |
Correct |
5 ms |
18268 KB |
Output is correct |
27 |
Correct |
4 ms |
18268 KB |
Output is correct |
28 |
Correct |
4 ms |
18268 KB |
Output is correct |
29 |
Correct |
5 ms |
18436 KB |
Output is correct |
30 |
Correct |
5 ms |
18268 KB |
Output is correct |
31 |
Correct |
5 ms |
18256 KB |
Output is correct |
32 |
Correct |
4 ms |
18268 KB |
Output is correct |
33 |
Correct |
5 ms |
18268 KB |
Output is correct |
34 |
Correct |
4 ms |
18268 KB |
Output is correct |
35 |
Correct |
5 ms |
18268 KB |
Output is correct |
36 |
Correct |
4 ms |
18268 KB |
Output is correct |
37 |
Correct |
4 ms |
18268 KB |
Output is correct |
38 |
Correct |
4 ms |
18172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18108 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
5 ms |
18012 KB |
Output is correct |
5 |
Correct |
4 ms |
18012 KB |
Output is correct |
6 |
Correct |
4 ms |
18176 KB |
Output is correct |
7 |
Correct |
5 ms |
18012 KB |
Output is correct |
8 |
Correct |
5 ms |
18172 KB |
Output is correct |
9 |
Correct |
4 ms |
18204 KB |
Output is correct |
10 |
Correct |
4 ms |
18012 KB |
Output is correct |
11 |
Correct |
4 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18012 KB |
Output is correct |
13 |
Correct |
4 ms |
18100 KB |
Output is correct |
14 |
Correct |
4 ms |
18012 KB |
Output is correct |
15 |
Correct |
4 ms |
18172 KB |
Output is correct |
16 |
Correct |
4 ms |
18012 KB |
Output is correct |
17 |
Correct |
3 ms |
18012 KB |
Output is correct |
18 |
Correct |
4 ms |
18012 KB |
Output is correct |
19 |
Correct |
77 ms |
37896 KB |
Output is correct |
20 |
Correct |
83 ms |
37720 KB |
Output is correct |
21 |
Correct |
96 ms |
37812 KB |
Output is correct |
22 |
Correct |
90 ms |
37944 KB |
Output is correct |
23 |
Correct |
117 ms |
51168 KB |
Output is correct |
24 |
Correct |
101 ms |
43552 KB |
Output is correct |
25 |
Correct |
58 ms |
35924 KB |
Output is correct |
26 |
Correct |
47 ms |
43644 KB |
Output is correct |
27 |
Correct |
144 ms |
46828 KB |
Output is correct |
28 |
Correct |
166 ms |
79708 KB |
Output is correct |
29 |
Correct |
197 ms |
78184 KB |
Output is correct |
30 |
Correct |
132 ms |
46676 KB |
Output is correct |
31 |
Correct |
141 ms |
46828 KB |
Output is correct |
32 |
Correct |
207 ms |
46928 KB |
Output is correct |
33 |
Correct |
155 ms |
45400 KB |
Output is correct |
34 |
Correct |
239 ms |
78416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18012 KB |
Output is correct |
2 |
Correct |
4 ms |
18108 KB |
Output is correct |
3 |
Correct |
4 ms |
18012 KB |
Output is correct |
4 |
Correct |
5 ms |
18012 KB |
Output is correct |
5 |
Correct |
4 ms |
18012 KB |
Output is correct |
6 |
Correct |
4 ms |
18176 KB |
Output is correct |
7 |
Correct |
5 ms |
18012 KB |
Output is correct |
8 |
Correct |
5 ms |
18172 KB |
Output is correct |
9 |
Correct |
4 ms |
18204 KB |
Output is correct |
10 |
Correct |
4 ms |
18012 KB |
Output is correct |
11 |
Correct |
4 ms |
18012 KB |
Output is correct |
12 |
Correct |
4 ms |
18012 KB |
Output is correct |
13 |
Correct |
4 ms |
18100 KB |
Output is correct |
14 |
Correct |
4 ms |
18012 KB |
Output is correct |
15 |
Correct |
4 ms |
18172 KB |
Output is correct |
16 |
Correct |
4 ms |
18012 KB |
Output is correct |
17 |
Correct |
3 ms |
18012 KB |
Output is correct |
18 |
Correct |
4 ms |
18012 KB |
Output is correct |
19 |
Correct |
4 ms |
18012 KB |
Output is correct |
20 |
Correct |
5 ms |
18168 KB |
Output is correct |
21 |
Correct |
4 ms |
18268 KB |
Output is correct |
22 |
Correct |
5 ms |
18452 KB |
Output is correct |
23 |
Correct |
6 ms |
18268 KB |
Output is correct |
24 |
Correct |
5 ms |
18268 KB |
Output is correct |
25 |
Correct |
5 ms |
18264 KB |
Output is correct |
26 |
Correct |
5 ms |
18268 KB |
Output is correct |
27 |
Correct |
4 ms |
18268 KB |
Output is correct |
28 |
Correct |
4 ms |
18268 KB |
Output is correct |
29 |
Correct |
5 ms |
18436 KB |
Output is correct |
30 |
Correct |
5 ms |
18268 KB |
Output is correct |
31 |
Correct |
5 ms |
18256 KB |
Output is correct |
32 |
Correct |
4 ms |
18268 KB |
Output is correct |
33 |
Correct |
5 ms |
18268 KB |
Output is correct |
34 |
Correct |
4 ms |
18268 KB |
Output is correct |
35 |
Correct |
5 ms |
18268 KB |
Output is correct |
36 |
Correct |
4 ms |
18268 KB |
Output is correct |
37 |
Correct |
4 ms |
18268 KB |
Output is correct |
38 |
Correct |
4 ms |
18172 KB |
Output is correct |
39 |
Correct |
77 ms |
37896 KB |
Output is correct |
40 |
Correct |
83 ms |
37720 KB |
Output is correct |
41 |
Correct |
96 ms |
37812 KB |
Output is correct |
42 |
Correct |
90 ms |
37944 KB |
Output is correct |
43 |
Correct |
117 ms |
51168 KB |
Output is correct |
44 |
Correct |
101 ms |
43552 KB |
Output is correct |
45 |
Correct |
58 ms |
35924 KB |
Output is correct |
46 |
Correct |
47 ms |
43644 KB |
Output is correct |
47 |
Correct |
144 ms |
46828 KB |
Output is correct |
48 |
Correct |
166 ms |
79708 KB |
Output is correct |
49 |
Correct |
197 ms |
78184 KB |
Output is correct |
50 |
Correct |
132 ms |
46676 KB |
Output is correct |
51 |
Correct |
141 ms |
46828 KB |
Output is correct |
52 |
Correct |
207 ms |
46928 KB |
Output is correct |
53 |
Correct |
155 ms |
45400 KB |
Output is correct |
54 |
Correct |
239 ms |
78416 KB |
Output is correct |
55 |
Correct |
15 ms |
20828 KB |
Output is correct |
56 |
Correct |
8 ms |
19548 KB |
Output is correct |
57 |
Correct |
53 ms |
36376 KB |
Output is correct |
58 |
Correct |
43 ms |
32204 KB |
Output is correct |
59 |
Correct |
63 ms |
50000 KB |
Output is correct |
60 |
Correct |
209 ms |
78320 KB |
Output is correct |
61 |
Correct |
172 ms |
50724 KB |
Output is correct |
62 |
Correct |
139 ms |
46868 KB |
Output is correct |
63 |
Correct |
161 ms |
46896 KB |
Output is correct |
64 |
Correct |
360 ms |
96960 KB |
Output is correct |
65 |
Correct |
311 ms |
97656 KB |
Output is correct |
66 |
Correct |
204 ms |
74576 KB |
Output is correct |
67 |
Correct |
138 ms |
46264 KB |
Output is correct |
68 |
Correct |
246 ms |
65108 KB |
Output is correct |
69 |
Correct |
297 ms |
65552 KB |
Output is correct |
70 |
Correct |
241 ms |
63060 KB |
Output is correct |