# include <bits/stdc++.h>
using namespace std;
const int MN = 201;
vector <pair <int, char> > opening[MN], closing[MN];
typedef long long res_t;
char val[128];
const res_t inf = 1e18;
//a, b, c, d --> dp[a][d] min= dp[b][c] + 2
//-1 i, k, l --> dp[i][k] min= dp[i][j] + dp[j][k]
struct rule {
int a, b, c, d;
rule(int a_, int b_, int c_, int d_) : a(a_), b(b_), c(c_), d(d_) {}
};
vector <rule> rules;
res_t dp[MN][MN];
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);
for (int i = 0; i < m; ++i) {
int x, y;
char c;
scanf("%d%d %c", &x, &y, &c);
c = val[(int)c];
if (c > 0)
opening[x].push_back(make_pair(y, c));
else
closing[x].push_back(make_pair(y, -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)
dp[i][i] = 0;
for (int i = 1; i <= n; ++i)
for (int k = 1; k <= n; ++k)
for (auto x : opening[i])
for (auto y : closing[k])
if (x.second == y.second)
rules.emplace_back(i, x.first, k, y.first);
for (int i = 1; i <= n; ++i)
for (int k = 1; k <= n; ++k)
for (int l = 1; l <= n; ++l)
rules.emplace_back(-1, i, k, l);
random_shuffle(rules.begin(), rules.end());
bool changed = true;
while (changed) {
changed = false;
for (auto rule : rules) {
if (rule.a == -1) {
if (dp[rule.b][rule.d] > dp[rule.b][rule.c] + dp[rule.c][rule.d]) {
dp[rule.b][rule.d] = dp[rule.b][rule.c] + dp[rule.c][rule.d];
changed = true;
}
}
else {
if (dp[rule.a][rule.d] > dp[rule.b][rule.c] + 2) {
dp[rule.a][rule.d] = dp[rule.b][rule.c] + 2;
changed = true;
}
}
}
}
if (dp[s][t] == inf)
printf("-1\n");
else
printf("%lld\n", dp[s][t]);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:27:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | scanf("%d%d%d%d", &n, &m, &s, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf("%d%d %c", &x, &y, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
8 |
Correct |
2 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
8 |
Correct |
2 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 |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
716 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
716 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
8 |
Correct |
2 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 |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
716 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
716 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
716 KB |
Output is correct |
25 |
Correct |
21 ms |
2520 KB |
Output is correct |
26 |
Correct |
8 ms |
2496 KB |
Output is correct |
27 |
Correct |
185 ms |
4668 KB |
Output is correct |
28 |
Correct |
8 ms |
2496 KB |
Output is correct |
29 |
Correct |
9 ms |
4540 KB |
Output is correct |
30 |
Correct |
33 ms |
8736 KB |
Output is correct |
31 |
Correct |
86 ms |
2496 KB |
Output is correct |
32 |
Correct |
9 ms |
2496 KB |
Output is correct |
33 |
Correct |
10 ms |
2496 KB |
Output is correct |
34 |
Correct |
15 ms |
2496 KB |
Output is correct |
35 |
Correct |
24 ms |
8760 KB |
Output is correct |
36 |
Correct |
12 ms |
4540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
8 |
Correct |
2 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 |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
716 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
716 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
716 KB |
Output is correct |
25 |
Correct |
21 ms |
2520 KB |
Output is correct |
26 |
Correct |
8 ms |
2496 KB |
Output is correct |
27 |
Correct |
185 ms |
4668 KB |
Output is correct |
28 |
Correct |
8 ms |
2496 KB |
Output is correct |
29 |
Correct |
9 ms |
4540 KB |
Output is correct |
30 |
Correct |
33 ms |
8736 KB |
Output is correct |
31 |
Correct |
86 ms |
2496 KB |
Output is correct |
32 |
Correct |
9 ms |
2496 KB |
Output is correct |
33 |
Correct |
10 ms |
2496 KB |
Output is correct |
34 |
Correct |
15 ms |
2496 KB |
Output is correct |
35 |
Correct |
24 ms |
8760 KB |
Output is correct |
36 |
Correct |
12 ms |
4540 KB |
Output is correct |
37 |
Execution timed out |
1091 ms |
16948 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1102 ms |
132080 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
8 |
Correct |
2 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 |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
716 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
716 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
4 ms |
716 KB |
Output is correct |
25 |
Correct |
21 ms |
2520 KB |
Output is correct |
26 |
Correct |
8 ms |
2496 KB |
Output is correct |
27 |
Correct |
185 ms |
4668 KB |
Output is correct |
28 |
Correct |
8 ms |
2496 KB |
Output is correct |
29 |
Correct |
9 ms |
4540 KB |
Output is correct |
30 |
Correct |
33 ms |
8736 KB |
Output is correct |
31 |
Correct |
86 ms |
2496 KB |
Output is correct |
32 |
Correct |
9 ms |
2496 KB |
Output is correct |
33 |
Correct |
10 ms |
2496 KB |
Output is correct |
34 |
Correct |
15 ms |
2496 KB |
Output is correct |
35 |
Correct |
24 ms |
8760 KB |
Output is correct |
36 |
Correct |
12 ms |
4540 KB |
Output is correct |
37 |
Execution timed out |
1091 ms |
16948 KB |
Time limit exceeded |
38 |
Halted |
0 ms |
0 KB |
- |