# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
635324 |
2022-08-26T05:20:13 Z |
S2speed |
LOSTIKS (INOI20_lostiks) |
C++17 |
|
2000 ms |
232312 KB |
#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];
vector<int> dis[maxn] , bfs;
int g[maxn] , k[22] , d[22] , e[22] , dp[maxn][22];
void BFS(int r , bool f = false){
bfs.clear();
dis[r].assign(n , inf);
dis[r][r] = 0;
g[r] = 0;
bfs.push_back(r);
int x = 0;
while(x < sze(bfs)){
int v = bfs[x++];
for(auto p : adj[v]){
int 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--;
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});
}
}
BFS(s , true);
if(g[t] == 0){
cout<<dis[s][t]<<'\n';
return 0;
}
for(int 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]];
}
}
int lm = (1 << m);
for(int mask = 1 ; mask < lm ; mask++){
for(int j = 0 ; j < m ; j++){
if(mask & (1 << j)) continue;
if((e[j] & mask) != e[j]) continue;
int h = inf;
for(int 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';
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[d[i]][t]);
}
ans = min(ans , h);
}
cout<<(ans == inf ? -1 : ans)<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
139832 KB |
Output is correct |
2 |
Correct |
57 ms |
139760 KB |
Output is correct |
3 |
Correct |
129 ms |
148684 KB |
Output is correct |
4 |
Correct |
147 ms |
148544 KB |
Output is correct |
5 |
Correct |
148 ms |
148616 KB |
Output is correct |
6 |
Correct |
142 ms |
148480 KB |
Output is correct |
7 |
Correct |
126 ms |
148548 KB |
Output is correct |
8 |
Correct |
123 ms |
148424 KB |
Output is correct |
9 |
Correct |
136 ms |
148508 KB |
Output is correct |
10 |
Correct |
145 ms |
148552 KB |
Output is correct |
11 |
Correct |
139 ms |
148516 KB |
Output is correct |
12 |
Correct |
138 ms |
147888 KB |
Output is correct |
13 |
Correct |
126 ms |
148020 KB |
Output is correct |
14 |
Correct |
143 ms |
147944 KB |
Output is correct |
15 |
Correct |
132 ms |
148020 KB |
Output is correct |
16 |
Correct |
117 ms |
147744 KB |
Output is correct |
17 |
Correct |
124 ms |
147800 KB |
Output is correct |
18 |
Correct |
114 ms |
147904 KB |
Output is correct |
19 |
Correct |
110 ms |
147724 KB |
Output is correct |
20 |
Correct |
109 ms |
147720 KB |
Output is correct |
21 |
Correct |
112 ms |
147788 KB |
Output is correct |
22 |
Correct |
63 ms |
139856 KB |
Output is correct |
23 |
Correct |
64 ms |
139756 KB |
Output is correct |
24 |
Correct |
59 ms |
139848 KB |
Output is correct |
25 |
Correct |
55 ms |
139904 KB |
Output is correct |
26 |
Correct |
56 ms |
139852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
139848 KB |
Output is correct |
2 |
Correct |
58 ms |
139844 KB |
Output is correct |
3 |
Correct |
68 ms |
139792 KB |
Output is correct |
4 |
Correct |
65 ms |
139800 KB |
Output is correct |
5 |
Correct |
172 ms |
140784 KB |
Output is correct |
6 |
Correct |
150 ms |
140748 KB |
Output is correct |
7 |
Correct |
206 ms |
140800 KB |
Output is correct |
8 |
Correct |
169 ms |
140696 KB |
Output is correct |
9 |
Correct |
178 ms |
140876 KB |
Output is correct |
10 |
Correct |
120 ms |
140772 KB |
Output is correct |
11 |
Correct |
140 ms |
140712 KB |
Output is correct |
12 |
Correct |
118 ms |
140832 KB |
Output is correct |
13 |
Correct |
121 ms |
140800 KB |
Output is correct |
14 |
Correct |
118 ms |
140680 KB |
Output is correct |
15 |
Correct |
167 ms |
140748 KB |
Output is correct |
16 |
Correct |
202 ms |
140828 KB |
Output is correct |
17 |
Correct |
172 ms |
140764 KB |
Output is correct |
18 |
Correct |
122 ms |
140756 KB |
Output is correct |
19 |
Correct |
128 ms |
140760 KB |
Output is correct |
20 |
Correct |
124 ms |
140708 KB |
Output is correct |
21 |
Correct |
127 ms |
140768 KB |
Output is correct |
22 |
Correct |
129 ms |
140680 KB |
Output is correct |
23 |
Correct |
126 ms |
140876 KB |
Output is correct |
24 |
Correct |
129 ms |
140756 KB |
Output is correct |
25 |
Correct |
621 ms |
139844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
139832 KB |
Output is correct |
2 |
Correct |
57 ms |
139760 KB |
Output is correct |
3 |
Correct |
129 ms |
148684 KB |
Output is correct |
4 |
Correct |
147 ms |
148544 KB |
Output is correct |
5 |
Correct |
148 ms |
148616 KB |
Output is correct |
6 |
Correct |
142 ms |
148480 KB |
Output is correct |
7 |
Correct |
126 ms |
148548 KB |
Output is correct |
8 |
Correct |
123 ms |
148424 KB |
Output is correct |
9 |
Correct |
136 ms |
148508 KB |
Output is correct |
10 |
Correct |
145 ms |
148552 KB |
Output is correct |
11 |
Correct |
139 ms |
148516 KB |
Output is correct |
12 |
Correct |
138 ms |
147888 KB |
Output is correct |
13 |
Correct |
126 ms |
148020 KB |
Output is correct |
14 |
Correct |
143 ms |
147944 KB |
Output is correct |
15 |
Correct |
132 ms |
148020 KB |
Output is correct |
16 |
Correct |
117 ms |
147744 KB |
Output is correct |
17 |
Correct |
124 ms |
147800 KB |
Output is correct |
18 |
Correct |
114 ms |
147904 KB |
Output is correct |
19 |
Correct |
110 ms |
147724 KB |
Output is correct |
20 |
Correct |
109 ms |
147720 KB |
Output is correct |
21 |
Correct |
112 ms |
147788 KB |
Output is correct |
22 |
Correct |
63 ms |
139856 KB |
Output is correct |
23 |
Correct |
64 ms |
139756 KB |
Output is correct |
24 |
Correct |
59 ms |
139848 KB |
Output is correct |
25 |
Correct |
55 ms |
139904 KB |
Output is correct |
26 |
Correct |
56 ms |
139852 KB |
Output is correct |
27 |
Correct |
56 ms |
139848 KB |
Output is correct |
28 |
Correct |
58 ms |
139844 KB |
Output is correct |
29 |
Correct |
68 ms |
139792 KB |
Output is correct |
30 |
Correct |
65 ms |
139800 KB |
Output is correct |
31 |
Correct |
172 ms |
140784 KB |
Output is correct |
32 |
Correct |
150 ms |
140748 KB |
Output is correct |
33 |
Correct |
206 ms |
140800 KB |
Output is correct |
34 |
Correct |
169 ms |
140696 KB |
Output is correct |
35 |
Correct |
178 ms |
140876 KB |
Output is correct |
36 |
Correct |
120 ms |
140772 KB |
Output is correct |
37 |
Correct |
140 ms |
140712 KB |
Output is correct |
38 |
Correct |
118 ms |
140832 KB |
Output is correct |
39 |
Correct |
121 ms |
140800 KB |
Output is correct |
40 |
Correct |
118 ms |
140680 KB |
Output is correct |
41 |
Correct |
167 ms |
140748 KB |
Output is correct |
42 |
Correct |
202 ms |
140828 KB |
Output is correct |
43 |
Correct |
172 ms |
140764 KB |
Output is correct |
44 |
Correct |
122 ms |
140756 KB |
Output is correct |
45 |
Correct |
128 ms |
140760 KB |
Output is correct |
46 |
Correct |
124 ms |
140708 KB |
Output is correct |
47 |
Correct |
127 ms |
140768 KB |
Output is correct |
48 |
Correct |
129 ms |
140680 KB |
Output is correct |
49 |
Correct |
126 ms |
140876 KB |
Output is correct |
50 |
Correct |
129 ms |
140756 KB |
Output is correct |
51 |
Correct |
621 ms |
139844 KB |
Output is correct |
52 |
Correct |
1866 ms |
220500 KB |
Output is correct |
53 |
Execution timed out |
2083 ms |
232312 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |