#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define sze(x) (int)(x.size())
typedef long long ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;
const ll maxn = (1 << 20) + 17 , inf = 2e8;
int n , s , t;
vector<pii> adj[maxn];
int dis[22][maxn];
int bfs[maxn] , sz = 0;
int g[maxn] , k[22] , d[22] , e[22] , f[22][22] , dp[maxn][22] , ind[22];
void rBFS(int r , int h){
sz = 0;
dis[h][r] = 0;
g[r] = 0;
bfs[sz++] = r;
int x = 0;
while(x < sz){
int v = bfs[x++];
for(auto p : adj[v]){
int i = p.first , t = p.second;
if(dis[h][i] < dis[h][v] + 1) continue;
g[i] = g[v];
if(t != -1){
g[i] ^= (1 << t);
d[t] = v;
}
dis[h][i] = dis[h][v] + 1;
bfs[sz++] = i;
}
}
return;
}
void BFS(int r , int h){
sz = 0;
dis[h][r] = 0;
g[r] = 0;
bfs[sz++] = r;
int x = 0;
while(x < sz){
int v = bfs[x++];
for(auto p : adj[v]){
int i = p.first , t = p.second;
if(dis[h][i] < dis[h][v] + 1) continue;
dis[h][i] = dis[h][v] + 1;
bfs[sz++] = i;
}
}
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
memset(dp , 63 , sizeof(dp));
memset(dis , 63 , sizeof(dis));
cin>>n>>s>>t; s--; t--;
int m = 0;
for(int i = 1 ; i < n ; i++){
int 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});
}
}
rBFS(s , m);
if(g[t] == 0){
cout<<dis[m][t]<<'\n';
return 0;
}
for(int j = 0 ; j < m ; j++){
e[j] = g[k[j]] | g[d[j]];
BFS(d[j] , j);
if(e[j] == 0){
dp[(1 << j)][j] = dis[m][k[j]] + dis[j][k[j]];
}
}
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < m ; j++){
f[i][j] = dis[i][k[j]] + dis[j][k[j]];
}
}
int lm = (1 << m);
for(int mask = 3 ; mask < lm ; mask++){
int bp = 0;
for(int j = 0 ; j < m ; j++){
if(mask & (1 << j)) ind[bp++] = j;
}
if(bp == 1) continue;
for(int jj = 0 ; jj < bp ; jj++){
int j = ind[jj];
if((e[j] & mask) != e[j]) continue;
int h = inf , msk = mask ^ (1 << j);
for(int ii = 0 ; ii < bp ; ii++){
int i = ind[ii];
if(i == j) continue;
h = min(h , dp[msk][i] + f[i][j]);
}
dp[mask][j] = h;
}
}
int ans = inf;
for(int mask = 1 ; mask < lm ; mask++){
if((g[t] & mask) != g[t]) continue;
int h = inf;
for(int i = 0 ; i < m ; i++){
if(!(mask & (1 << i))) continue;
// cout<<mask<<' '<<i<<'\n';
h = min(h , dp[mask][i] + dis[i][t]);
}
ans = min(ans , h);
}
cout<<(ans == inf ? -1 : ans)<<'\n';
return 0;
}
Compilation message
Main.cpp: In function 'void BFS(int, int)':
Main.cpp:52:22: warning: unused variable 't' [-Wunused-variable]
52 | int i = p.first , t = p.second;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
205520 KB |
Output is correct |
2 |
Correct |
81 ms |
205432 KB |
Output is correct |
3 |
Correct |
152 ms |
210008 KB |
Output is correct |
4 |
Correct |
146 ms |
210048 KB |
Output is correct |
5 |
Correct |
137 ms |
210044 KB |
Output is correct |
6 |
Correct |
145 ms |
210048 KB |
Output is correct |
7 |
Correct |
133 ms |
210056 KB |
Output is correct |
8 |
Correct |
145 ms |
209992 KB |
Output is correct |
9 |
Correct |
149 ms |
210056 KB |
Output is correct |
10 |
Correct |
140 ms |
209964 KB |
Output is correct |
11 |
Correct |
138 ms |
209944 KB |
Output is correct |
12 |
Correct |
130 ms |
209368 KB |
Output is correct |
13 |
Correct |
134 ms |
209432 KB |
Output is correct |
14 |
Correct |
149 ms |
209376 KB |
Output is correct |
15 |
Correct |
134 ms |
209328 KB |
Output is correct |
16 |
Correct |
131 ms |
209392 KB |
Output is correct |
17 |
Correct |
142 ms |
209356 KB |
Output is correct |
18 |
Correct |
139 ms |
209356 KB |
Output is correct |
19 |
Correct |
122 ms |
209356 KB |
Output is correct |
20 |
Correct |
122 ms |
209440 KB |
Output is correct |
21 |
Correct |
124 ms |
209424 KB |
Output is correct |
22 |
Correct |
75 ms |
205420 KB |
Output is correct |
23 |
Correct |
75 ms |
205492 KB |
Output is correct |
24 |
Correct |
79 ms |
205496 KB |
Output is correct |
25 |
Correct |
76 ms |
205460 KB |
Output is correct |
26 |
Correct |
77 ms |
205480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
205420 KB |
Output is correct |
2 |
Correct |
74 ms |
205480 KB |
Output is correct |
3 |
Correct |
76 ms |
205516 KB |
Output is correct |
4 |
Correct |
77 ms |
205600 KB |
Output is correct |
5 |
Correct |
157 ms |
205780 KB |
Output is correct |
6 |
Correct |
152 ms |
205896 KB |
Output is correct |
7 |
Correct |
166 ms |
205844 KB |
Output is correct |
8 |
Correct |
149 ms |
205756 KB |
Output is correct |
9 |
Correct |
165 ms |
205852 KB |
Output is correct |
10 |
Correct |
123 ms |
205832 KB |
Output is correct |
11 |
Correct |
132 ms |
205796 KB |
Output is correct |
12 |
Correct |
126 ms |
205772 KB |
Output is correct |
13 |
Correct |
127 ms |
205772 KB |
Output is correct |
14 |
Correct |
126 ms |
205752 KB |
Output is correct |
15 |
Correct |
147 ms |
205784 KB |
Output is correct |
16 |
Correct |
161 ms |
205768 KB |
Output is correct |
17 |
Correct |
153 ms |
205788 KB |
Output is correct |
18 |
Correct |
123 ms |
205760 KB |
Output is correct |
19 |
Correct |
125 ms |
205796 KB |
Output is correct |
20 |
Correct |
147 ms |
205716 KB |
Output is correct |
21 |
Correct |
125 ms |
205804 KB |
Output is correct |
22 |
Correct |
124 ms |
205812 KB |
Output is correct |
23 |
Correct |
133 ms |
205744 KB |
Output is correct |
24 |
Correct |
126 ms |
205800 KB |
Output is correct |
25 |
Correct |
381 ms |
205516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
205520 KB |
Output is correct |
2 |
Correct |
81 ms |
205432 KB |
Output is correct |
3 |
Correct |
152 ms |
210008 KB |
Output is correct |
4 |
Correct |
146 ms |
210048 KB |
Output is correct |
5 |
Correct |
137 ms |
210044 KB |
Output is correct |
6 |
Correct |
145 ms |
210048 KB |
Output is correct |
7 |
Correct |
133 ms |
210056 KB |
Output is correct |
8 |
Correct |
145 ms |
209992 KB |
Output is correct |
9 |
Correct |
149 ms |
210056 KB |
Output is correct |
10 |
Correct |
140 ms |
209964 KB |
Output is correct |
11 |
Correct |
138 ms |
209944 KB |
Output is correct |
12 |
Correct |
130 ms |
209368 KB |
Output is correct |
13 |
Correct |
134 ms |
209432 KB |
Output is correct |
14 |
Correct |
149 ms |
209376 KB |
Output is correct |
15 |
Correct |
134 ms |
209328 KB |
Output is correct |
16 |
Correct |
131 ms |
209392 KB |
Output is correct |
17 |
Correct |
142 ms |
209356 KB |
Output is correct |
18 |
Correct |
139 ms |
209356 KB |
Output is correct |
19 |
Correct |
122 ms |
209356 KB |
Output is correct |
20 |
Correct |
122 ms |
209440 KB |
Output is correct |
21 |
Correct |
124 ms |
209424 KB |
Output is correct |
22 |
Correct |
75 ms |
205420 KB |
Output is correct |
23 |
Correct |
75 ms |
205492 KB |
Output is correct |
24 |
Correct |
79 ms |
205496 KB |
Output is correct |
25 |
Correct |
76 ms |
205460 KB |
Output is correct |
26 |
Correct |
77 ms |
205480 KB |
Output is correct |
27 |
Correct |
86 ms |
205420 KB |
Output is correct |
28 |
Correct |
74 ms |
205480 KB |
Output is correct |
29 |
Correct |
76 ms |
205516 KB |
Output is correct |
30 |
Correct |
77 ms |
205600 KB |
Output is correct |
31 |
Correct |
157 ms |
205780 KB |
Output is correct |
32 |
Correct |
152 ms |
205896 KB |
Output is correct |
33 |
Correct |
166 ms |
205844 KB |
Output is correct |
34 |
Correct |
149 ms |
205756 KB |
Output is correct |
35 |
Correct |
165 ms |
205852 KB |
Output is correct |
36 |
Correct |
123 ms |
205832 KB |
Output is correct |
37 |
Correct |
132 ms |
205796 KB |
Output is correct |
38 |
Correct |
126 ms |
205772 KB |
Output is correct |
39 |
Correct |
127 ms |
205772 KB |
Output is correct |
40 |
Correct |
126 ms |
205752 KB |
Output is correct |
41 |
Correct |
147 ms |
205784 KB |
Output is correct |
42 |
Correct |
161 ms |
205768 KB |
Output is correct |
43 |
Correct |
153 ms |
205788 KB |
Output is correct |
44 |
Correct |
123 ms |
205760 KB |
Output is correct |
45 |
Correct |
125 ms |
205796 KB |
Output is correct |
46 |
Correct |
147 ms |
205716 KB |
Output is correct |
47 |
Correct |
125 ms |
205804 KB |
Output is correct |
48 |
Correct |
124 ms |
205812 KB |
Output is correct |
49 |
Correct |
133 ms |
205744 KB |
Output is correct |
50 |
Correct |
126 ms |
205800 KB |
Output is correct |
51 |
Correct |
381 ms |
205516 KB |
Output is correct |
52 |
Correct |
1855 ms |
250744 KB |
Output is correct |
53 |
Execution timed out |
2098 ms |
250628 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |