#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int kN = 5e4;
const int kLog = 15;
const int kK = 5;
int res[kK][kK], ret[kK][kK], dp[kN][1 + kLog][kK][kK];
void minSelf(int &x, int y) {
if (y < x) {
x = y;
}
}
void combine(int n, int a[kK][kK], int b[kK][kK], int c[kK][kK]) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
minSelf(a[i][j], b[i][k] + c[k][j]);
}
}
}
}
void testCase() {
int k, n, m, q;
cin >> k >> n >> m >> q;
for (int i = 0; i <= (n - 1) / k; ++i) {
for (int j = 0; (1 << j) <= (n - 1) / k; ++j) {
for (int a = 0; a < k; ++a) {
for (int b = 0; b < k; ++b) {
dp[i][j][a][b] = INF;
}
}
}
}
for (int i = 0; i < m; ++i) {
int a, b, t;
cin >> a >> b >> t;
dp[a / k][0][a % k][b % k] = t;
}
for (int j = 1; (1 << j) <= (n - 1) / k; ++j) {
for (int i = 0; i + (1 << j) - 1 <= (n - 1) / k; ++i) {
combine(k, dp[i][j], dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
}
for (int qq = 0; qq < q; ++qq) {
int a, b;
cin >> a >> b;
for (int i = 0; i < k; ++i) {
for (int j = 0; j < k; ++j) {
if (i == j) {
res[i][i] = 0;
} else {
res[i][j] = INF;
}
}
}
int s = a / k, t = b / k;
for (int i = kLog; i >= 0; --i) {
if (s + (1 << i) <= t) {
for (int l = 0; l < k; ++l) {
for (int c = 0; c < k; ++c) {
ret[l][c] = res[l][c];
res[l][c] = INF;
}
}
combine(k, res, ret, dp[s][i]);
s += (1 << i);
}
}
if (res[a % k][b % k] == INF) {
cout << "-1\n";
} else {
cout << res[a % k][b % k] << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
79428 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
1868 KB |
Output is correct |
6 |
Correct |
2 ms |
1868 KB |
Output is correct |
7 |
Correct |
2 ms |
1868 KB |
Output is correct |
8 |
Correct |
67 ms |
79416 KB |
Output is correct |
9 |
Correct |
65 ms |
79428 KB |
Output is correct |
10 |
Correct |
49 ms |
78724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
41044 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
4 ms |
1868 KB |
Output is correct |
8 |
Correct |
5 ms |
1188 KB |
Output is correct |
9 |
Correct |
65 ms |
79456 KB |
Output is correct |
10 |
Correct |
86 ms |
28688 KB |
Output is correct |
11 |
Correct |
83 ms |
41156 KB |
Output is correct |
12 |
Correct |
70 ms |
27796 KB |
Output is correct |
13 |
Correct |
64 ms |
12100 KB |
Output is correct |
14 |
Correct |
41 ms |
17356 KB |
Output is correct |
15 |
Correct |
47 ms |
10820 KB |
Output is correct |
16 |
Correct |
47 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
1868 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
3 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
60 ms |
79244 KB |
Output is correct |
11 |
Correct |
61 ms |
40960 KB |
Output is correct |
12 |
Correct |
71 ms |
28492 KB |
Output is correct |
13 |
Correct |
80 ms |
28748 KB |
Output is correct |
14 |
Correct |
80 ms |
28176 KB |
Output is correct |
15 |
Correct |
45 ms |
10936 KB |
Output is correct |
16 |
Correct |
43 ms |
10880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
1868 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
3 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
60 ms |
79244 KB |
Output is correct |
11 |
Correct |
61 ms |
40960 KB |
Output is correct |
12 |
Correct |
71 ms |
28492 KB |
Output is correct |
13 |
Correct |
80 ms |
28748 KB |
Output is correct |
14 |
Correct |
80 ms |
28176 KB |
Output is correct |
15 |
Correct |
45 ms |
10936 KB |
Output is correct |
16 |
Correct |
43 ms |
10880 KB |
Output is correct |
17 |
Correct |
68 ms |
41008 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
4 ms |
1872 KB |
Output is correct |
24 |
Correct |
3 ms |
1152 KB |
Output is correct |
25 |
Correct |
5 ms |
716 KB |
Output is correct |
26 |
Correct |
4 ms |
720 KB |
Output is correct |
27 |
Correct |
66 ms |
79572 KB |
Output is correct |
28 |
Correct |
73 ms |
28616 KB |
Output is correct |
29 |
Correct |
77 ms |
28840 KB |
Output is correct |
30 |
Correct |
68 ms |
28180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
79428 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
1868 KB |
Output is correct |
6 |
Correct |
2 ms |
1868 KB |
Output is correct |
7 |
Correct |
2 ms |
1868 KB |
Output is correct |
8 |
Correct |
67 ms |
79416 KB |
Output is correct |
9 |
Correct |
65 ms |
79428 KB |
Output is correct |
10 |
Correct |
49 ms |
78724 KB |
Output is correct |
11 |
Correct |
69 ms |
41044 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
4 ms |
1868 KB |
Output is correct |
18 |
Correct |
5 ms |
1188 KB |
Output is correct |
19 |
Correct |
65 ms |
79456 KB |
Output is correct |
20 |
Correct |
86 ms |
28688 KB |
Output is correct |
21 |
Correct |
83 ms |
41156 KB |
Output is correct |
22 |
Correct |
70 ms |
27796 KB |
Output is correct |
23 |
Correct |
64 ms |
12100 KB |
Output is correct |
24 |
Correct |
41 ms |
17356 KB |
Output is correct |
25 |
Correct |
47 ms |
10820 KB |
Output is correct |
26 |
Correct |
47 ms |
10840 KB |
Output is correct |
27 |
Correct |
0 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
204 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
204 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
1868 KB |
Output is correct |
33 |
Correct |
1 ms |
1100 KB |
Output is correct |
34 |
Correct |
3 ms |
588 KB |
Output is correct |
35 |
Correct |
2 ms |
588 KB |
Output is correct |
36 |
Correct |
60 ms |
79244 KB |
Output is correct |
37 |
Correct |
61 ms |
40960 KB |
Output is correct |
38 |
Correct |
71 ms |
28492 KB |
Output is correct |
39 |
Correct |
80 ms |
28748 KB |
Output is correct |
40 |
Correct |
80 ms |
28176 KB |
Output is correct |
41 |
Correct |
45 ms |
10936 KB |
Output is correct |
42 |
Correct |
43 ms |
10880 KB |
Output is correct |
43 |
Correct |
68 ms |
41008 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
0 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
204 KB |
Output is correct |
49 |
Correct |
4 ms |
1872 KB |
Output is correct |
50 |
Correct |
3 ms |
1152 KB |
Output is correct |
51 |
Correct |
5 ms |
716 KB |
Output is correct |
52 |
Correct |
4 ms |
720 KB |
Output is correct |
53 |
Correct |
66 ms |
79572 KB |
Output is correct |
54 |
Correct |
73 ms |
28616 KB |
Output is correct |
55 |
Correct |
77 ms |
28840 KB |
Output is correct |
56 |
Correct |
68 ms |
28180 KB |
Output is correct |
57 |
Correct |
113 ms |
23184 KB |
Output is correct |
58 |
Correct |
81 ms |
79456 KB |
Output is correct |
59 |
Correct |
78 ms |
41212 KB |
Output is correct |