#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
#define ff first
#define ss second
void dbg(){
cerr << endl;
}
template<typename Head, typename... Tail> void dbg(Head h, Tail... t){
cerr << " " << h << ",";
dbg(t...);
}
#define er(...) cerr << __LINE__ << " <" << #__VA_ARGS__ << ">:", dbg(__VA_ARGS__)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
// #ifndef ONLINE_JUDGE
// freopen("inp.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #endif
}
const ll mod = 1e9 + 7, maxn = 1e6 + 5, maxm = 20, inf = ll(1e9) + 5;
int val[maxn], ed[maxm], nxt[maxm], tmp;
vector<pp> adj[maxn];
void dfs(int r, int p){
val[r] |= val[p];
for(auto&[c, w]: adj[r]) if(c - p){
if(w + 1){
ed[tmp] = r;
nxt[tmp] = w;
val[c] = 1<<tmp;
tmp++;
dfs(c, r);
} else{
dfs(c, r);
}
}
}
int dist[maxm + 5][maxn], id;
void dfs2(int r, int p){
for(auto&[c, w]: adj[r]) if(c - p){
dist[id][c] = dist[id][r] + 1;
dfs2(c, r);
}
}
int dp[1<<maxm][maxm];
int main(){ IOS();
int n, s, t; cin >> n >> s >> t; s--, t--;
int m = 0;
rep(i,1,n){
int u, v, w; cin >> u >> v >> w; u--, v--, w--;
adj[u].pb({v, w});
adj[v].pb({u, w});
m += w != -1;
}
dfs(s, -1);
rep(i,0,m){
id = i;
dfs2(ed[i], -1);
}
id = m;
dfs2(s, -1);
auto chk = [&](int msk, int r){
return (val[r]&msk) == val[r];
};
per(i,(1<<m)-1,1){
vector<int> is, nis;
rep(e,0,m) {
if(i>>e&1){
if(chk(i, ed[e])) is.pb(e);
}else{
if(chk(i, nxt[e]) && chk(i, ed[e])) nis.pb(e);
}
}
for(int e: is){
dp[i][e] = inf;
if(chk(i, t)){
dp[i][e] = dist[e][t];
}else{
for(int x: nis){
dp[i][e] = min(dp[i][e], dp[i^(1<<x)][x] + dist[e][nxt[x]] + dist[x][nxt[x]]);
}
}
}
}
int ans = inf;
rep(i,0,m){
if(chk(0, nxt[i]) && chk(0, ed[i])) ans = min(ans, dp[1<<i][i] + dist[m][nxt[i]] + dist[i][nxt[i]]);
}
if(chk(0, t)){
ans = dist[m][t];
}
cout << (ans == inf? -1: ans) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
94 ms |
31200 KB |
Output is correct |
4 |
Correct |
82 ms |
31148 KB |
Output is correct |
5 |
Correct |
83 ms |
31256 KB |
Output is correct |
6 |
Correct |
88 ms |
31292 KB |
Output is correct |
7 |
Correct |
85 ms |
31300 KB |
Output is correct |
8 |
Correct |
95 ms |
31340 KB |
Output is correct |
9 |
Correct |
80 ms |
31408 KB |
Output is correct |
10 |
Correct |
78 ms |
31340 KB |
Output is correct |
11 |
Correct |
78 ms |
31300 KB |
Output is correct |
12 |
Correct |
93 ms |
32312 KB |
Output is correct |
13 |
Correct |
79 ms |
32068 KB |
Output is correct |
14 |
Correct |
67 ms |
31744 KB |
Output is correct |
15 |
Correct |
71 ms |
32404 KB |
Output is correct |
16 |
Correct |
77 ms |
33004 KB |
Output is correct |
17 |
Correct |
76 ms |
32980 KB |
Output is correct |
18 |
Correct |
73 ms |
33532 KB |
Output is correct |
19 |
Correct |
75 ms |
36592 KB |
Output is correct |
20 |
Correct |
83 ms |
36508 KB |
Output is correct |
21 |
Correct |
83 ms |
36296 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23800 KB |
Output is correct |
24 |
Correct |
12 ms |
23860 KB |
Output is correct |
25 |
Correct |
12 ms |
23892 KB |
Output is correct |
26 |
Correct |
13 ms |
23848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23892 KB |
Output is correct |
4 |
Correct |
12 ms |
23928 KB |
Output is correct |
5 |
Correct |
187 ms |
65740 KB |
Output is correct |
6 |
Correct |
139 ms |
65828 KB |
Output is correct |
7 |
Correct |
204 ms |
65780 KB |
Output is correct |
8 |
Correct |
163 ms |
65756 KB |
Output is correct |
9 |
Correct |
179 ms |
65732 KB |
Output is correct |
10 |
Correct |
91 ms |
65824 KB |
Output is correct |
11 |
Correct |
89 ms |
65868 KB |
Output is correct |
12 |
Correct |
93 ms |
65860 KB |
Output is correct |
13 |
Correct |
94 ms |
65800 KB |
Output is correct |
14 |
Correct |
96 ms |
65816 KB |
Output is correct |
15 |
Correct |
156 ms |
65772 KB |
Output is correct |
16 |
Correct |
203 ms |
65952 KB |
Output is correct |
17 |
Correct |
188 ms |
65796 KB |
Output is correct |
18 |
Correct |
114 ms |
65868 KB |
Output is correct |
19 |
Correct |
88 ms |
65924 KB |
Output is correct |
20 |
Correct |
90 ms |
65924 KB |
Output is correct |
21 |
Correct |
87 ms |
65980 KB |
Output is correct |
22 |
Correct |
112 ms |
66228 KB |
Output is correct |
23 |
Correct |
102 ms |
66224 KB |
Output is correct |
24 |
Correct |
105 ms |
66240 KB |
Output is correct |
25 |
Correct |
612 ms |
106092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23796 KB |
Output is correct |
3 |
Correct |
94 ms |
31200 KB |
Output is correct |
4 |
Correct |
82 ms |
31148 KB |
Output is correct |
5 |
Correct |
83 ms |
31256 KB |
Output is correct |
6 |
Correct |
88 ms |
31292 KB |
Output is correct |
7 |
Correct |
85 ms |
31300 KB |
Output is correct |
8 |
Correct |
95 ms |
31340 KB |
Output is correct |
9 |
Correct |
80 ms |
31408 KB |
Output is correct |
10 |
Correct |
78 ms |
31340 KB |
Output is correct |
11 |
Correct |
78 ms |
31300 KB |
Output is correct |
12 |
Correct |
93 ms |
32312 KB |
Output is correct |
13 |
Correct |
79 ms |
32068 KB |
Output is correct |
14 |
Correct |
67 ms |
31744 KB |
Output is correct |
15 |
Correct |
71 ms |
32404 KB |
Output is correct |
16 |
Correct |
77 ms |
33004 KB |
Output is correct |
17 |
Correct |
76 ms |
32980 KB |
Output is correct |
18 |
Correct |
73 ms |
33532 KB |
Output is correct |
19 |
Correct |
75 ms |
36592 KB |
Output is correct |
20 |
Correct |
83 ms |
36508 KB |
Output is correct |
21 |
Correct |
83 ms |
36296 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23800 KB |
Output is correct |
24 |
Correct |
12 ms |
23860 KB |
Output is correct |
25 |
Correct |
12 ms |
23892 KB |
Output is correct |
26 |
Correct |
13 ms |
23848 KB |
Output is correct |
27 |
Correct |
12 ms |
23764 KB |
Output is correct |
28 |
Correct |
12 ms |
23764 KB |
Output is correct |
29 |
Correct |
12 ms |
23892 KB |
Output is correct |
30 |
Correct |
12 ms |
23928 KB |
Output is correct |
31 |
Correct |
187 ms |
65740 KB |
Output is correct |
32 |
Correct |
139 ms |
65828 KB |
Output is correct |
33 |
Correct |
204 ms |
65780 KB |
Output is correct |
34 |
Correct |
163 ms |
65756 KB |
Output is correct |
35 |
Correct |
179 ms |
65732 KB |
Output is correct |
36 |
Correct |
91 ms |
65824 KB |
Output is correct |
37 |
Correct |
89 ms |
65868 KB |
Output is correct |
38 |
Correct |
93 ms |
65860 KB |
Output is correct |
39 |
Correct |
94 ms |
65800 KB |
Output is correct |
40 |
Correct |
96 ms |
65816 KB |
Output is correct |
41 |
Correct |
156 ms |
65772 KB |
Output is correct |
42 |
Correct |
203 ms |
65952 KB |
Output is correct |
43 |
Correct |
188 ms |
65796 KB |
Output is correct |
44 |
Correct |
114 ms |
65868 KB |
Output is correct |
45 |
Correct |
88 ms |
65924 KB |
Output is correct |
46 |
Correct |
90 ms |
65924 KB |
Output is correct |
47 |
Correct |
87 ms |
65980 KB |
Output is correct |
48 |
Correct |
112 ms |
66228 KB |
Output is correct |
49 |
Correct |
102 ms |
66224 KB |
Output is correct |
50 |
Correct |
105 ms |
66240 KB |
Output is correct |
51 |
Correct |
612 ms |
106092 KB |
Output is correct |
52 |
Execution timed out |
2083 ms |
105000 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |