#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e6;
const int M=20;
const int INF=1e9+7;
vector<pair<int,int>> e[N+10];
int dep[N+10];
int jmp[N+10][M+2];
int keys[N+10];
int doors[M+10];
int dp[(1<<M)+10][M+2];
int dk[M+2][M+2];
int wh_k[M+2];
int wh_d[M+2];
void find_doors(int x,int p,int msk,int t)
{
if(x==t)
doors[M]=msk;
dep[x]=dep[p]+1;
jmp[x][0]=p;
for(int i=1;jmp[x][i-1]!=-0;i++)
jmp[x][i]=jmp[jmp[x][i-1]][i-1];
for(int i=0;i<M;i++)
{
if(keys[x]&(1<<i))
doors[i]|=msk;
}
for(auto [v,c]:e[x])
{
if(v==p)
continue;
for(int i=0;i<M;i++)
{
if(c&(1<<i))
{
wh_d[i]=x;
doors[i]|=msk;
}
}
find_doors(v,x,msk|c,t);
}
return;
}
int dis(int x,int y)
{
int ans=0;
if(dep[x]<dep[y])
swap(x,y);
for(int i=M;i>=0;i--)
{
if(dep[jmp[x][i]]>=dep[y])
{
x=jmp[x][i];
ans+=(1<<i);
}
}
if(x==y)
return ans;
for(int i=M;i>=0;i--)
{
if(jmp[x][i]!=jmp[y][i])
{
x=jmp[x][i];
y=jmp[y][i];
ans+=(2<<i);
}
}
return ans+2;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m=0;
int s,t;
int ans=INF;
cin>>n;
cin>>s>>t;
for(int i=1;i<n;i++)
{
int a,b,c;
cin>>a>>b>>c;
if(c!=0)
{
wh_k[m]=c;
keys[c]|=(1<<m);
c=(1<<m);
m++;
}
e[a].emplace_back(b,c);
e[b].emplace_back(a,c);
}
find_doors(s,0,0,t);
wh_k[m]=t;
wh_d[m]=s;
for(int i=0;i<=m;i++)
{
for(int j=0;j<=m;j++)
dk[i][j]=dis(wh_d[i],wh_k[j]);
}
for(int i=0;i<m;i++)
dp[0][i]=INF;
for(int msk=1;msk<(1<<m);msk++)
{
for(int i=0;i<=m;i++)
{
dp[msk][i]=INF;
if((msk&(1<<i))==0 || ((msk^(1<<i))|doors[i])!=(msk^(1<<i)))
continue;
for(int j=0;j<=m;j++)
dp[msk][i]=min(dp[msk][i],dp[msk^(1<<i)][j]+dk[j][i]);
dp[msk][i]+=dk[i][i];
}
if((msk|doors[M])==msk)
{
for(int i=0;i<=m;i++)
ans=min(ans,dp[msk][i]+dk[i][m]);
}
}
cout<<(ans==INF ? -1:ans)<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29276 KB |
Output is correct |
2 |
Correct |
8 ms |
29276 KB |
Output is correct |
3 |
Correct |
69 ms |
47184 KB |
Output is correct |
4 |
Correct |
60 ms |
47184 KB |
Output is correct |
5 |
Correct |
71 ms |
47440 KB |
Output is correct |
6 |
Correct |
61 ms |
47400 KB |
Output is correct |
7 |
Correct |
61 ms |
47440 KB |
Output is correct |
8 |
Correct |
61 ms |
47440 KB |
Output is correct |
9 |
Correct |
70 ms |
47652 KB |
Output is correct |
10 |
Correct |
60 ms |
47444 KB |
Output is correct |
11 |
Correct |
60 ms |
47444 KB |
Output is correct |
12 |
Correct |
61 ms |
48824 KB |
Output is correct |
13 |
Correct |
62 ms |
48292 KB |
Output is correct |
14 |
Correct |
67 ms |
48088 KB |
Output is correct |
15 |
Correct |
62 ms |
48980 KB |
Output is correct |
16 |
Correct |
71 ms |
49748 KB |
Output is correct |
17 |
Correct |
68 ms |
49764 KB |
Output is correct |
18 |
Correct |
70 ms |
50056 KB |
Output is correct |
19 |
Correct |
67 ms |
54100 KB |
Output is correct |
20 |
Correct |
92 ms |
54096 KB |
Output is correct |
21 |
Correct |
79 ms |
53884 KB |
Output is correct |
22 |
Correct |
8 ms |
29276 KB |
Output is correct |
23 |
Correct |
9 ms |
29276 KB |
Output is correct |
24 |
Correct |
9 ms |
29276 KB |
Output is correct |
25 |
Correct |
10 ms |
29276 KB |
Output is correct |
26 |
Incorrect |
9 ms |
29272 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29276 KB |
Output is correct |
2 |
Correct |
8 ms |
29276 KB |
Output is correct |
3 |
Correct |
9 ms |
29532 KB |
Output is correct |
4 |
Correct |
9 ms |
29528 KB |
Output is correct |
5 |
Correct |
80 ms |
77132 KB |
Output is correct |
6 |
Correct |
72 ms |
77140 KB |
Output is correct |
7 |
Correct |
100 ms |
77136 KB |
Output is correct |
8 |
Correct |
75 ms |
77148 KB |
Output is correct |
9 |
Correct |
79 ms |
77180 KB |
Output is correct |
10 |
Correct |
68 ms |
77176 KB |
Output is correct |
11 |
Correct |
62 ms |
77192 KB |
Output is correct |
12 |
Correct |
64 ms |
77136 KB |
Output is correct |
13 |
Correct |
61 ms |
77140 KB |
Output is correct |
14 |
Correct |
67 ms |
77180 KB |
Output is correct |
15 |
Correct |
76 ms |
77176 KB |
Output is correct |
16 |
Correct |
86 ms |
77648 KB |
Output is correct |
17 |
Correct |
79 ms |
76976 KB |
Output is correct |
18 |
Correct |
63 ms |
77332 KB |
Output is correct |
19 |
Correct |
63 ms |
77296 KB |
Output is correct |
20 |
Correct |
69 ms |
77304 KB |
Output is correct |
21 |
Correct |
68 ms |
77608 KB |
Output is correct |
22 |
Correct |
64 ms |
77648 KB |
Output is correct |
23 |
Correct |
62 ms |
77680 KB |
Output is correct |
24 |
Correct |
61 ms |
77648 KB |
Output is correct |
25 |
Correct |
294 ms |
118108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
29276 KB |
Output is correct |
2 |
Correct |
8 ms |
29276 KB |
Output is correct |
3 |
Correct |
69 ms |
47184 KB |
Output is correct |
4 |
Correct |
60 ms |
47184 KB |
Output is correct |
5 |
Correct |
71 ms |
47440 KB |
Output is correct |
6 |
Correct |
61 ms |
47400 KB |
Output is correct |
7 |
Correct |
61 ms |
47440 KB |
Output is correct |
8 |
Correct |
61 ms |
47440 KB |
Output is correct |
9 |
Correct |
70 ms |
47652 KB |
Output is correct |
10 |
Correct |
60 ms |
47444 KB |
Output is correct |
11 |
Correct |
60 ms |
47444 KB |
Output is correct |
12 |
Correct |
61 ms |
48824 KB |
Output is correct |
13 |
Correct |
62 ms |
48292 KB |
Output is correct |
14 |
Correct |
67 ms |
48088 KB |
Output is correct |
15 |
Correct |
62 ms |
48980 KB |
Output is correct |
16 |
Correct |
71 ms |
49748 KB |
Output is correct |
17 |
Correct |
68 ms |
49764 KB |
Output is correct |
18 |
Correct |
70 ms |
50056 KB |
Output is correct |
19 |
Correct |
67 ms |
54100 KB |
Output is correct |
20 |
Correct |
92 ms |
54096 KB |
Output is correct |
21 |
Correct |
79 ms |
53884 KB |
Output is correct |
22 |
Correct |
8 ms |
29276 KB |
Output is correct |
23 |
Correct |
9 ms |
29276 KB |
Output is correct |
24 |
Correct |
9 ms |
29276 KB |
Output is correct |
25 |
Correct |
10 ms |
29276 KB |
Output is correct |
26 |
Incorrect |
9 ms |
29272 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |