# include <bits/stdc++.h>
using namespace std;
const int MN = 201;
vector <pair <int, char> > opening[MN];
vector <pair <int, char> > closing[MN];
char val[128];
typedef long long res_t;
res_t dp[MN][MN];
const res_t inf = 1e18;
set <pair <res_t, pair <int, int> > > dijkstra_queue;
void enque(int start, int end, res_t value) {
if (dp[start][end] > value) {
dijkstra_queue.erase(make_pair(dp[start][end], make_pair(start, end)));
dp[start][end] = value;
dijkstra_queue.insert(make_pair(dp[start][end], make_pair(start, end)));
}
}
int main() {
val['['] = 1;
val[']'] = -1;
val['{'] = 2;
val['}'] = -2;
val['('] = 3;
val[')'] = -3;
val['<'] = 4;
val['>'] = -4;
int n, m, s, t;
scanf("%d%d%d%d", &n, &m, &s, &t);
fprintf(stderr, "n = %d m = %d\n", n, m);
for (int i = 0; i < m; ++i) {
int a, b;
char c;
scanf("%d%d %c", &a, &b, &c);
c = val[(int)c];
if (c > 0)
opening[b].push_back(make_pair(a, c));
else
closing[a].push_back(make_pair(b, -c));
}
for (int i = 1; i <= n; ++i)
for (int k = 1; k <= n; ++k)
dp[i][k] = inf;
for (int i = 1; i <= n; ++i)
enque(i, i, 0);
int turns = 0;
while (dijkstra_queue.size()) {
turns++;
auto curr = *dijkstra_queue.begin();
dijkstra_queue.erase(dijkstra_queue.begin());
int start = curr.second.first, end = curr.second.second;
assert(dp[start][end] < inf);
for (int i = 1; i <= n; ++i) {
enque(start, i, dp[start][end] + dp[end][i]);
enque(i, end, dp[i][start] + dp[start][end]);
}
for (auto x : opening[start])
for (auto y : closing[end]) {
if (x.second == y.second)
enque(x.first, y.first, dp[start][end] + 2);
}
}
fprintf(stderr, "%d / %d turns\n", turns, n * n);
if (dp[s][t] == inf) {
printf("-1\n");
fprintf(stderr, "-1\n");
}
else {
printf("%lld\n", dp[s][t]);
fprintf(stderr, "%lld = %.3Le\n", dp[s][t], (long double)dp[s][t]);
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
28 | scanf("%d%d%d%d", &n, &m, &s, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d%d %c", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
4 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
4 ms |
536 KB |
Output is correct |
30 |
Correct |
10 ms |
560 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
3 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
8 ms |
460 KB |
Output is correct |
36 |
Correct |
5 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
4 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
4 ms |
536 KB |
Output is correct |
30 |
Correct |
10 ms |
560 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
3 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
8 ms |
460 KB |
Output is correct |
36 |
Correct |
5 ms |
460 KB |
Output is correct |
37 |
Correct |
5 ms |
488 KB |
Output is correct |
38 |
Correct |
26 ms |
1064 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
25 ms |
1076 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
460 KB |
Output is correct |
44 |
Correct |
3 ms |
460 KB |
Output is correct |
45 |
Correct |
27 ms |
976 KB |
Output is correct |
46 |
Correct |
29 ms |
1060 KB |
Output is correct |
47 |
Correct |
4 ms |
460 KB |
Output is correct |
48 |
Correct |
37 ms |
972 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
1712 KB |
Output is correct |
2 |
Correct |
70 ms |
1756 KB |
Output is correct |
3 |
Correct |
70 ms |
1752 KB |
Output is correct |
4 |
Correct |
70 ms |
1740 KB |
Output is correct |
5 |
Correct |
69 ms |
1716 KB |
Output is correct |
6 |
Correct |
65 ms |
1728 KB |
Output is correct |
7 |
Correct |
64 ms |
1696 KB |
Output is correct |
8 |
Correct |
75 ms |
1756 KB |
Output is correct |
9 |
Correct |
76 ms |
1740 KB |
Output is correct |
10 |
Correct |
67 ms |
1612 KB |
Output is correct |
11 |
Correct |
70 ms |
1712 KB |
Output is correct |
12 |
Correct |
68 ms |
1612 KB |
Output is correct |
13 |
Correct |
61 ms |
1608 KB |
Output is correct |
14 |
Correct |
19 ms |
716 KB |
Output is correct |
15 |
Correct |
14 ms |
716 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
4 ms |
460 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
4 ms |
536 KB |
Output is correct |
30 |
Correct |
10 ms |
560 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
3 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
8 ms |
460 KB |
Output is correct |
36 |
Correct |
5 ms |
460 KB |
Output is correct |
37 |
Correct |
5 ms |
488 KB |
Output is correct |
38 |
Correct |
26 ms |
1064 KB |
Output is correct |
39 |
Correct |
2 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
25 ms |
1076 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
460 KB |
Output is correct |
44 |
Correct |
3 ms |
460 KB |
Output is correct |
45 |
Correct |
27 ms |
976 KB |
Output is correct |
46 |
Correct |
29 ms |
1060 KB |
Output is correct |
47 |
Correct |
4 ms |
460 KB |
Output is correct |
48 |
Correct |
37 ms |
972 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
83 ms |
1712 KB |
Output is correct |
51 |
Correct |
70 ms |
1756 KB |
Output is correct |
52 |
Correct |
70 ms |
1752 KB |
Output is correct |
53 |
Correct |
70 ms |
1740 KB |
Output is correct |
54 |
Correct |
69 ms |
1716 KB |
Output is correct |
55 |
Correct |
65 ms |
1728 KB |
Output is correct |
56 |
Correct |
64 ms |
1696 KB |
Output is correct |
57 |
Correct |
75 ms |
1756 KB |
Output is correct |
58 |
Correct |
76 ms |
1740 KB |
Output is correct |
59 |
Correct |
67 ms |
1612 KB |
Output is correct |
60 |
Correct |
70 ms |
1712 KB |
Output is correct |
61 |
Correct |
68 ms |
1612 KB |
Output is correct |
62 |
Correct |
61 ms |
1608 KB |
Output is correct |
63 |
Correct |
19 ms |
716 KB |
Output is correct |
64 |
Correct |
14 ms |
716 KB |
Output is correct |
65 |
Correct |
2 ms |
588 KB |
Output is correct |
66 |
Correct |
2 ms |
588 KB |
Output is correct |
67 |
Correct |
213 ms |
2924 KB |
Output is correct |
68 |
Correct |
224 ms |
2884 KB |
Output is correct |
69 |
Correct |
227 ms |
2940 KB |
Output is correct |
70 |
Correct |
7 ms |
716 KB |
Output is correct |
71 |
Correct |
17 ms |
588 KB |
Output is correct |
72 |
Correct |
155 ms |
2972 KB |
Output is correct |
73 |
Correct |
154 ms |
2836 KB |
Output is correct |
74 |
Correct |
25 ms |
588 KB |
Output is correct |
75 |
Correct |
222 ms |
2936 KB |
Output is correct |
76 |
Correct |
8 ms |
736 KB |
Output is correct |
77 |
Correct |
4 ms |
460 KB |
Output is correct |
78 |
Correct |
17 ms |
744 KB |
Output is correct |
79 |
Correct |
169 ms |
2744 KB |
Output is correct |
80 |
Correct |
223 ms |
2900 KB |
Output is correct |
81 |
Correct |
28 ms |
588 KB |
Output is correct |
82 |
Correct |
9 ms |
576 KB |
Output is correct |
83 |
Correct |
160 ms |
3012 KB |
Output is correct |