This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |