# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
418573 | snasibov05 | Race (IOI11_race) | C++14 | 0 ms | 0 KiB |
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 <vector>
using namespace std;
#define oo 1000000000
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
vector<vector<pii>> ed;
vector<vector<vector<int>>> dp;
void dfs(int cur, int pr, int k){
dp[cur][0][0] = dp[cur][0][1] = 0;
for (auto [to, dist] : ed[cur]){
if (to == pr) continue;
dfs(to, cur, k);
for (int i = 0; i <= k; ++i) {
dp[cur][i][0] = min({dp[cur][i][0], dp[to][i][0], dp[to][i][1]});
if (i - dist >= 0) dp[cur][i][1] = min(dp[cur][i][1], dp[to][i - dist][1] + 1);
}
}
}
int best_path(int n, int k, int h[][2], int l[]){
ed.resize(n);
dp.resize(n);
dp.assign(n, vector<vector<int>>(k+1, vector<int>(2, oo)));
for (int i = 0; i < n - 1; ++i) {
ed[h[i][0]].pb({h[i][1], l[i]});
ed[h[i][1]].pb({h[i][0], l[i]});
}
dfs(0, -1, k);
if (dp[0][k][0] == oo) dp[0][k][0] = -1;
return dp[0][k][0];
}