#include <bits/stdc++.h>
#include "race.h"
using namespace std;
const int maxn = 1e6 + 5;
const int inf = 1e9 + 7;
int depth[maxn];
int road[maxn];
int n, k;
vector<pair<int, int>> tr[maxn];
int dp[maxn][105];
int result = inf;
void solve(int u, int p){
for(auto node : tr[u]){
int v = node.first;
int w = node.second;
if(v == p) continue;
solve(v, u);
for(int in = 0; in + w <= k; in++){
int out = k - w - in;
//cout << "Before : " << result << " " << dp[u][out] << " " << dp[v][in] << endl;
result = min(result, dp[u][out] + dp[v][in] + 1);
//cout << "After : " << result << endl;
}
for(int j = 0; j + w <= k; j++){
dp[u][j + w] = min(dp[u][j + w], dp[v][j] + 1);
}
}
}
void dfs(int u, int p){
for(auto node : tr[u]){
int v = node.first;
int w = node.second;
if(v == p) continue;
depth[v] = depth[u] + 1;
road[v] = road[u] + w;
dfs(v, u);
}
}
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
for(int i = 0; i < n - 1; i++){
int a, b, l;
a = H[i][0];
b = H[i][1];
l = L[i];
tr[a].push_back({b, l});
tr[b].push_back({a, l});
}
if(k <= 100){
for(int i = 0; i < n; i++){
dp[i][0] = 0;
}
for(int i = 0; i < n; i++){
for(int j = 1; j <= k; j++){
dp[i][j] = inf;
}
}
solve(0, -1);
}else{
for(int i = 0; i < n; i++){
depth[i] = 0;
road[i] = 0;
dfs(i, -1);
for(int j = 0; j < n; j++){
if(road[j] == k && result > depth[j]){
result = depth[j];
}
}
}
}
if(result == inf){
return -1;
}else{
return result;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
12 ms |
23796 KB |
Output is correct |
4 |
Correct |
12 ms |
23844 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23852 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23796 KB |
Output is correct |
9 |
Correct |
13 ms |
23788 KB |
Output is correct |
10 |
Correct |
13 ms |
23816 KB |
Output is correct |
11 |
Correct |
12 ms |
23856 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
14 ms |
23832 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23756 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
12 ms |
23796 KB |
Output is correct |
4 |
Correct |
12 ms |
23844 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23852 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23796 KB |
Output is correct |
9 |
Correct |
13 ms |
23788 KB |
Output is correct |
10 |
Correct |
13 ms |
23816 KB |
Output is correct |
11 |
Correct |
12 ms |
23856 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
14 ms |
23832 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23756 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23804 KB |
Output is correct |
20 |
Correct |
13 ms |
23796 KB |
Output is correct |
21 |
Correct |
20 ms |
23788 KB |
Output is correct |
22 |
Correct |
19 ms |
23892 KB |
Output is correct |
23 |
Correct |
22 ms |
23832 KB |
Output is correct |
24 |
Correct |
19 ms |
23808 KB |
Output is correct |
25 |
Correct |
21 ms |
23764 KB |
Output is correct |
26 |
Correct |
19 ms |
23792 KB |
Output is correct |
27 |
Correct |
18 ms |
23812 KB |
Output is correct |
28 |
Correct |
18 ms |
23764 KB |
Output is correct |
29 |
Correct |
20 ms |
23888 KB |
Output is correct |
30 |
Correct |
20 ms |
23812 KB |
Output is correct |
31 |
Correct |
19 ms |
23808 KB |
Output is correct |
32 |
Correct |
18 ms |
23892 KB |
Output is correct |
33 |
Correct |
19 ms |
23892 KB |
Output is correct |
34 |
Correct |
19 ms |
23816 KB |
Output is correct |
35 |
Correct |
19 ms |
23892 KB |
Output is correct |
36 |
Correct |
18 ms |
23916 KB |
Output is correct |
37 |
Correct |
18 ms |
23804 KB |
Output is correct |
38 |
Correct |
20 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
12 ms |
23796 KB |
Output is correct |
4 |
Correct |
12 ms |
23844 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23852 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23796 KB |
Output is correct |
9 |
Correct |
13 ms |
23788 KB |
Output is correct |
10 |
Correct |
13 ms |
23816 KB |
Output is correct |
11 |
Correct |
12 ms |
23856 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
14 ms |
23832 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23756 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
109 ms |
71036 KB |
Output is correct |
20 |
Correct |
120 ms |
71024 KB |
Output is correct |
21 |
Correct |
107 ms |
70988 KB |
Output is correct |
22 |
Correct |
104 ms |
71084 KB |
Output is correct |
23 |
Correct |
98 ms |
71316 KB |
Output is correct |
24 |
Correct |
96 ms |
71356 KB |
Output is correct |
25 |
Correct |
127 ms |
74624 KB |
Output is correct |
26 |
Correct |
78 ms |
78360 KB |
Output is correct |
27 |
Correct |
169 ms |
118292 KB |
Output is correct |
28 |
Correct |
197 ms |
133148 KB |
Output is correct |
29 |
Correct |
186 ms |
132048 KB |
Output is correct |
30 |
Correct |
168 ms |
118272 KB |
Output is correct |
31 |
Correct |
171 ms |
118188 KB |
Output is correct |
32 |
Correct |
230 ms |
118308 KB |
Output is correct |
33 |
Correct |
218 ms |
117264 KB |
Output is correct |
34 |
Correct |
140 ms |
117756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
12 ms |
23796 KB |
Output is correct |
4 |
Correct |
12 ms |
23844 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
12 ms |
23852 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23796 KB |
Output is correct |
9 |
Correct |
13 ms |
23788 KB |
Output is correct |
10 |
Correct |
13 ms |
23816 KB |
Output is correct |
11 |
Correct |
12 ms |
23856 KB |
Output is correct |
12 |
Correct |
12 ms |
23784 KB |
Output is correct |
13 |
Correct |
14 ms |
23832 KB |
Output is correct |
14 |
Correct |
12 ms |
23764 KB |
Output is correct |
15 |
Correct |
13 ms |
23756 KB |
Output is correct |
16 |
Correct |
12 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23804 KB |
Output is correct |
20 |
Correct |
13 ms |
23796 KB |
Output is correct |
21 |
Correct |
20 ms |
23788 KB |
Output is correct |
22 |
Correct |
19 ms |
23892 KB |
Output is correct |
23 |
Correct |
22 ms |
23832 KB |
Output is correct |
24 |
Correct |
19 ms |
23808 KB |
Output is correct |
25 |
Correct |
21 ms |
23764 KB |
Output is correct |
26 |
Correct |
19 ms |
23792 KB |
Output is correct |
27 |
Correct |
18 ms |
23812 KB |
Output is correct |
28 |
Correct |
18 ms |
23764 KB |
Output is correct |
29 |
Correct |
20 ms |
23888 KB |
Output is correct |
30 |
Correct |
20 ms |
23812 KB |
Output is correct |
31 |
Correct |
19 ms |
23808 KB |
Output is correct |
32 |
Correct |
18 ms |
23892 KB |
Output is correct |
33 |
Correct |
19 ms |
23892 KB |
Output is correct |
34 |
Correct |
19 ms |
23816 KB |
Output is correct |
35 |
Correct |
19 ms |
23892 KB |
Output is correct |
36 |
Correct |
18 ms |
23916 KB |
Output is correct |
37 |
Correct |
18 ms |
23804 KB |
Output is correct |
38 |
Correct |
20 ms |
23808 KB |
Output is correct |
39 |
Correct |
109 ms |
71036 KB |
Output is correct |
40 |
Correct |
120 ms |
71024 KB |
Output is correct |
41 |
Correct |
107 ms |
70988 KB |
Output is correct |
42 |
Correct |
104 ms |
71084 KB |
Output is correct |
43 |
Correct |
98 ms |
71316 KB |
Output is correct |
44 |
Correct |
96 ms |
71356 KB |
Output is correct |
45 |
Correct |
127 ms |
74624 KB |
Output is correct |
46 |
Correct |
78 ms |
78360 KB |
Output is correct |
47 |
Correct |
169 ms |
118292 KB |
Output is correct |
48 |
Correct |
197 ms |
133148 KB |
Output is correct |
49 |
Correct |
186 ms |
132048 KB |
Output is correct |
50 |
Correct |
168 ms |
118272 KB |
Output is correct |
51 |
Correct |
171 ms |
118188 KB |
Output is correct |
52 |
Correct |
230 ms |
118308 KB |
Output is correct |
53 |
Correct |
218 ms |
117264 KB |
Output is correct |
54 |
Correct |
140 ms |
117756 KB |
Output is correct |
55 |
Correct |
1663 ms |
24500 KB |
Output is correct |
56 |
Correct |
1984 ms |
24488 KB |
Output is correct |
57 |
Correct |
69 ms |
71276 KB |
Output is correct |
58 |
Execution timed out |
3062 ms |
30736 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |