답안 #832773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
832773 2023-08-21T14:48:00 Z rainboy Relay Marathon (NOI20_relaymarathon) C
100 / 100
1858 ms 120776 KB
#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

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--;
      |   ^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 300 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 300 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 300 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 300 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 308 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 1 ms 300 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 296 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 424 KB Output is correct
37 Correct 2 ms 596 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 19 ms 3376 KB Output is correct
40 Correct 4 ms 980 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 4 ms 980 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 292 KB Output is correct
46 Correct 23 ms 3888 KB Output is correct
47 Correct 3 ms 748 KB Output is correct
48 Correct 16 ms 3236 KB Output is correct
49 Correct 20 ms 3448 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 308 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 10 ms 1976 KB Output is correct
54 Correct 20 ms 3592 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 26 ms 4176 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 7140 KB Output is correct
2 Correct 11 ms 6324 KB Output is correct
3 Correct 1436 ms 62056 KB Output is correct
4 Correct 566 ms 28472 KB Output is correct
5 Correct 117 ms 9224 KB Output is correct
6 Correct 127 ms 8336 KB Output is correct
7 Correct 125 ms 9760 KB Output is correct
8 Correct 56 ms 7160 KB Output is correct
9 Correct 98 ms 7972 KB Output is correct
10 Correct 90 ms 7372 KB Output is correct
11 Correct 1567 ms 65020 KB Output is correct
12 Correct 78 ms 7572 KB Output is correct
13 Correct 361 ms 19136 KB Output is correct
14 Correct 196 ms 9856 KB Output is correct
15 Correct 1534 ms 63720 KB Output is correct
16 Correct 41 ms 6868 KB Output is correct
17 Correct 978 ms 44488 KB Output is correct
18 Correct 11 ms 6356 KB Output is correct
19 Correct 1605 ms 66304 KB Output is correct
20 Correct 126 ms 9380 KB Output is correct
21 Correct 109 ms 8768 KB Output is correct
22 Correct 68 ms 7384 KB Output is correct
23 Correct 21 ms 6784 KB Output is correct
24 Correct 1022 ms 49408 KB Output is correct
25 Correct 93 ms 8120 KB Output is correct
26 Correct 59 ms 7116 KB Output is correct
27 Correct 107 ms 7696 KB Output is correct
28 Correct 22 ms 6484 KB Output is correct
29 Correct 127 ms 9652 KB Output is correct
30 Correct 318 ms 15332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 300 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 300 KB Output is correct
22 Correct 0 ms 296 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 308 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 1 ms 300 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 296 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 424 KB Output is correct
37 Correct 2 ms 596 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 19 ms 3376 KB Output is correct
40 Correct 4 ms 980 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 4 ms 980 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 292 KB Output is correct
46 Correct 23 ms 3888 KB Output is correct
47 Correct 3 ms 748 KB Output is correct
48 Correct 16 ms 3236 KB Output is correct
49 Correct 20 ms 3448 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 308 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 10 ms 1976 KB Output is correct
54 Correct 20 ms 3592 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 26 ms 4176 KB Output is correct
59 Correct 0 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 64 ms 7140 KB Output is correct
62 Correct 11 ms 6324 KB Output is correct
63 Correct 1436 ms 62056 KB Output is correct
64 Correct 566 ms 28472 KB Output is correct
65 Correct 117 ms 9224 KB Output is correct
66 Correct 127 ms 8336 KB Output is correct
67 Correct 125 ms 9760 KB Output is correct
68 Correct 56 ms 7160 KB Output is correct
69 Correct 98 ms 7972 KB Output is correct
70 Correct 90 ms 7372 KB Output is correct
71 Correct 1567 ms 65020 KB Output is correct
72 Correct 78 ms 7572 KB Output is correct
73 Correct 361 ms 19136 KB Output is correct
74 Correct 196 ms 9856 KB Output is correct
75 Correct 1534 ms 63720 KB Output is correct
76 Correct 41 ms 6868 KB Output is correct
77 Correct 978 ms 44488 KB Output is correct
78 Correct 11 ms 6356 KB Output is correct
79 Correct 1605 ms 66304 KB Output is correct
80 Correct 126 ms 9380 KB Output is correct
81 Correct 109 ms 8768 KB Output is correct
82 Correct 68 ms 7384 KB Output is correct
83 Correct 21 ms 6784 KB Output is correct
84 Correct 1022 ms 49408 KB Output is correct
85 Correct 93 ms 8120 KB Output is correct
86 Correct 59 ms 7116 KB Output is correct
87 Correct 107 ms 7696 KB Output is correct
88 Correct 22 ms 6484 KB Output is correct
89 Correct 127 ms 9652 KB Output is correct
90 Correct 318 ms 15332 KB Output is correct
91 Correct 134 ms 9584 KB Output is correct
92 Correct 1664 ms 112960 KB Output is correct
93 Correct 214 ms 12892 KB Output is correct
94 Correct 1000 ms 64120 KB Output is correct
95 Correct 11 ms 6484 KB Output is correct
96 Correct 13 ms 6560 KB Output is correct
97 Correct 209 ms 15824 KB Output is correct
98 Correct 132 ms 9548 KB Output is correct
99 Correct 113 ms 10228 KB Output is correct
100 Correct 1858 ms 119752 KB Output is correct
101 Correct 649 ms 42736 KB Output is correct
102 Correct 607 ms 42084 KB Output is correct
103 Correct 1128 ms 79304 KB Output is correct
104 Correct 1383 ms 92648 KB Output is correct
105 Correct 86 ms 8368 KB Output is correct
106 Correct 1222 ms 79960 KB Output is correct
107 Correct 303 ms 23396 KB Output is correct
108 Correct 175 ms 11876 KB Output is correct
109 Correct 18 ms 7124 KB Output is correct
110 Correct 89 ms 8112 KB Output is correct
111 Correct 184 ms 12168 KB Output is correct
112 Correct 1783 ms 119924 KB Output is correct
113 Correct 1830 ms 120776 KB Output is correct
114 Correct 175 ms 11732 KB Output is correct
115 Correct 547 ms 40196 KB Output is correct
116 Correct 1599 ms 111120 KB Output is correct
117 Correct 1107 ms 77676 KB Output is correct
118 Correct 962 ms 69392 KB Output is correct
119 Correct 1461 ms 98192 KB Output is correct
120 Correct 856 ms 57436 KB Output is correct