#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = (int)1e9;
int K, N, M, Q;
vector<vector<vector<vector<int>>>> dp;
signed main() {
cin >> K >> N >> M >> Q;
dp.resize((N / K + 1) + 2, vector<vector<vector<int>>>(ceil(log2(N / K)) + 3,
vector<vector<int>>(K, vector<int>(K, INF))));
for (int i = 0; i < M; i++) {
int a, b, c; cin >> a >> b >> c;
dp[a / K][0][a % K][b % K] = c;
}
for (int l = 1; (1 << (l - 1)) <= (N + K - 1) / K; l++) {
for (int g = 0; g + (1 << l) < (N + K - 1) / K; g++) {
for (int x = 0; x < K; x++) {
for (int y = 0; y < K; y++) {
for (int z = 0; z < K; z++) {
dp[g][l][x][y] = min(dp[g][l][x][y],
dp[g][l - 1][x][z] + dp[g + (1 << (l - 1))][l - 1][z][y]);
}
}
}
}
}
for (int i = 0; i < Q; i++) {
int a, b; cin >> a >> b;
vector<vector<int>> answer(K, vector<int>(K, INF));
for (int j = 0; j < K; j++) {
answer[j][j] = 0;
}
int len = b / K - a / K, lst = a / K;
for (int l = ceil(log2(len)) + 2; l >= 0; l--) {
if (lst + (1 << l) <= b / K) {
vector<vector<int>> tmp(K, vector<int>(K, INF));
for (int x = 0; x < K; x++) {
for (int y = 0; y < K; y++) {
for (int z = 0; z < K; z++) {
tmp[x][y] = min(tmp[x][y], answer[x][z] + dp[lst][l][z][y]);
}
}
}
answer = tmp;
lst += (1 << l);
}
}
if (answer[a % K][b % K] == INF) {
cout << "-1\n";
} else {
cout << answer[a % K][b % K] << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
83688 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 |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
1460 KB |
Output is correct |
6 |
Correct |
5 ms |
1364 KB |
Output is correct |
7 |
Correct |
5 ms |
1364 KB |
Output is correct |
8 |
Correct |
205 ms |
84580 KB |
Output is correct |
9 |
Correct |
223 ms |
84572 KB |
Output is correct |
10 |
Correct |
159 ms |
83932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
244 ms |
68216 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
26 ms |
1472 KB |
Output is correct |
8 |
Correct |
26 ms |
1256 KB |
Output is correct |
9 |
Correct |
215 ms |
83680 KB |
Output is correct |
10 |
Correct |
269 ms |
59696 KB |
Output is correct |
11 |
Correct |
222 ms |
68216 KB |
Output is correct |
12 |
Correct |
192 ms |
59660 KB |
Output is correct |
13 |
Correct |
182 ms |
37364 KB |
Output is correct |
14 |
Correct |
154 ms |
33900 KB |
Output is correct |
15 |
Correct |
158 ms |
37324 KB |
Output is correct |
16 |
Correct |
134 ms |
37364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1236 KB |
Output is correct |
8 |
Correct |
5 ms |
1208 KB |
Output is correct |
9 |
Correct |
5 ms |
1252 KB |
Output is correct |
10 |
Correct |
180 ms |
84408 KB |
Output is correct |
11 |
Correct |
191 ms |
69648 KB |
Output is correct |
12 |
Correct |
220 ms |
61824 KB |
Output is correct |
13 |
Correct |
230 ms |
62028 KB |
Output is correct |
14 |
Correct |
209 ms |
61360 KB |
Output is correct |
15 |
Correct |
115 ms |
38604 KB |
Output is correct |
16 |
Correct |
115 ms |
38532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1236 KB |
Output is correct |
8 |
Correct |
5 ms |
1208 KB |
Output is correct |
9 |
Correct |
5 ms |
1252 KB |
Output is correct |
10 |
Correct |
180 ms |
84408 KB |
Output is correct |
11 |
Correct |
191 ms |
69648 KB |
Output is correct |
12 |
Correct |
220 ms |
61824 KB |
Output is correct |
13 |
Correct |
230 ms |
62028 KB |
Output is correct |
14 |
Correct |
209 ms |
61360 KB |
Output is correct |
15 |
Correct |
115 ms |
38604 KB |
Output is correct |
16 |
Correct |
115 ms |
38532 KB |
Output is correct |
17 |
Correct |
212 ms |
69884 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
300 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
9 ms |
1484 KB |
Output is correct |
24 |
Correct |
9 ms |
1236 KB |
Output is correct |
25 |
Correct |
14 ms |
1252 KB |
Output is correct |
26 |
Correct |
12 ms |
1208 KB |
Output is correct |
27 |
Correct |
180 ms |
84472 KB |
Output is correct |
28 |
Correct |
243 ms |
61968 KB |
Output is correct |
29 |
Correct |
232 ms |
62076 KB |
Output is correct |
30 |
Correct |
205 ms |
61432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
83688 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 |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
1460 KB |
Output is correct |
6 |
Correct |
5 ms |
1364 KB |
Output is correct |
7 |
Correct |
5 ms |
1364 KB |
Output is correct |
8 |
Correct |
205 ms |
84580 KB |
Output is correct |
9 |
Correct |
223 ms |
84572 KB |
Output is correct |
10 |
Correct |
159 ms |
83932 KB |
Output is correct |
11 |
Correct |
244 ms |
68216 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
26 ms |
1472 KB |
Output is correct |
18 |
Correct |
26 ms |
1256 KB |
Output is correct |
19 |
Correct |
215 ms |
83680 KB |
Output is correct |
20 |
Correct |
269 ms |
59696 KB |
Output is correct |
21 |
Correct |
222 ms |
68216 KB |
Output is correct |
22 |
Correct |
192 ms |
59660 KB |
Output is correct |
23 |
Correct |
182 ms |
37364 KB |
Output is correct |
24 |
Correct |
154 ms |
33900 KB |
Output is correct |
25 |
Correct |
158 ms |
37324 KB |
Output is correct |
26 |
Correct |
134 ms |
37364 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
3 ms |
1364 KB |
Output is correct |
33 |
Correct |
3 ms |
1236 KB |
Output is correct |
34 |
Correct |
5 ms |
1208 KB |
Output is correct |
35 |
Correct |
5 ms |
1252 KB |
Output is correct |
36 |
Correct |
180 ms |
84408 KB |
Output is correct |
37 |
Correct |
191 ms |
69648 KB |
Output is correct |
38 |
Correct |
220 ms |
61824 KB |
Output is correct |
39 |
Correct |
230 ms |
62028 KB |
Output is correct |
40 |
Correct |
209 ms |
61360 KB |
Output is correct |
41 |
Correct |
115 ms |
38604 KB |
Output is correct |
42 |
Correct |
115 ms |
38532 KB |
Output is correct |
43 |
Correct |
212 ms |
69884 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
300 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
300 KB |
Output is correct |
48 |
Correct |
1 ms |
300 KB |
Output is correct |
49 |
Correct |
9 ms |
1484 KB |
Output is correct |
50 |
Correct |
9 ms |
1236 KB |
Output is correct |
51 |
Correct |
14 ms |
1252 KB |
Output is correct |
52 |
Correct |
12 ms |
1208 KB |
Output is correct |
53 |
Correct |
180 ms |
84472 KB |
Output is correct |
54 |
Correct |
243 ms |
61968 KB |
Output is correct |
55 |
Correct |
232 ms |
62076 KB |
Output is correct |
56 |
Correct |
205 ms |
61432 KB |
Output is correct |
57 |
Correct |
318 ms |
72212 KB |
Output is correct |
58 |
Correct |
209 ms |
84588 KB |
Output is correct |
59 |
Correct |
242 ms |
69988 KB |
Output is correct |