#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr first
#define sc second
const int mxn=1e6+5,sz=(1<<22);
int n,dis[40][40],par[mxn][22],c[mxn],key[25],door[25],cnt=0,d[mxn];
ll dp[sz][25],ans=1e9;
vector<pair<int,int>>v[mxn];
void dfs(int z,int f=0){
for(int i=1;i<=20;i++)
par[z][i]=par[par[z][i-1]][i-1];
for(auto i:v[z]){
if(par[z][0]!=i.fr){
par[i.fr][0]=z;
c[i.fr]=c[z];
d[i.fr]=d[z]+1;
if(i.sc){
door[++cnt]=z;
key[cnt]=i.sc;
c[i.fr]|=(1<<(cnt-1));
}
dfs(i.fr);
}
}
}
int lca(int x,int y){
if(x==0 || y==0)return 0;
if(d[x]<d[y])swap(x,y);
int res=0;
for(int i=20;i>=0;i--){
if((1<<i)<=d[x]-d[y])x=par[x][i],res+=(1<<i);
}
if(x==y)return res;
for(int i=20;i>=0;i--){
if(par[x][i]!=par[y][i])x=par[x][i],y=par[y][i],res+=(1<<(i+1));
}
return res+2;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
cin>>door[21]>>key[21];
for(int i=1;i<n;i++){
int x,y,w;
cin>>x>>y>>w;
v[x].push_back({y,w});
v[y].push_back({x,w});
}
dfs(door[21]);
for(int i=1;i<=21;i++){
for(int j=1;j<=21;j++){
dis[i][j]=lca(door[i],key[j]);
}
}
for(int i=0;i<cnt;i++){
if(!c[door[i+1]] && !c[key[i+1]])
dp[(1<<i)][i]=dis[21][i+1]+dis[i+1][i+1];
else
dp[(1<<i)][i]=1e9;
}
for(int i=1;i<(1<<cnt);i++){
for(int j=0;j<cnt;j++){
if(__builtin_popcount(i)!=1)dp[i][j]=1e9;
if((i&(1<<j)) && (i&c[door[j+1]])==c[door[j+1]] && (i&c[key[j+1]])==c[key[j+1]]) {
for(int w=0;w<cnt;w++){
if(i&(1<<w)){
if(w!=j && dp[i-(1<<j)][w]!=1e9){
dp[i][j]=min(dp[i][j],dis[j+1][j+1]+dis[w+1][j+1]+dp[i-(1<<j)][w]);
}
}
}
if((i&(c[key[21]]))==c[key[21]]){
ans=min(ans,dp[i][j]+dis[j+1][21]);
}
}
}
}
if(c[key[21]]==0)ans=dis[21][21];
if(ans==1e9)ans=-1;
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27224 KB |
Output is correct |
3 |
Correct |
55 ms |
41480 KB |
Output is correct |
4 |
Correct |
53 ms |
41304 KB |
Output is correct |
5 |
Correct |
57 ms |
41564 KB |
Output is correct |
6 |
Correct |
63 ms |
41552 KB |
Output is correct |
7 |
Correct |
54 ms |
41552 KB |
Output is correct |
8 |
Correct |
62 ms |
41436 KB |
Output is correct |
9 |
Correct |
80 ms |
41552 KB |
Output is correct |
10 |
Correct |
61 ms |
41512 KB |
Output is correct |
11 |
Correct |
68 ms |
41560 KB |
Output is correct |
12 |
Correct |
76 ms |
43008 KB |
Output is correct |
13 |
Correct |
57 ms |
42572 KB |
Output is correct |
14 |
Correct |
57 ms |
41980 KB |
Output is correct |
15 |
Correct |
59 ms |
43092 KB |
Output is correct |
16 |
Correct |
61 ms |
43884 KB |
Output is correct |
17 |
Correct |
75 ms |
45140 KB |
Output is correct |
18 |
Correct |
74 ms |
45516 KB |
Output is correct |
19 |
Correct |
86 ms |
49736 KB |
Output is correct |
20 |
Correct |
76 ms |
49492 KB |
Output is correct |
21 |
Correct |
77 ms |
49252 KB |
Output is correct |
22 |
Correct |
7 ms |
27228 KB |
Output is correct |
23 |
Correct |
6 ms |
27228 KB |
Output is correct |
24 |
Correct |
6 ms |
27228 KB |
Output is correct |
25 |
Correct |
6 ms |
27228 KB |
Output is correct |
26 |
Correct |
6 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27224 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27268 KB |
Output is correct |
5 |
Correct |
158 ms |
130780 KB |
Output is correct |
6 |
Correct |
133 ms |
130656 KB |
Output is correct |
7 |
Correct |
177 ms |
130672 KB |
Output is correct |
8 |
Correct |
146 ms |
130788 KB |
Output is correct |
9 |
Correct |
159 ms |
130708 KB |
Output is correct |
10 |
Correct |
101 ms |
130764 KB |
Output is correct |
11 |
Correct |
102 ms |
130644 KB |
Output is correct |
12 |
Correct |
102 ms |
130800 KB |
Output is correct |
13 |
Correct |
106 ms |
130720 KB |
Output is correct |
14 |
Correct |
103 ms |
130644 KB |
Output is correct |
15 |
Correct |
134 ms |
130752 KB |
Output is correct |
16 |
Correct |
179 ms |
130740 KB |
Output is correct |
17 |
Correct |
154 ms |
130800 KB |
Output is correct |
18 |
Correct |
103 ms |
130772 KB |
Output is correct |
19 |
Correct |
102 ms |
130812 KB |
Output is correct |
20 |
Correct |
100 ms |
130900 KB |
Output is correct |
21 |
Correct |
100 ms |
131012 KB |
Output is correct |
22 |
Correct |
106 ms |
131180 KB |
Output is correct |
23 |
Correct |
105 ms |
131268 KB |
Output is correct |
24 |
Correct |
100 ms |
131176 KB |
Output is correct |
25 |
Correct |
800 ms |
232408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27224 KB |
Output is correct |
2 |
Correct |
6 ms |
27224 KB |
Output is correct |
3 |
Correct |
55 ms |
41480 KB |
Output is correct |
4 |
Correct |
53 ms |
41304 KB |
Output is correct |
5 |
Correct |
57 ms |
41564 KB |
Output is correct |
6 |
Correct |
63 ms |
41552 KB |
Output is correct |
7 |
Correct |
54 ms |
41552 KB |
Output is correct |
8 |
Correct |
62 ms |
41436 KB |
Output is correct |
9 |
Correct |
80 ms |
41552 KB |
Output is correct |
10 |
Correct |
61 ms |
41512 KB |
Output is correct |
11 |
Correct |
68 ms |
41560 KB |
Output is correct |
12 |
Correct |
76 ms |
43008 KB |
Output is correct |
13 |
Correct |
57 ms |
42572 KB |
Output is correct |
14 |
Correct |
57 ms |
41980 KB |
Output is correct |
15 |
Correct |
59 ms |
43092 KB |
Output is correct |
16 |
Correct |
61 ms |
43884 KB |
Output is correct |
17 |
Correct |
75 ms |
45140 KB |
Output is correct |
18 |
Correct |
74 ms |
45516 KB |
Output is correct |
19 |
Correct |
86 ms |
49736 KB |
Output is correct |
20 |
Correct |
76 ms |
49492 KB |
Output is correct |
21 |
Correct |
77 ms |
49252 KB |
Output is correct |
22 |
Correct |
7 ms |
27228 KB |
Output is correct |
23 |
Correct |
6 ms |
27228 KB |
Output is correct |
24 |
Correct |
6 ms |
27228 KB |
Output is correct |
25 |
Correct |
6 ms |
27228 KB |
Output is correct |
26 |
Correct |
6 ms |
27228 KB |
Output is correct |
27 |
Correct |
5 ms |
27224 KB |
Output is correct |
28 |
Correct |
6 ms |
27224 KB |
Output is correct |
29 |
Correct |
6 ms |
27228 KB |
Output is correct |
30 |
Correct |
6 ms |
27268 KB |
Output is correct |
31 |
Correct |
158 ms |
130780 KB |
Output is correct |
32 |
Correct |
133 ms |
130656 KB |
Output is correct |
33 |
Correct |
177 ms |
130672 KB |
Output is correct |
34 |
Correct |
146 ms |
130788 KB |
Output is correct |
35 |
Correct |
159 ms |
130708 KB |
Output is correct |
36 |
Correct |
101 ms |
130764 KB |
Output is correct |
37 |
Correct |
102 ms |
130644 KB |
Output is correct |
38 |
Correct |
102 ms |
130800 KB |
Output is correct |
39 |
Correct |
106 ms |
130720 KB |
Output is correct |
40 |
Correct |
103 ms |
130644 KB |
Output is correct |
41 |
Correct |
134 ms |
130752 KB |
Output is correct |
42 |
Correct |
179 ms |
130740 KB |
Output is correct |
43 |
Correct |
154 ms |
130800 KB |
Output is correct |
44 |
Correct |
103 ms |
130772 KB |
Output is correct |
45 |
Correct |
102 ms |
130812 KB |
Output is correct |
46 |
Correct |
100 ms |
130900 KB |
Output is correct |
47 |
Correct |
100 ms |
131012 KB |
Output is correct |
48 |
Correct |
106 ms |
131180 KB |
Output is correct |
49 |
Correct |
105 ms |
131268 KB |
Output is correct |
50 |
Correct |
100 ms |
131176 KB |
Output is correct |
51 |
Correct |
800 ms |
232408 KB |
Output is correct |
52 |
Correct |
1098 ms |
178448 KB |
Output is correct |
53 |
Correct |
1113 ms |
179012 KB |
Output is correct |
54 |
Correct |
1010 ms |
175996 KB |
Output is correct |
55 |
Correct |
1063 ms |
175804 KB |
Output is correct |
56 |
Correct |
1075 ms |
176740 KB |
Output is correct |
57 |
Correct |
1034 ms |
178400 KB |
Output is correct |
58 |
Correct |
6 ms |
27312 KB |
Output is correct |
59 |
Correct |
6 ms |
27228 KB |
Output is correct |
60 |
Correct |
6 ms |
27228 KB |
Output is correct |
61 |
Correct |
1059 ms |
178536 KB |
Output is correct |
62 |
Correct |
1045 ms |
200120 KB |
Output is correct |
63 |
Correct |
1053 ms |
187700 KB |
Output is correct |
64 |
Correct |
97 ms |
45008 KB |
Output is correct |
65 |
Correct |
72 ms |
45084 KB |
Output is correct |
66 |
Correct |
1033 ms |
178136 KB |
Output is correct |
67 |
Correct |
1037 ms |
178644 KB |
Output is correct |
68 |
Correct |
973 ms |
276096 KB |
Output is correct |
69 |
Correct |
970 ms |
276780 KB |
Output is correct |
70 |
Correct |
963 ms |
276052 KB |
Output is correct |
71 |
Correct |
1016 ms |
276008 KB |
Output is correct |
72 |
Correct |
971 ms |
275712 KB |
Output is correct |
73 |
Correct |
892 ms |
277980 KB |
Output is correct |
74 |
Correct |
926 ms |
278368 KB |
Output is correct |
75 |
Correct |
977 ms |
278428 KB |
Output is correct |
76 |
Correct |
926 ms |
279032 KB |
Output is correct |
77 |
Correct |
915 ms |
277228 KB |
Output is correct |
78 |
Correct |
1062 ms |
280684 KB |
Output is correct |
79 |
Correct |
1052 ms |
280236 KB |
Output is correct |
80 |
Correct |
1129 ms |
279180 KB |
Output is correct |
81 |
Correct |
1148 ms |
295356 KB |
Output is correct |
82 |
Correct |
1186 ms |
296916 KB |
Output is correct |
83 |
Correct |
1134 ms |
296836 KB |
Output is correct |
84 |
Correct |
1104 ms |
308144 KB |
Output is correct |
85 |
Correct |
1256 ms |
345140 KB |
Output is correct |
86 |
Correct |
1368 ms |
344708 KB |
Output is correct |
87 |
Correct |
1390 ms |
344796 KB |
Output is correct |