#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define sze(x) (ll)(x.size())
typedef long long ll;
typedef pair<ll , ll> pll;
const ll maxn = (1 << 20) + 17 , inf = 2e16;
ll n , s , t;
vector<pll> adj[maxn];
vector<ll> dis[maxn] , bfs;
ll g[maxn] , k[22] , d[22] , e[22] , dp[maxn][22];
void BFS(ll r , bool f = false){
bfs.clear();
dis[r].assign(n , inf);
dis[r][r] = 0;
g[r] = 0;
bfs.push_back(r);
ll x = 0;
while(x < sze(bfs)){
ll v = bfs[x++];
for(auto p : adj[v]){
ll i = p.first , t = p.second;
if(dis[r][i] < dis[r][v] + 1) continue;
if(f){
g[i] = g[v];
if(t != -1){
g[i] ^= (1 << t);
d[t] = v;
}
}
dis[r][i] = dis[r][v] + 1;
bfs.push_back(i);
}
}
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
memset(dp , 63 , sizeof(dp));
cin>>n>>s>>t; s--; t--;
ll m = 0;
for(ll i = 1 ; i < n ; i++){
ll v , u , w;
cin>>v>>u>>w; v--; u--; w--;
if(w != -1){
k[m] = w;
adj[v].push_back({u , m}); adj[u].push_back({v , m});
m++;
} else {
adj[v].push_back({u , w}); adj[u].push_back({v , w});
}
}
BFS(s , true);
if(g[t] == 0){
cout<<dis[s][t]<<'\n';
return 0;
}
for(ll j = 0 ; j < m ; j++){
e[j] = g[k[j]] | g[d[j]];
BFS(d[j]);
if(e[j] == 0){
dp[(1 << j)][j] = dis[s][k[j]] + dis[d[j]][k[j]];
}
}
ll lm = (1 << m);
for(ll mask = 1 ; mask < lm ; mask++){
for(ll j = 0 ; j < m ; j++){
if(mask & (1 << j)) continue;
if((e[j] & mask) != e[j]) continue;
ll h = inf;
for(ll i = 0 ; i < m ; i++){
if(!(mask & (1 << i))) continue;
h = min(h , dp[mask][i] + dis[d[i]][k[j]] + dis[d[j]][k[j]]);
}
dp[mask ^ (1 << j)][j] = min(dp[mask ^ (1 << j)][j] , h);
}
}
// cout<<dp[2][1]<<' '<<dp[6][2]<<' '<<dp[7][0]<<'\n';
// cout<<dis[d[0]][t]<<'\n';
ll ans = inf;
for(ll mask = 1 ; mask < lm ; mask++){
if((g[t] & mask) != g[t]) continue;
ll h = inf;
for(ll i = 0 ; i < m ; i++){
if(!(mask & (1 << i))) continue;
// cout<<mask<<' '<<i<<'\n';
h = min(h , dp[mask][i] + dis[d[i]][t]);
}
ans = min(ans , h);
}
cout<<(ans == inf ? -1 : ans)<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
230012 KB |
Output is correct |
2 |
Correct |
89 ms |
230048 KB |
Output is correct |
3 |
Correct |
174 ms |
244676 KB |
Output is correct |
4 |
Correct |
181 ms |
244560 KB |
Output is correct |
5 |
Correct |
168 ms |
244672 KB |
Output is correct |
6 |
Correct |
185 ms |
244520 KB |
Output is correct |
7 |
Correct |
191 ms |
244572 KB |
Output is correct |
8 |
Correct |
171 ms |
244512 KB |
Output is correct |
9 |
Correct |
173 ms |
244496 KB |
Output is correct |
10 |
Correct |
170 ms |
244604 KB |
Output is correct |
11 |
Correct |
170 ms |
244588 KB |
Output is correct |
12 |
Correct |
163 ms |
244048 KB |
Output is correct |
13 |
Correct |
167 ms |
244108 KB |
Output is correct |
14 |
Correct |
171 ms |
244288 KB |
Output is correct |
15 |
Correct |
163 ms |
244144 KB |
Output is correct |
16 |
Correct |
160 ms |
244168 KB |
Output is correct |
17 |
Correct |
153 ms |
244088 KB |
Output is correct |
18 |
Correct |
157 ms |
244172 KB |
Output is correct |
19 |
Correct |
149 ms |
244172 KB |
Output is correct |
20 |
Correct |
147 ms |
244140 KB |
Output is correct |
21 |
Correct |
144 ms |
244108 KB |
Output is correct |
22 |
Correct |
97 ms |
230124 KB |
Output is correct |
23 |
Correct |
92 ms |
230120 KB |
Output is correct |
24 |
Correct |
93 ms |
230056 KB |
Output is correct |
25 |
Correct |
91 ms |
230160 KB |
Output is correct |
26 |
Correct |
89 ms |
230072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
230092 KB |
Output is correct |
2 |
Correct |
89 ms |
230136 KB |
Output is correct |
3 |
Correct |
89 ms |
230112 KB |
Output is correct |
4 |
Correct |
88 ms |
230068 KB |
Output is correct |
5 |
Correct |
225 ms |
231680 KB |
Output is correct |
6 |
Correct |
196 ms |
231692 KB |
Output is correct |
7 |
Correct |
254 ms |
231800 KB |
Output is correct |
8 |
Correct |
203 ms |
231688 KB |
Output is correct |
9 |
Correct |
221 ms |
231800 KB |
Output is correct |
10 |
Correct |
155 ms |
231788 KB |
Output is correct |
11 |
Correct |
155 ms |
231672 KB |
Output is correct |
12 |
Correct |
159 ms |
231792 KB |
Output is correct |
13 |
Correct |
156 ms |
231792 KB |
Output is correct |
14 |
Correct |
155 ms |
231752 KB |
Output is correct |
15 |
Correct |
211 ms |
231740 KB |
Output is correct |
16 |
Correct |
233 ms |
231708 KB |
Output is correct |
17 |
Correct |
215 ms |
231708 KB |
Output is correct |
18 |
Correct |
154 ms |
231764 KB |
Output is correct |
19 |
Correct |
158 ms |
231680 KB |
Output is correct |
20 |
Correct |
158 ms |
231884 KB |
Output is correct |
21 |
Correct |
155 ms |
231688 KB |
Output is correct |
22 |
Correct |
155 ms |
231736 KB |
Output is correct |
23 |
Correct |
157 ms |
231756 KB |
Output is correct |
24 |
Correct |
155 ms |
231656 KB |
Output is correct |
25 |
Correct |
735 ms |
230200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
230012 KB |
Output is correct |
2 |
Correct |
89 ms |
230048 KB |
Output is correct |
3 |
Correct |
174 ms |
244676 KB |
Output is correct |
4 |
Correct |
181 ms |
244560 KB |
Output is correct |
5 |
Correct |
168 ms |
244672 KB |
Output is correct |
6 |
Correct |
185 ms |
244520 KB |
Output is correct |
7 |
Correct |
191 ms |
244572 KB |
Output is correct |
8 |
Correct |
171 ms |
244512 KB |
Output is correct |
9 |
Correct |
173 ms |
244496 KB |
Output is correct |
10 |
Correct |
170 ms |
244604 KB |
Output is correct |
11 |
Correct |
170 ms |
244588 KB |
Output is correct |
12 |
Correct |
163 ms |
244048 KB |
Output is correct |
13 |
Correct |
167 ms |
244108 KB |
Output is correct |
14 |
Correct |
171 ms |
244288 KB |
Output is correct |
15 |
Correct |
163 ms |
244144 KB |
Output is correct |
16 |
Correct |
160 ms |
244168 KB |
Output is correct |
17 |
Correct |
153 ms |
244088 KB |
Output is correct |
18 |
Correct |
157 ms |
244172 KB |
Output is correct |
19 |
Correct |
149 ms |
244172 KB |
Output is correct |
20 |
Correct |
147 ms |
244140 KB |
Output is correct |
21 |
Correct |
144 ms |
244108 KB |
Output is correct |
22 |
Correct |
97 ms |
230124 KB |
Output is correct |
23 |
Correct |
92 ms |
230120 KB |
Output is correct |
24 |
Correct |
93 ms |
230056 KB |
Output is correct |
25 |
Correct |
91 ms |
230160 KB |
Output is correct |
26 |
Correct |
89 ms |
230072 KB |
Output is correct |
27 |
Correct |
92 ms |
230092 KB |
Output is correct |
28 |
Correct |
89 ms |
230136 KB |
Output is correct |
29 |
Correct |
89 ms |
230112 KB |
Output is correct |
30 |
Correct |
88 ms |
230068 KB |
Output is correct |
31 |
Correct |
225 ms |
231680 KB |
Output is correct |
32 |
Correct |
196 ms |
231692 KB |
Output is correct |
33 |
Correct |
254 ms |
231800 KB |
Output is correct |
34 |
Correct |
203 ms |
231688 KB |
Output is correct |
35 |
Correct |
221 ms |
231800 KB |
Output is correct |
36 |
Correct |
155 ms |
231788 KB |
Output is correct |
37 |
Correct |
155 ms |
231672 KB |
Output is correct |
38 |
Correct |
159 ms |
231792 KB |
Output is correct |
39 |
Correct |
156 ms |
231792 KB |
Output is correct |
40 |
Correct |
155 ms |
231752 KB |
Output is correct |
41 |
Correct |
211 ms |
231740 KB |
Output is correct |
42 |
Correct |
233 ms |
231708 KB |
Output is correct |
43 |
Correct |
215 ms |
231708 KB |
Output is correct |
44 |
Correct |
154 ms |
231764 KB |
Output is correct |
45 |
Correct |
158 ms |
231680 KB |
Output is correct |
46 |
Correct |
158 ms |
231884 KB |
Output is correct |
47 |
Correct |
155 ms |
231688 KB |
Output is correct |
48 |
Correct |
155 ms |
231736 KB |
Output is correct |
49 |
Correct |
157 ms |
231756 KB |
Output is correct |
50 |
Correct |
155 ms |
231656 KB |
Output is correct |
51 |
Correct |
735 ms |
230200 KB |
Output is correct |
52 |
Correct |
1975 ms |
383204 KB |
Output is correct |
53 |
Execution timed out |
2098 ms |
399020 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |