Submission #832773

#TimeUsernameProblemLanguageResultExecution timeMemory
832773rainboyRelay Marathon (NOI20_relaymarathon)C11
100 / 100
1858 ms120776 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 100000 #define M 3000000 #define INF 0x3f3f3f3f int min(int a, int b) { return a < b ? a : b; } char special[N]; int n; int ii[M], jj[M], ww[M], m; int *eh[N], eo[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; } int dd[N], rr[N], iq[N + 1], pq[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(int i) { pq[i] = ++cnt, pq_up(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 dijkstra() { int i, o; memset(pq, 0, n * sizeof *pq), cnt = 0; for (i = 0; i < n; i++) pq_add(i); while (cnt) { i = pq_remove_first(); for (o = eo[i]; o--; ) { int h = eh[i][o], j = i ^ ii[h] ^ jj[h], d = dd[i] + ww[h]; if (dd[j] > d) dd[j] = d, rr[j] = rr[i], pq_up(j); } } } int main() { int k, h, i, i_, i1, j, j_, j1, w, d, d1, d2, di1, di2, dj1, dj2, ans; scanf("%d%d%d", &n, &m, &k); 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, &w), i--, j--; ii[h] = i, jj[h] = j, ww[h] = w; append(i, h), append(j, h); } while (k--) { scanf("%d", &i), i--; special[i] = 1; } for (i = 0; i < n; i++) dd[i] = special[i] ? 0 : INF, rr[i] = i; dijkstra(); d1 = INF, i_ = j_ = -1; for (h = 0; h < m; h++) { i = ii[h], j = jj[h]; if (rr[i] != rr[j] && d1 > (d = dd[i] + ww[h] + dd[j])) d1 = d, i_ = rr[i], j_ = rr[j]; } for (i = 0; i < n; i++) dd[i] = special[i] && i != i_ && i != j_ ? 0 : INF, rr[i] = i; dijkstra(); d2 = INF; for (h = 0; h < m; h++) { i = ii[h], j = jj[h]; if (rr[i] != rr[j]) d2 = min(d2, dd[i] + ww[h] + dd[j]); } ans = d1 + d2; for (i = 0; i < n; i++) dd[i] = i == i_ ? 0 : INF, rr[i] = i; dijkstra(); di1 = di2 = INF, j1 = -1; for (j = 0; j < n; j++) if (special[j] && j != i_ && j != j_) { if (di1 > dd[j]) di2 = di1, di1 = dd[j], j1 = j; else if (di2 > dd[j]) di2 = dd[j]; } for (i = 0; i < n; i++) dd[i] = i == j_ ? 0 : INF, rr[i] = i; dijkstra(); dj1 = dj2 = INF, i1 = -1; for (i = 0; i < n; i++) if (special[i] && i != i_ && i != j_) { if (dj1 > dd[i]) dj2 = dj1, dj1 = dd[i], i1 = i; else if (dj2 > dd[i]) dj2 = dd[i]; } if (i1 != j1) ans = min(ans, di1 + dj1); else ans = min(ans, min(di1 + dj2, di2 + dj1)); printf("%d\n", ans); return 0; }

Compilation message (stderr)

RelayMarathon.c: In function 'append':
RelayMarathon.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
RelayMarathon.c: In function 'main':
RelayMarathon.c:81:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |  scanf("%d%d%d", &n, &m, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.c:85:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |   scanf("%d%d%d", &i, &j, &w), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.c:90:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |   scanf("%d", &i), 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...