# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1000746 | nomuluun | Race (IOI11_race) | C++17 | 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<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N=4, K=3;
ll H[200005][2];
ll L[200005];
vector<pair<int,int>>v[200005];
ll mi=INT_MAX;
void dfs(ll q, ll dis, ll hi, ll vis[200005]){
if(dis==K){
mi=min(mi,hi);
}
for(int i=0; i<v[q].size(); i++){
int k=v[q][i].first;
ll cost=v[q][i].second;
if(!vis[k]){
vis[k]=1;
dfs(k,dis+cost,hi+1,vis);
}
}
}
int best_path(ll N, ll K, ll H[200005][2], ll L[200005]){
for(int i=0; i<N-1; i++){
ll vis[1000]={0};
vis[i]=1;
dfs(i,0,0,vis); //ehleh oroi distance highway
}
if(mi==INT_MAX)return -1;
else return mi;
}
int main(){
cin>>N>>K;
for(int i=0; i<N-1; i++){
cin>>H[i][0]>>H[i][1]>>L[i];
}
for(int i=0; i<N-1; i++){
v[H[i][0]].push_back({H[i][1], L[i]});
v[H[i][1]].push_back({H[i][0], L[i]});
}
cout<<best_path(N,K,H,L);
}