#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 5000
#define N_ (N << 5)
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
int *ej[N_], *ew[N_], eo[N_], n_;
void append(int i, int j, int w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
long long dd[N_]; int 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_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 ww[5];
int n, m, k, q, g, i, j, b, o, w;
long long d, d_;
scanf("%d%d%d", &n, &m, &k), n_ = n << 5;
for (i = 0; i < n_; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
ew[i] = (int *) malloc(2 * sizeof *ew[i]);
}
memset(dd, 0x3f, n_ * sizeof *dd);
while (k--) {
scanf("%d", &i);
dd[i << 5 | 0] = 0, pq_add_last(i << 5 | 0);
}
while (m--) {
scanf("%d%d%d", &i, &j, &w);
for (b = 0; b < 1 << 5; b++) {
append(j << 5 | b, i << 5 | b, w);
for (g = 0; g < 5; g++)
if ((b & 1 << g) == 0)
append(j << 5 | b, i << 5 | b | 1 << g, w / 10 * (9 - g));
}
}
while (cnt) {
i = pq_remove_first();
for (o = eo[i]; o--; ) {
j = ej[i][o], w = ew[i][o], d = dd[i] + w;
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d, pq_up(j);
}
}
}
scanf("%d", &q);
while (q--) {
scanf("%d", &i);
for (g = 0; g < 5; g++)
scanf("%d", &ww[g]);
d_ = INF;
for (b = 0; b < 1 << 5; b++) {
d = dd[i << 5 | b];
for (g = 0; g < 5; g++)
if ((b & 1 << g) != 0)
d = ww[g] == -1 ? INF : min(d + ww[g], INF);
d_ = min(d_, d);
}
if (d_ == INF)
d_ = -1;
printf("%lld\n", d_);
}
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:16:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
16 | if (o >= 2 && (o & o - 1) == 0) {
| ~~^~~
Main.c: In function 'main':
Main.c:65:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d%d%d", &n, &m, &k), n_ = n << 5;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:72:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d", &i);
| ^~~~~~~~~~~~~~~
Main.c:76:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf("%d%d%d", &i, &j, &w);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:95:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
95 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
Main.c:97:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | scanf("%d", &i);
| ^~~~~~~~~~~~~~~
Main.c:99:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | scanf("%d", &ww[g]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
23440 KB |
Output is correct |
2 |
Correct |
38 ms |
16516 KB |
Output is correct |
3 |
Correct |
79 ms |
22660 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
23440 KB |
Output is correct |
2 |
Correct |
38 ms |
16516 KB |
Output is correct |
3 |
Correct |
79 ms |
22660 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
77 ms |
23476 KB |
Output is correct |
7 |
Correct |
30 ms |
16616 KB |
Output is correct |
8 |
Correct |
78 ms |
22736 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
23440 KB |
Output is correct |
2 |
Correct |
38 ms |
16516 KB |
Output is correct |
3 |
Correct |
79 ms |
22660 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
77 ms |
23476 KB |
Output is correct |
7 |
Correct |
30 ms |
16616 KB |
Output is correct |
8 |
Correct |
78 ms |
22736 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
82 ms |
23540 KB |
Output is correct |
12 |
Correct |
34 ms |
16568 KB |
Output is correct |
13 |
Correct |
79 ms |
22692 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
23440 KB |
Output is correct |
2 |
Correct |
38 ms |
16516 KB |
Output is correct |
3 |
Correct |
79 ms |
22660 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
39 ms |
22732 KB |
Output is correct |
7 |
Correct |
36 ms |
16808 KB |
Output is correct |
8 |
Correct |
80 ms |
22720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
23440 KB |
Output is correct |
2 |
Correct |
38 ms |
16516 KB |
Output is correct |
3 |
Correct |
79 ms |
22660 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
77 ms |
23476 KB |
Output is correct |
7 |
Correct |
30 ms |
16616 KB |
Output is correct |
8 |
Correct |
78 ms |
22736 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
39 ms |
22732 KB |
Output is correct |
12 |
Correct |
36 ms |
16808 KB |
Output is correct |
13 |
Correct |
80 ms |
22720 KB |
Output is correct |
14 |
Correct |
79 ms |
23372 KB |
Output is correct |
15 |
Correct |
31 ms |
16560 KB |
Output is correct |
16 |
Correct |
79 ms |
22784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
23456 KB |
Output is correct |
2 |
Correct |
83 ms |
23464 KB |
Output is correct |
3 |
Correct |
47 ms |
16636 KB |
Output is correct |
4 |
Correct |
79 ms |
22812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1876 KB |
Output is correct |
2 |
Correct |
4 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
680 KB |
Output is correct |
4 |
Correct |
3 ms |
1492 KB |
Output is correct |
5 |
Correct |
3 ms |
1620 KB |
Output is correct |
6 |
Correct |
1 ms |
680 KB |
Output is correct |
7 |
Correct |
4 ms |
1876 KB |
Output is correct |
8 |
Correct |
4 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
23440 KB |
Output is correct |
2 |
Correct |
38 ms |
16516 KB |
Output is correct |
3 |
Correct |
79 ms |
22660 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
77 ms |
23476 KB |
Output is correct |
7 |
Correct |
30 ms |
16616 KB |
Output is correct |
8 |
Correct |
78 ms |
22736 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
82 ms |
23540 KB |
Output is correct |
12 |
Correct |
34 ms |
16568 KB |
Output is correct |
13 |
Correct |
79 ms |
22692 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
288 KB |
Output is correct |
16 |
Correct |
39 ms |
22732 KB |
Output is correct |
17 |
Correct |
36 ms |
16808 KB |
Output is correct |
18 |
Correct |
80 ms |
22720 KB |
Output is correct |
19 |
Correct |
79 ms |
23372 KB |
Output is correct |
20 |
Correct |
31 ms |
16560 KB |
Output is correct |
21 |
Correct |
79 ms |
22784 KB |
Output is correct |
22 |
Correct |
77 ms |
23456 KB |
Output is correct |
23 |
Correct |
83 ms |
23464 KB |
Output is correct |
24 |
Correct |
47 ms |
16636 KB |
Output is correct |
25 |
Correct |
79 ms |
22812 KB |
Output is correct |
26 |
Correct |
4 ms |
1876 KB |
Output is correct |
27 |
Correct |
4 ms |
1876 KB |
Output is correct |
28 |
Correct |
1 ms |
680 KB |
Output is correct |
29 |
Correct |
3 ms |
1492 KB |
Output is correct |
30 |
Correct |
3 ms |
1620 KB |
Output is correct |
31 |
Correct |
1 ms |
680 KB |
Output is correct |
32 |
Correct |
4 ms |
1876 KB |
Output is correct |
33 |
Correct |
4 ms |
1876 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
292 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
84 ms |
23608 KB |
Output is correct |
38 |
Correct |
91 ms |
23620 KB |
Output is correct |
39 |
Correct |
78 ms |
23508 KB |
Output is correct |
40 |
Correct |
13 ms |
14120 KB |
Output is correct |
41 |
Correct |
9 ms |
14644 KB |
Output is correct |
42 |
Correct |
83 ms |
22896 KB |
Output is correct |
43 |
Correct |
94 ms |
22888 KB |
Output is correct |
44 |
Correct |
81 ms |
22680 KB |
Output is correct |
45 |
Correct |
43 ms |
16624 KB |
Output is correct |