#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200
#define INF 0x3f3f3f3f3f3f3f3fLL
void append(int **ej, int **ec, int *eo, int i, int j, int c) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ec[i] = (int *) realloc(ec[i], o * 2 * sizeof *ec[i]);
}
ej[i][o] = j, ec[i][o] = c;
}
long long dd[N * N]; int iq[N * N + 1], pq[N * N], cnt;
int lt(int i, int j) { return dd[i] < dd[j]; }
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
int main() {
static int *ej[N], *ec[N], eo[N], *fi[N], *fc[N], fo[N];
int n, m, s, t, h, i, i_, j, j_, k, u, v, w, o1, o2, c1, c2;
long long d;
scanf("%d%d%d%d", &n, &m, &s, &t), s--, t--;
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ec[i] = (int *) malloc(2 * sizeof *ec[i]);
fi[i] = (int *) malloc(2 * sizeof *fi[i]);
fc[i] = (int *) malloc(2 * sizeof *fc[i]);
}
for (h = 0; h < m; h++) {
static char s[2];
int c, d;
scanf("%d%d%s", &i, &j, s), i--, j--;
switch (s[0]) {
case '(':
c = 0, d = 1;
break;
case ')':
c = 0, d = 2;
break;
case '[':
c = 1, d = 1;
break;
case ']':
c = 1, d = 2;
break;
case '{':
c = 2, d = 1;
break;
case '}':
c = 2, d = 2;
break;
case '<':
c = 3, d = 1;
break;
case '>':
c = 3, d = 2;
break;
default:
c = -1, d = 0;
break;
}
if (d == 1)
append(fi, fc, fo, j, i, c);
else
append(ej, ec, eo, i, j, c);
}
memset(dd, 0x3f, n * n * sizeof *dd);
for (i = 0; i < n; i++) {
u = i * n + i;
dd[u] = 0, pq_add_last(u);
}
while (cnt) {
u = pq_remove_first(), i = u / n, j = u % n;
if (i == s && j == t) {
printf("%lld\n", dd[u]);
return 0;
}
for (o1 = fo[i]; o1--; ) {
i_ = fi[i][o1], c1 = fc[i][o1];
for (o2 = eo[j]; o2--; ) {
j_ = ej[j][o2], c2 = ec[j][o2];
if (c1 == c2) {
w = i_ * n + j_, d = dd[u] + 2;
if (dd[w] > d) {
if (dd[w] == INF)
pq_add_last(w);
dd[w] = d, pq_up(w);
}
}
}
}
for (k = 0; k < n; k++) {
v = j * n + k, w = i * n + k, d = dd[u] + dd[v];
if (dd[w] > d) {
if (dd[w] == INF)
pq_add_last(w);
dd[w] = d, pq_up(w);
}
v = k * n + i, w = k * n + j, d = dd[v] + dd[u];
if (dd[w] > d) {
if (dd[w] == INF)
pq_add_last(w);
dd[w] = d, pq_up(w);
}
}
}
printf("-1\n");
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
11 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
Main.c: In function 'main':
Main.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%d%d%d", &n, &m, &s, &t), s--, t--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:71:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d%d%s", &i, &j, s), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
420 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
420 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
424 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
436 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
420 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
424 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
436 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
420 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
424 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
436 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
6 ms |
412 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
7 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
6 ms |
604 KB |
Output is correct |
46 |
Correct |
9 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
548 KB |
Output is correct |
48 |
Correct |
10 ms |
604 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
860 KB |
Output is correct |
2 |
Correct |
15 ms |
860 KB |
Output is correct |
3 |
Correct |
17 ms |
860 KB |
Output is correct |
4 |
Correct |
16 ms |
860 KB |
Output is correct |
5 |
Correct |
15 ms |
860 KB |
Output is correct |
6 |
Correct |
14 ms |
860 KB |
Output is correct |
7 |
Correct |
14 ms |
860 KB |
Output is correct |
8 |
Correct |
15 ms |
860 KB |
Output is correct |
9 |
Correct |
15 ms |
860 KB |
Output is correct |
10 |
Correct |
14 ms |
860 KB |
Output is correct |
11 |
Correct |
19 ms |
860 KB |
Output is correct |
12 |
Correct |
19 ms |
1012 KB |
Output is correct |
13 |
Correct |
13 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
4 ms |
860 KB |
Output is correct |
16 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
420 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
424 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
424 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
436 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
3 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
6 ms |
412 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
7 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
6 ms |
604 KB |
Output is correct |
46 |
Correct |
9 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
548 KB |
Output is correct |
48 |
Correct |
10 ms |
604 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
15 ms |
860 KB |
Output is correct |
51 |
Correct |
15 ms |
860 KB |
Output is correct |
52 |
Correct |
17 ms |
860 KB |
Output is correct |
53 |
Correct |
16 ms |
860 KB |
Output is correct |
54 |
Correct |
15 ms |
860 KB |
Output is correct |
55 |
Correct |
14 ms |
860 KB |
Output is correct |
56 |
Correct |
14 ms |
860 KB |
Output is correct |
57 |
Correct |
15 ms |
860 KB |
Output is correct |
58 |
Correct |
15 ms |
860 KB |
Output is correct |
59 |
Correct |
14 ms |
860 KB |
Output is correct |
60 |
Correct |
19 ms |
860 KB |
Output is correct |
61 |
Correct |
19 ms |
1012 KB |
Output is correct |
62 |
Correct |
13 ms |
860 KB |
Output is correct |
63 |
Correct |
1 ms |
860 KB |
Output is correct |
64 |
Correct |
4 ms |
860 KB |
Output is correct |
65 |
Correct |
1 ms |
860 KB |
Output is correct |
66 |
Correct |
1 ms |
860 KB |
Output is correct |
67 |
Correct |
32 ms |
860 KB |
Output is correct |
68 |
Correct |
33 ms |
1004 KB |
Output is correct |
69 |
Correct |
32 ms |
860 KB |
Output is correct |
70 |
Correct |
3 ms |
860 KB |
Output is correct |
71 |
Correct |
3 ms |
860 KB |
Output is correct |
72 |
Correct |
13 ms |
1120 KB |
Output is correct |
73 |
Correct |
29 ms |
864 KB |
Output is correct |
74 |
Correct |
6 ms |
864 KB |
Output is correct |
75 |
Correct |
31 ms |
1108 KB |
Output is correct |
76 |
Correct |
2 ms |
860 KB |
Output is correct |
77 |
Correct |
1 ms |
704 KB |
Output is correct |
78 |
Correct |
5 ms |
944 KB |
Output is correct |
79 |
Correct |
31 ms |
868 KB |
Output is correct |
80 |
Correct |
34 ms |
1120 KB |
Output is correct |
81 |
Correct |
5 ms |
868 KB |
Output is correct |
82 |
Correct |
3 ms |
356 KB |
Output is correct |
83 |
Correct |
22 ms |
872 KB |
Output is correct |