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;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
int n, k;
vector<pii> g[200010];
bool v[200010];
int mn[200010][101];
int res=1e9;
void dfs(int s){
v[s]=true;
mn[s][0]=1;
for(auto pr:g[s]){
int u=pr.ft, d=pr.sc;
if(!v[u]){
dfs(u);
for(int sum=0; (sum+d)<=( k ); sum++){
if( (mn[s][k-(sum+d) ]>0) && (mn[u][sum]>0) ){
res=min(res, mn[s][k-(sum+d)]+mn[u][sum]);
}
}
for(int sum=0; ( (sum+d)<=k ); sum++){
if( (mn[u][sum]>0) ){
if(mn[s][sum+d]==0){
mn[s][sum+d]=mn[u][sum]+1;
}
else{
mn[s][sum+d]=min(mn[s][sum+d], mn[u][sum]+1);
}
}
}
}
}
if(mn[s][k]>0){
res=min(res, mn[s][k]);
}
return;
}
int dfs2(int s, int l, int h){
int res=1e9;
if(l>k){
return res;
}
if(l==k){
return h;
}
v[s]=true;
for(pii pr:g[s]){
int u=pr.ft, d=pr.sc;
if(!v[u]){
res=min(dfs2(u, l+d, h+1), res);
}
}
v[s]=false;
return res;
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N; k=K;
for(int i=0; i<n; i++){
int u=H[i][0]+1, v=H[i][1]+1, d=L[i];
g[u].pb({v, d}); g[v].pb({u, d});
}
if(k<=1000){ dfs(1);}
else{
for(int i=1; i<=n; i++){
res=min(res, dfs2(i, 0, 1));
}
}
if(res==(1e9)){
return -1;
}
return res-1;
}
# | 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... |