Submission #680410

#TimeUsernameProblemLanguageResultExecution timeMemory
680410rainboyWild Boar (JOI18_wild_boar)C11
62 / 100
4006 ms189380 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 2000 #define M 2000 #define L 100000 #define INF 0x3f3f3f3f3f3f3f3fLL long long min(long long a, long long b) { return a < b ? a : b; } int ij[M * 2], cc[M], m; int *eh[N], eo[N], n; void append(int i, int h) { int o = eo[i]++; if (o >= 2 && (o & o - 1) == 0) eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]); eh[i][o] = h; } long long *dd_; int iq[M * 2 + 1], pq[M * 2], 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; } void solve(long long *dd, int u) { static int kk[N]; int h, i, o; memset(dd, 0x3f, m * 2 * sizeof *dd), memset(kk, 0, n * sizeof *kk); dd_ = dd, dd[u] = cc[u >> 1], pq_add_last(u); while (cnt) { h = pq_remove_first(), i = ij[h ^ 1]; if (kk[i] < 2) { kk[i]++; for (o = eo[i]; o--; ) { int h_ = eh[i][o]; long long d = dd[h] + cc[h_ >> 1]; if ((h ^ h_) != 1 && dd[h_] > d) { if (dd[h_] == INF) pq_add_last(h_); dd[h_] = d, pq_up(h_); } } } } } int main() { static long long dd[M * 2][M * 2], dp[M * 2], dq[M * 2]; static int uu1[M * 2][N], uu2[M * 2][N], ii[L]; int q, l, g, h, i, j, t, t1, t2, u, v; long long ans; scanf("%d%d%d%d", &n, &m, &q, &l); for (i = 0; i < n; i++) eh[i] = (int *) malloc(2 * sizeof *eh[i]); for (h = 0; h < m; h++) { scanf("%d%d%d", &i, &j, &cc[h]), i--, j--; ij[h << 1 | 0] = i, ij[h << 1 | 1] = j; append(i, h << 1 | 0), append(j, h << 1 | 1); } for (u = 0; u < m * 2; u++) solve(dd[u], u); for (g = 0; g < l; g++) scanf("%d", &ii[g]), ii[g]--; while (q--) { scanf("%d%d", &g, &i), g--, i--; ii[g] = i; } for (v = 0; v < m * 2; v++) { memset(uu1[v], -1, n * sizeof *uu1[v]), memset(uu2[v], -1, n * sizeof *uu2[v]); for (u = 0; u < m * 2; u++) if (dd[u][v] != INF) { i = ij[u]; if (uu1[v][i] == -1 || dd[uu1[v][i]][v] > dd[u][v]) uu2[v][i] = uu1[v][i], uu1[v][i] = u; else if (uu2[v][i] == -1 || dd[uu2[v][i]][v] > dd[u][v]) uu2[v][i] = u; } } for (g = 0; g + 1 < l; g++) { i = ii[g], j = ii[g + 1]; memset(dq, 0x3f, m * 2 * sizeof *dq); t1 = t2 = -1; for (t = 0; t < m * 2; t++) if (dp[t] != INF) { if (t1 == -1 || dp[t1] > dp[t]) t2 = t1, t1 = t; else if (t2 == -1 || dp[t2] > dp[t]) t2 = t; } for (v = 0; v < m * 2; v++) if (ij[v ^ 1] == j && (u = uu1[v][i]) != -1) { if (g == 0) dq[v] = min(dq[v], dd[u][v]); else { if (t1 != -1 && (t1 ^ u) != 1) dq[v] = min(dq[v], dp[t1] + dd[u][v]); else if (t2 != -1) dq[v] = min(dq[v], dp[t2] + dd[u][v]); if ((u = uu2[v][i]) != -1) { if (t1 != -1 && (t1 ^ u) != 1) dq[v] = min(dq[v], dp[t1] + dd[u][v]); else if (t2 != -1) dq[v] = min(dq[v], dp[t2] + dd[u][v]); } } } memcpy(dp, dq, m * 2 * sizeof *dq); } ans = INF; for (v = 0; v < m * 2; v++) ans = min(ans, dp[v]); printf("%lld\n", ans == INF ? -1 : ans); return 0; }

Compilation message (stderr)

wild_boar.c: In function 'append':
wild_boar.c:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   18 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
wild_boar.c: In function 'main':
wild_boar.c:90:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |  scanf("%d%d%d%d", &n, &m, &q, &l);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.c:94:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |   scanf("%d%d%d", &i, &j, &cc[h]), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.c:101:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   scanf("%d", &ii[g]), ii[g]--;
      |   ^~~~~~~~~~~~~~~~~~~
wild_boar.c:103:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |   scanf("%d%d", &g, &i), g--, i--;
      |   ^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...