#include "race.h"
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
vector<pair<int, int>> adj[300005];
int dp[300005][105];
void dfs(int node, int parent, int K) {
dp[node][0] = 0; // Distance to self with 0 jumps is 0
for (auto &edge : adj[node]) {
int next = edge.first;
int length = edge.second;
if (next == parent) continue;
dfs(next, node, K);
for (int j = K; j >= 0; --j) {
if (dp[node][j] == INF) continue;
for (int l = 1; l + j <= K; ++l) {
if (dp[next][l] != INF) {
dp[node][j + l] = min(dp[node][j + l], dp[node][j] + dp[next][l] + length);
}
}
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j <= K; ++j) {
dp[i][j] = INF;
}
}
for (int i = 0; i < N - 1; ++i) {
int u = H[i][0];
int v = H[i][1];
int len = L[i];
adj[u].push_back({v, len});
adj[v].push_back({u, len});
}
dfs(0, -1, K);
int answer = INF;
for (int i = 0; i < N; ++i) {
answer = min(answer, dp[i][K]);
}
return (answer == INF) ? -1 : answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
13144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
13144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
13144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
13144 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |