Submission #905150

#TimeUsernameProblemLanguageResultExecution timeMemory
905150rainboyBrackets (CPSPC17_brackets)C11
100 / 100
34 ms1120 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...