Submission #832753

#TimeUsernameProblemLanguageResultExecution timeMemory
832753rainboyRelay Marathon (NOI20_relaymarathon)C11
25 / 100
1180 ms106956 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], 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, pq_up(j); } } } int rr[N]; void dfs(int i, int r) { int o; if (rr[i] != -1) return; rr[i] = r; 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) dfs(j, r); } } int main() { int k, h, i, j, w, 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; dijkstra(); memset(rr, -1, n * sizeof *rr); for (i = 0; i < n; i++) if (special[i]) dfs(i, i); ans = INF; for (h = 0; h < m; h++) if (ii[h] != 0 && jj[h] != 0 && rr[ii[h]] != rr[jj[h]]) ans = min(ans, dd[ii[h]] + ww[h] + dd[jj[h]] + 1); 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:97:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |  scanf("%d%d%d", &n, &m, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.c:101:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   scanf("%d%d%d", &i, &j, &w), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.c:106:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |   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...