#include <stdio.h>
#define N 50000
#define K 5
#define Q 10000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int dd[N][K][K], ii[Q], jj[Q], ans[Q], k;
void solve(int *hh, int l, int r, int q) {
static int dd1[N][K][K], dd2[N][K][K];
int m, h, hl, hm, hr, i, j, a, b, c, tmp;
if (q == 0)
return;
m = (l + r) / 2;
hl = 0, hm = 0, hr = q;
while (hm < hr)
if (ii[hh[hm]] / k <= m && m <= jj[hh[hm]] / k)
hm++;
else if (jj[hh[hm]] / k < m) {
tmp = hh[hl], hh[hl] = hh[hm], hh[hm] = tmp;
hl++, hm++;
} else {
hr--;
tmp = hh[hm], hh[hm] = hh[hr], hh[hr] = tmp;
}
for (i = l; i <= m; i++)
for (a = 0; a < k; a++)
for (b = 0; b < k; b++)
dd1[i][a][b] = INF;
for (i = m; i <= r; i++)
for (a = 0; a < k; a++)
for (b = 0; b < k; b++)
dd2[i][a][b] = INF;
for (a = 0; a < k; a++)
dd1[m][a][a] = dd2[m][a][a] = 0;
for (i = m; i > l; i--)
for (a = 0; a < k; a++)
for (b = 0; b < k; b++) {
int x = dd1[i][a][b];
if (x != INF)
for (c = 0; c < k; c++)
dd1[i - 1][c][b] = min(dd1[i - 1][c][b], dd[i - 1][c][a] + x);
}
for (i = m; i < r; i++)
for (a = 0; a < k; a++)
for (b = 0; b < k; b++) {
int x = dd2[i][a][b];
if (x != INF)
for (c = 0; c < k; c++)
dd2[i + 1][a][c] = min(dd2[i + 1][a][c], x + dd[i][b][c]);
}
for (h = hl; h < hr; h++) {
int h_ = hh[h], x;
i = ii[h_] / k, a = ii[h_] % k, j = jj[h_] / k, b = jj[h_] % k;
x = INF;
for (c = 0; c < k; c++)
x = min(x, dd1[i][a][c] + dd2[j][c][b]);
ans[h_] = x;
}
solve(hh, l, m - 1, hl);
solve(hh + hr, m + 1, r, q - hr);
}
int main() {
static int hh[Q];
int n, m, q, h, i, j, a, b;
scanf("%d%d%d%d", &k, &n, &m, &q), n = (n + k - 1) / k;
for (i = 0; i + 1 < n; i++)
for (a = 0; a < k; a++)
for (b = 0; b < k; b++)
dd[i][a][b] = INF;
while (m--) {
int d;
scanf("%d%d%d", &i, &j, &d);
dd[i / k][i % k][j % k] = d;
}
for (h = 0; h < q; h++) {
scanf("%d%d", &ii[h], &jj[h]);
hh[h] = h;
}
solve(hh, 0, n - 1, q);
for (h = 0; h < q; h++)
printf("%d\n", ans[h] == INF ? -1 : ans[h]);
return 0;
}
Compilation message
toll.c: In function 'main':
toll.c:75:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | scanf("%d%d%d%d", &k, &n, &m, &q), n = (n + k - 1) / k;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.c:83:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d%d%d", &i, &j, &d);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.c:87:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | scanf("%d%d", &ii[h], &jj[h]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
15924 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
552 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
35 ms |
15912 KB |
Output is correct |
9 |
Correct |
30 ms |
15820 KB |
Output is correct |
10 |
Correct |
13 ms |
15116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
8200 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
288 KB |
Output is correct |
7 |
Correct |
4 ms |
684 KB |
Output is correct |
8 |
Correct |
4 ms |
680 KB |
Output is correct |
9 |
Correct |
28 ms |
13480 KB |
Output is correct |
10 |
Correct |
57 ms |
6748 KB |
Output is correct |
11 |
Correct |
43 ms |
8220 KB |
Output is correct |
12 |
Correct |
34 ms |
5740 KB |
Output is correct |
13 |
Correct |
73 ms |
4168 KB |
Output is correct |
14 |
Correct |
35 ms |
4172 KB |
Output is correct |
15 |
Correct |
30 ms |
3020 KB |
Output is correct |
16 |
Correct |
28 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
556 KB |
Output is correct |
7 |
Correct |
1 ms |
428 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
416 KB |
Output is correct |
10 |
Correct |
37 ms |
15400 KB |
Output is correct |
11 |
Correct |
57 ms |
8856 KB |
Output is correct |
12 |
Correct |
52 ms |
7020 KB |
Output is correct |
13 |
Correct |
58 ms |
7244 KB |
Output is correct |
14 |
Correct |
46 ms |
6764 KB |
Output is correct |
15 |
Correct |
37 ms |
3140 KB |
Output is correct |
16 |
Correct |
33 ms |
3172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
556 KB |
Output is correct |
7 |
Correct |
1 ms |
428 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
416 KB |
Output is correct |
10 |
Correct |
37 ms |
15400 KB |
Output is correct |
11 |
Correct |
57 ms |
8856 KB |
Output is correct |
12 |
Correct |
52 ms |
7020 KB |
Output is correct |
13 |
Correct |
58 ms |
7244 KB |
Output is correct |
14 |
Correct |
46 ms |
6764 KB |
Output is correct |
15 |
Correct |
37 ms |
3140 KB |
Output is correct |
16 |
Correct |
33 ms |
3172 KB |
Output is correct |
17 |
Correct |
49 ms |
9100 KB |
Output is correct |
18 |
Correct |
1 ms |
292 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
3 ms |
432 KB |
Output is correct |
26 |
Correct |
3 ms |
468 KB |
Output is correct |
27 |
Correct |
36 ms |
15708 KB |
Output is correct |
28 |
Correct |
68 ms |
7392 KB |
Output is correct |
29 |
Correct |
74 ms |
7532 KB |
Output is correct |
30 |
Correct |
54 ms |
7012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
15924 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
552 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
560 KB |
Output is correct |
8 |
Correct |
35 ms |
15912 KB |
Output is correct |
9 |
Correct |
30 ms |
15820 KB |
Output is correct |
10 |
Correct |
13 ms |
15116 KB |
Output is correct |
11 |
Correct |
48 ms |
8200 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
292 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
288 KB |
Output is correct |
17 |
Correct |
4 ms |
684 KB |
Output is correct |
18 |
Correct |
4 ms |
680 KB |
Output is correct |
19 |
Correct |
28 ms |
13480 KB |
Output is correct |
20 |
Correct |
57 ms |
6748 KB |
Output is correct |
21 |
Correct |
43 ms |
8220 KB |
Output is correct |
22 |
Correct |
34 ms |
5740 KB |
Output is correct |
23 |
Correct |
73 ms |
4168 KB |
Output is correct |
24 |
Correct |
35 ms |
4172 KB |
Output is correct |
25 |
Correct |
30 ms |
3020 KB |
Output is correct |
26 |
Correct |
28 ms |
3020 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
292 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
556 KB |
Output is correct |
33 |
Correct |
1 ms |
428 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
3 ms |
416 KB |
Output is correct |
36 |
Correct |
37 ms |
15400 KB |
Output is correct |
37 |
Correct |
57 ms |
8856 KB |
Output is correct |
38 |
Correct |
52 ms |
7020 KB |
Output is correct |
39 |
Correct |
58 ms |
7244 KB |
Output is correct |
40 |
Correct |
46 ms |
6764 KB |
Output is correct |
41 |
Correct |
37 ms |
3140 KB |
Output is correct |
42 |
Correct |
33 ms |
3172 KB |
Output is correct |
43 |
Correct |
49 ms |
9100 KB |
Output is correct |
44 |
Correct |
1 ms |
292 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
2 ms |
596 KB |
Output is correct |
50 |
Correct |
4 ms |
468 KB |
Output is correct |
51 |
Correct |
3 ms |
432 KB |
Output is correct |
52 |
Correct |
3 ms |
468 KB |
Output is correct |
53 |
Correct |
36 ms |
15708 KB |
Output is correct |
54 |
Correct |
68 ms |
7392 KB |
Output is correct |
55 |
Correct |
74 ms |
7532 KB |
Output is correct |
56 |
Correct |
54 ms |
7012 KB |
Output is correct |
57 |
Correct |
85 ms |
7468 KB |
Output is correct |
58 |
Correct |
36 ms |
15900 KB |
Output is correct |
59 |
Correct |
53 ms |
9480 KB |
Output is correct |