#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int n, m, s, t, val[N], d[22][N], r[22][N];
int dp[1 << 20][20], key[22], num[22], door[22], distDoor[22], reqDoor[22], dist[22][22], req[22][22];
vector<pair<int, int>> adj[N];
void Dfs(int u, int p, int keyInd) {
for(auto e : adj[u]) {
int v = e.first;
int ind = e.second;
if (v == p)
continue;
r[keyInd][v] = r[keyInd][u] | val[ind];
d[keyInd][v] = d[keyInd][u] + 1;
if (keyInd != -1 && val[ind] == (1 << keyInd)) {
door[keyInd] = u;
distDoor[keyInd] = d[keyInd][u];
reqDoor[keyInd] = r[keyInd][u];
}
Dfs(v, u, keyInd);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s >> t;
s--, t--;
for(int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--, w--;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
if (w >= 0) {
key[m] = w;
num[m] = i;
++m;
}
}
key[m] = s;
key[m + 1] = t;
door[m] = s;
door[m + 1] = t;
for(int i = 0; i < m; i++)
val[num[i]] = (1 << i);
for(int i = 0; i < m + 2; i++) {
Dfs(key[i], -1, i);
}
for(int i = 0; i < m + 2; i++)
for(int j = 0; j < m + 2; j++) {
dist[i][j] = d[j][door[i]] + distDoor[j];
req[i][j] = r[j][door[i]] | reqDoor[j];
}
int ans = (req[m][m + 1] == 0? dist[m][m + 1] : 1e9);
for(int mask = 0; mask < (1 << m); mask++)
for(int i = 0; i < m; i++)
dp[mask][i] = 1e9;
for(int i = 0; i < m; i++)
if (req[m][i] == 0)
dp[(1 << i)][i] = dist[m][i];
for(int mask = 0; mask < (1 << m); mask++)
for(int i = 0; i < m; i++) {
if (dp[mask][i] == 1e9)
continue;
if ((mask & req[i][m + 1]) == req[i][m + 1])
ans = min(ans, dp[mask][i] + dist[i][m + 1]);
for(int j = 0; j < m; j++)
if ((mask & req[i][j]) == req[i][j])
dp[mask | (1 << j)][j] = min(dp[mask | (1 << j)][j], dp[mask][i] + dist[i][j]);
}
cout << (ans == 1e9? -1 : ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23816 KB |
Output is correct |
2 |
Correct |
12 ms |
23812 KB |
Output is correct |
3 |
Correct |
87 ms |
36272 KB |
Output is correct |
4 |
Correct |
93 ms |
36308 KB |
Output is correct |
5 |
Correct |
85 ms |
36296 KB |
Output is correct |
6 |
Correct |
86 ms |
36292 KB |
Output is correct |
7 |
Correct |
83 ms |
36432 KB |
Output is correct |
8 |
Correct |
84 ms |
36308 KB |
Output is correct |
9 |
Correct |
100 ms |
36660 KB |
Output is correct |
10 |
Correct |
83 ms |
36300 KB |
Output is correct |
11 |
Correct |
86 ms |
36340 KB |
Output is correct |
12 |
Correct |
72 ms |
38052 KB |
Output is correct |
13 |
Correct |
72 ms |
38740 KB |
Output is correct |
14 |
Correct |
78 ms |
37620 KB |
Output is correct |
15 |
Correct |
79 ms |
38024 KB |
Output is correct |
16 |
Correct |
76 ms |
38860 KB |
Output is correct |
17 |
Correct |
72 ms |
38864 KB |
Output is correct |
18 |
Correct |
74 ms |
39260 KB |
Output is correct |
19 |
Correct |
86 ms |
43184 KB |
Output is correct |
20 |
Correct |
97 ms |
43448 KB |
Output is correct |
21 |
Correct |
82 ms |
43204 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23940 KB |
Output is correct |
24 |
Correct |
12 ms |
23972 KB |
Output is correct |
25 |
Correct |
12 ms |
23936 KB |
Output is correct |
26 |
Correct |
12 ms |
23984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23856 KB |
Output is correct |
2 |
Correct |
11 ms |
23892 KB |
Output is correct |
3 |
Correct |
12 ms |
24064 KB |
Output is correct |
4 |
Correct |
12 ms |
24032 KB |
Output is correct |
5 |
Correct |
46 ms |
66608 KB |
Output is correct |
6 |
Correct |
49 ms |
66636 KB |
Output is correct |
7 |
Correct |
46 ms |
66636 KB |
Output is correct |
8 |
Correct |
47 ms |
66644 KB |
Output is correct |
9 |
Correct |
46 ms |
66636 KB |
Output is correct |
10 |
Correct |
45 ms |
66660 KB |
Output is correct |
11 |
Correct |
46 ms |
66672 KB |
Output is correct |
12 |
Correct |
46 ms |
66724 KB |
Output is correct |
13 |
Correct |
46 ms |
66652 KB |
Output is correct |
14 |
Correct |
49 ms |
66680 KB |
Output is correct |
15 |
Correct |
44 ms |
66760 KB |
Output is correct |
16 |
Correct |
46 ms |
66660 KB |
Output is correct |
17 |
Correct |
47 ms |
66620 KB |
Output is correct |
18 |
Correct |
44 ms |
66772 KB |
Output is correct |
19 |
Correct |
45 ms |
66824 KB |
Output is correct |
20 |
Correct |
51 ms |
66788 KB |
Output is correct |
21 |
Correct |
46 ms |
66820 KB |
Output is correct |
22 |
Correct |
46 ms |
67100 KB |
Output is correct |
23 |
Correct |
45 ms |
67080 KB |
Output is correct |
24 |
Correct |
45 ms |
67128 KB |
Output is correct |
25 |
Correct |
491 ms |
106240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23816 KB |
Output is correct |
2 |
Correct |
12 ms |
23812 KB |
Output is correct |
3 |
Correct |
87 ms |
36272 KB |
Output is correct |
4 |
Correct |
93 ms |
36308 KB |
Output is correct |
5 |
Correct |
85 ms |
36296 KB |
Output is correct |
6 |
Correct |
86 ms |
36292 KB |
Output is correct |
7 |
Correct |
83 ms |
36432 KB |
Output is correct |
8 |
Correct |
84 ms |
36308 KB |
Output is correct |
9 |
Correct |
100 ms |
36660 KB |
Output is correct |
10 |
Correct |
83 ms |
36300 KB |
Output is correct |
11 |
Correct |
86 ms |
36340 KB |
Output is correct |
12 |
Correct |
72 ms |
38052 KB |
Output is correct |
13 |
Correct |
72 ms |
38740 KB |
Output is correct |
14 |
Correct |
78 ms |
37620 KB |
Output is correct |
15 |
Correct |
79 ms |
38024 KB |
Output is correct |
16 |
Correct |
76 ms |
38860 KB |
Output is correct |
17 |
Correct |
72 ms |
38864 KB |
Output is correct |
18 |
Correct |
74 ms |
39260 KB |
Output is correct |
19 |
Correct |
86 ms |
43184 KB |
Output is correct |
20 |
Correct |
97 ms |
43448 KB |
Output is correct |
21 |
Correct |
82 ms |
43204 KB |
Output is correct |
22 |
Correct |
12 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23940 KB |
Output is correct |
24 |
Correct |
12 ms |
23972 KB |
Output is correct |
25 |
Correct |
12 ms |
23936 KB |
Output is correct |
26 |
Correct |
12 ms |
23984 KB |
Output is correct |
27 |
Correct |
11 ms |
23856 KB |
Output is correct |
28 |
Correct |
11 ms |
23892 KB |
Output is correct |
29 |
Correct |
12 ms |
24064 KB |
Output is correct |
30 |
Correct |
12 ms |
24032 KB |
Output is correct |
31 |
Correct |
46 ms |
66608 KB |
Output is correct |
32 |
Correct |
49 ms |
66636 KB |
Output is correct |
33 |
Correct |
46 ms |
66636 KB |
Output is correct |
34 |
Correct |
47 ms |
66644 KB |
Output is correct |
35 |
Correct |
46 ms |
66636 KB |
Output is correct |
36 |
Correct |
45 ms |
66660 KB |
Output is correct |
37 |
Correct |
46 ms |
66672 KB |
Output is correct |
38 |
Correct |
46 ms |
66724 KB |
Output is correct |
39 |
Correct |
46 ms |
66652 KB |
Output is correct |
40 |
Correct |
49 ms |
66680 KB |
Output is correct |
41 |
Correct |
44 ms |
66760 KB |
Output is correct |
42 |
Correct |
46 ms |
66660 KB |
Output is correct |
43 |
Correct |
47 ms |
66620 KB |
Output is correct |
44 |
Correct |
44 ms |
66772 KB |
Output is correct |
45 |
Correct |
45 ms |
66824 KB |
Output is correct |
46 |
Correct |
51 ms |
66788 KB |
Output is correct |
47 |
Correct |
46 ms |
66820 KB |
Output is correct |
48 |
Correct |
46 ms |
67100 KB |
Output is correct |
49 |
Correct |
45 ms |
67080 KB |
Output is correct |
50 |
Correct |
45 ms |
67128 KB |
Output is correct |
51 |
Correct |
491 ms |
106240 KB |
Output is correct |
52 |
Execution timed out |
2068 ms |
160768 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |