#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 5e4 + 5;
const int mxK = 5;
const int lg = 16;
const long long INF = 1e18;
long long dist[mxN][lg][mxK][mxK];
int k, n, m, q;
void testCase() {
for (int i = 0; i < mxN; i++) {
for (int j = 0; j < lg; j++) {
for (int x = 0; x < mxK; x++) {
for (int l = 0; l < mxK; l++) {
dist[i][j][x][l] = INF;
}
}
}
}
cin >> k >> n >> m >> q;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
dist[a / k][0][a % k][b % k] = min(dist[a / k][0][a % k][b % k], 1LL * c);
}
for (int j = 1; j < lg; j++) {
for (int i = 0; i < n; i++) {
for (int a = 0; a < k; a++) {
for (int b = 0; b < k; b++) {
for (int c = 0; c < k; c++) {
if (i + (1 << j) - 1 >= n) continue;
if (dist[i][j - 1][a][c] == INF) continue;
if (dist[i + (1 << (j - 1))][j - 1][c][b] == INF) continue;
dist[i][j][a][b] = min(dist[i][j][a][b], dist[i][j - 1][a][c] + dist[i + (1 << (j - 1))][j - 1][c][b]);
}
}
}
}
}
while (q--) {
int a, b;
cin >> a >> b;
int aa = a / k;
int bb = b / k;
long long ans[mxK];
for (int i = 0; i < k; i++) ans[i] = INF;
ans[a % k] = 0;
for (int j = lg - 1; j >= 0; j--) {
if (aa + (1 << j) <= bb) {
long long new_ans[mxK];
for (int i = 0; i < k; i++) new_ans[i] = INF;
for (int from = 0; from < k; from++) {
for (int to = 0; to < k; to++) {
if (ans[from] == INF) continue;
if (dist[aa][j][from][to] == INF) continue;
new_ans[to] = min(new_ans[to], ans[from] + dist[aa][j][from][to]);
}
}
for (int i = 0; i < k; i++) ans[i] = new_ans[i];
aa += (1 << j);
}
}
long long out = ans[b % k];
if (aa > bb || out == INF) out = -1;
cout << out << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
156876 KB |
Output is correct |
2 |
Correct |
65 ms |
156816 KB |
Output is correct |
3 |
Correct |
66 ms |
156776 KB |
Output is correct |
4 |
Correct |
62 ms |
156776 KB |
Output is correct |
5 |
Correct |
65 ms |
156840 KB |
Output is correct |
6 |
Correct |
64 ms |
156788 KB |
Output is correct |
7 |
Correct |
63 ms |
156848 KB |
Output is correct |
8 |
Correct |
99 ms |
157780 KB |
Output is correct |
9 |
Correct |
102 ms |
157700 KB |
Output is correct |
10 |
Correct |
92 ms |
157000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
156880 KB |
Output is correct |
2 |
Correct |
65 ms |
156800 KB |
Output is correct |
3 |
Correct |
67 ms |
156876 KB |
Output is correct |
4 |
Correct |
67 ms |
156864 KB |
Output is correct |
5 |
Correct |
64 ms |
156856 KB |
Output is correct |
6 |
Correct |
64 ms |
156852 KB |
Output is correct |
7 |
Correct |
68 ms |
156940 KB |
Output is correct |
8 |
Correct |
69 ms |
157028 KB |
Output is correct |
9 |
Correct |
105 ms |
157756 KB |
Output is correct |
10 |
Correct |
232 ms |
159124 KB |
Output is correct |
11 |
Correct |
160 ms |
158548 KB |
Output is correct |
12 |
Correct |
199 ms |
158192 KB |
Output is correct |
13 |
Correct |
244 ms |
159260 KB |
Output is correct |
14 |
Correct |
160 ms |
158316 KB |
Output is correct |
15 |
Correct |
253 ms |
158056 KB |
Output is correct |
16 |
Correct |
233 ms |
158084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
156864 KB |
Output is correct |
2 |
Correct |
78 ms |
156816 KB |
Output is correct |
3 |
Correct |
67 ms |
156860 KB |
Output is correct |
4 |
Correct |
72 ms |
156776 KB |
Output is correct |
5 |
Correct |
64 ms |
156788 KB |
Output is correct |
6 |
Correct |
63 ms |
156876 KB |
Output is correct |
7 |
Correct |
75 ms |
156876 KB |
Output is correct |
8 |
Correct |
73 ms |
156852 KB |
Output is correct |
9 |
Correct |
65 ms |
156992 KB |
Output is correct |
10 |
Correct |
103 ms |
157628 KB |
Output is correct |
11 |
Correct |
162 ms |
158404 KB |
Output is correct |
12 |
Correct |
215 ms |
159164 KB |
Output is correct |
13 |
Correct |
208 ms |
159288 KB |
Output is correct |
14 |
Correct |
212 ms |
158684 KB |
Output is correct |
15 |
Correct |
230 ms |
158132 KB |
Output is correct |
16 |
Correct |
259 ms |
158132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
156864 KB |
Output is correct |
2 |
Correct |
78 ms |
156816 KB |
Output is correct |
3 |
Correct |
67 ms |
156860 KB |
Output is correct |
4 |
Correct |
72 ms |
156776 KB |
Output is correct |
5 |
Correct |
64 ms |
156788 KB |
Output is correct |
6 |
Correct |
63 ms |
156876 KB |
Output is correct |
7 |
Correct |
75 ms |
156876 KB |
Output is correct |
8 |
Correct |
73 ms |
156852 KB |
Output is correct |
9 |
Correct |
65 ms |
156992 KB |
Output is correct |
10 |
Correct |
103 ms |
157628 KB |
Output is correct |
11 |
Correct |
162 ms |
158404 KB |
Output is correct |
12 |
Correct |
215 ms |
159164 KB |
Output is correct |
13 |
Correct |
208 ms |
159288 KB |
Output is correct |
14 |
Correct |
212 ms |
158684 KB |
Output is correct |
15 |
Correct |
230 ms |
158132 KB |
Output is correct |
16 |
Correct |
259 ms |
158132 KB |
Output is correct |
17 |
Correct |
156 ms |
158384 KB |
Output is correct |
18 |
Correct |
66 ms |
156752 KB |
Output is correct |
19 |
Correct |
67 ms |
156856 KB |
Output is correct |
20 |
Correct |
64 ms |
156844 KB |
Output is correct |
21 |
Correct |
68 ms |
156772 KB |
Output is correct |
22 |
Correct |
80 ms |
156952 KB |
Output is correct |
23 |
Correct |
76 ms |
156872 KB |
Output is correct |
24 |
Correct |
66 ms |
157000 KB |
Output is correct |
25 |
Correct |
71 ms |
156852 KB |
Output is correct |
26 |
Correct |
72 ms |
156844 KB |
Output is correct |
27 |
Correct |
98 ms |
157644 KB |
Output is correct |
28 |
Correct |
251 ms |
159160 KB |
Output is correct |
29 |
Correct |
235 ms |
159304 KB |
Output is correct |
30 |
Correct |
195 ms |
158620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
156876 KB |
Output is correct |
2 |
Correct |
65 ms |
156816 KB |
Output is correct |
3 |
Correct |
66 ms |
156776 KB |
Output is correct |
4 |
Correct |
62 ms |
156776 KB |
Output is correct |
5 |
Correct |
65 ms |
156840 KB |
Output is correct |
6 |
Correct |
64 ms |
156788 KB |
Output is correct |
7 |
Correct |
63 ms |
156848 KB |
Output is correct |
8 |
Correct |
99 ms |
157780 KB |
Output is correct |
9 |
Correct |
102 ms |
157700 KB |
Output is correct |
10 |
Correct |
92 ms |
157000 KB |
Output is correct |
11 |
Correct |
169 ms |
156880 KB |
Output is correct |
12 |
Correct |
65 ms |
156800 KB |
Output is correct |
13 |
Correct |
67 ms |
156876 KB |
Output is correct |
14 |
Correct |
67 ms |
156864 KB |
Output is correct |
15 |
Correct |
64 ms |
156856 KB |
Output is correct |
16 |
Correct |
64 ms |
156852 KB |
Output is correct |
17 |
Correct |
68 ms |
156940 KB |
Output is correct |
18 |
Correct |
69 ms |
157028 KB |
Output is correct |
19 |
Correct |
105 ms |
157756 KB |
Output is correct |
20 |
Correct |
232 ms |
159124 KB |
Output is correct |
21 |
Correct |
160 ms |
158548 KB |
Output is correct |
22 |
Correct |
199 ms |
158192 KB |
Output is correct |
23 |
Correct |
244 ms |
159260 KB |
Output is correct |
24 |
Correct |
160 ms |
158316 KB |
Output is correct |
25 |
Correct |
253 ms |
158056 KB |
Output is correct |
26 |
Correct |
233 ms |
158084 KB |
Output is correct |
27 |
Correct |
65 ms |
156864 KB |
Output is correct |
28 |
Correct |
78 ms |
156816 KB |
Output is correct |
29 |
Correct |
67 ms |
156860 KB |
Output is correct |
30 |
Correct |
72 ms |
156776 KB |
Output is correct |
31 |
Correct |
64 ms |
156788 KB |
Output is correct |
32 |
Correct |
63 ms |
156876 KB |
Output is correct |
33 |
Correct |
75 ms |
156876 KB |
Output is correct |
34 |
Correct |
73 ms |
156852 KB |
Output is correct |
35 |
Correct |
65 ms |
156992 KB |
Output is correct |
36 |
Correct |
103 ms |
157628 KB |
Output is correct |
37 |
Correct |
162 ms |
158404 KB |
Output is correct |
38 |
Correct |
215 ms |
159164 KB |
Output is correct |
39 |
Correct |
208 ms |
159288 KB |
Output is correct |
40 |
Correct |
212 ms |
158684 KB |
Output is correct |
41 |
Correct |
230 ms |
158132 KB |
Output is correct |
42 |
Correct |
259 ms |
158132 KB |
Output is correct |
43 |
Correct |
156 ms |
158384 KB |
Output is correct |
44 |
Correct |
66 ms |
156752 KB |
Output is correct |
45 |
Correct |
67 ms |
156856 KB |
Output is correct |
46 |
Correct |
64 ms |
156844 KB |
Output is correct |
47 |
Correct |
68 ms |
156772 KB |
Output is correct |
48 |
Correct |
80 ms |
156952 KB |
Output is correct |
49 |
Correct |
76 ms |
156872 KB |
Output is correct |
50 |
Correct |
66 ms |
157000 KB |
Output is correct |
51 |
Correct |
71 ms |
156852 KB |
Output is correct |
52 |
Correct |
72 ms |
156844 KB |
Output is correct |
53 |
Correct |
98 ms |
157644 KB |
Output is correct |
54 |
Correct |
251 ms |
159160 KB |
Output is correct |
55 |
Correct |
235 ms |
159304 KB |
Output is correct |
56 |
Correct |
195 ms |
158620 KB |
Output is correct |
57 |
Correct |
277 ms |
160144 KB |
Output is correct |
58 |
Correct |
105 ms |
157700 KB |
Output is correct |
59 |
Correct |
165 ms |
158656 KB |
Output is correct |