# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1100321 | vjudge1 | 경주 (Race) (IOI11_race) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <array>
using namespace std;
int k,dp[200000];
vector<pair<int,int>>v[200000];
int dfs(int x,int s)
{
if(s>k||dp[x])return -1;
else if(s==k)return 0;
int sm=-1;
dp[x]=1;
for(auto [i,vl]:v[x])
{
int sl=dfs(i,s+vl);
if(sl!=-1)
{
if(sm==-1)sm=sl+1;
else
{
sm=min(sl+1,sm);
}
}
}
dp[x]=0;
return sm;
}
int best_path(int n, int K, int h[][2], int l[])
{
k=K;
for(int x=0;x<n-1;x++)
{
v[h[x][0]].push_back({h[x][1],l[x]});
v[h[x][1]].push_back({h[x][0],l[x]});
}
if(n<=1000)
{
int ans=-1;
for(int x=0;x<n;x++)
{
int s=dfs(x,0);
if(s!=-1)
{
if(ans==-1)ans=s;
else
{
ans=min(ans,s);
}
}
}
return ans;
}
else
{
return 0;
}
}
int main()
{
int n,k;
cin>>n>>k;
int h[n][2],l[n];
for(int x=0;x<n-1;x++)
{
cin>>h[x][0]>>h[x][1]>>l[x];
}
cout<<best_path(n,k,h,l);
}