#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define io(x) if (fopen(x".inp","r")) {freopen(x".inp","r",stdin),freopen(x".out","w",stdout);}
#define mem(c, x) memset(c, x, sizeof(c))
#define all(c) c.begin(), c.end()
#define bit(i,j) ((i >> j) & 1)
#define pb push_back
#define se second
#define fi first
#define el '\n'
using namespace std;
template<class T> bool maximize(T &a, const T &b) { return (a < b ? a = b, 1 : 0); }
template<class T> bool minimize(T &a, const T &b) { return (a > b ? a = b, 1 : 0); }
int dx[8] = {0, 1, 0,-1, 1, 1,-1,-1};
int dy[8] = {1, 0,-1, 0, 1,-1,-1, 1};
const int maxn = 2e5 + 9;
const int Inf = 2e9 + 7;
const ll Infll = 1e18 + 9;
const ll Mod = 1e9 + 7;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int res = 0x3f3f3f3f, dp[maxn][200], n, k;
vector<pii> adj[maxn];
void dfs(int u, int par) {
dp[u][0] = 0;
for (auto x : adj[u]) {
int v = x.fi, w = x.se;
if (v == par) continue;
dfs(v, u);
for (int kilo=0; k-kilo-w >= 0; kilo++) {
minimize(res, dp[u][k-kilo-w] + 1 + dp[v][kilo]);
}
for (int kilo=w; kilo<=k; kilo++) {
minimize(dp[u][kilo], dp[v][kilo-w] + 1);
}
}
}
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 x = H[i][0];
int y = H[i][1];
int w = L[i];
adj[x].pb({y, w});
adj[y].pb({x, w});
}
mem(dp, 0x3f);
dfs(1, 1);
if (res == 0x3f3f3f3f) return -1;
return res;
}
# | 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... |