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;
#warning notusing64bitint
using ll = int;
using pi = pair<int, int>;
using pl = pair<int, int>;
using vi = vector<int>;
using vl = vector<int>;
using vpi = vector<pair<int, int>>;
using vpl = vector<pair<int, int>>;
#define fur(i, a, b) for(ll i = a; i <= (ll)b; ++i)
#define ruf(i, a, b) for(ll i = a; i >= (ll)b; --i)
#define fr first
#define sc second
#define mp make_pair
#define pb emplace_back
#define nl "\n"
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const ll mxn = 2e5L + 1;
ll k;
vpl adj[mxn];
ll dp[mxn][101];
ll sm[mxn][101];
int ans = mxn;
void dfs(ll cur, ll src) {
for(auto [ch, wt] : adj[cur]) {
if(ch != src) {
dfs(ch, cur);
fur(j, 1, k) {
if(j - wt >= 0) {
if(dp[cur][j] > 1 + dp[ch][j - wt]) {
sm[cur][j] = dp[cur][j];
dp[cur][j] = 1 + dp[ch][j - wt];
} else if(sm[cur][j] > 1 + dp[ch][j - wt]) {
sm[cur][j] = 1 + dp[ch][j - wt];
}
}
}
}
}
dp[cur][0] = 0;
}
void solve(ll cur, ll src) {
ans = min(ans, dp[cur][k]);
for(auto [ch, wt] : adj[cur]) {
if(ch != src) {
fur(j, 1, k - 1) {
if(j - wt < 0)
continue;
ll here = dp[ch][j - wt] + 1;
ll there = dp[cur][k - j];
if(k - j - wt >= 0 && dp[ch][k - j - wt] + 1 == there)
there = sm[cur][k - j];
ans = min(ans, here + there);
}
solve(ch, cur);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
fur(i, 0, N - 2) {
++H[i][0]; ++H[i][1];
adj[H[i][0]].pb(H[i][1], L[i]);
adj[H[i][1]].pb(H[i][0], L[i]);
}
fur(i, 0, N) {
fur(j, 0, k) {
dp[i][j] = sm[i][j] = mxn;
}
}
dfs(1, -1);
// fur(i, 1, N) {
// fur(j, 0, k) {
// cout << dp[i][j] << ' ';
// }
// cout << nl;
// }
// cout << nl;
// fur(i, 1, N) {
// fur(j, 0, k) {
// cout << sm[i][j] << ' ';
// }
// cout << nl;
// }
// cout << nl;
solve(1, -1);
if(ans == mxn)
ans = -1;
return ans;
}
Compilation message (stderr)
race.cpp:4:2: warning: #warning notusing64bitint [-Wcpp]
4 | #warning notusing64bitint
| ^~~~~~~
# | 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... |