#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int k, n, m, q;
cin >> k >> n >> m >> q;
int g = (n + k - 1) / k;
int go[g][16][k][k];
for (int i = 0; i < g; i++) {
for (int b = 0; b < 16; b++) {
for (int c = 0; c < k; c++) {
for (int d = 0; d < k; d++) {
go[i][b][c][d] = 1e9;
}
}
}
}
for (int i = 0; i < m; i++) {
int a, b, w;
cin >> a >> b >> w;
go[a / k][0][a % k][b % k] = w;
}
for (int b = 1; (1 << b) < g; b++) {
for (int i = 0; i < g; i++) {
if (i + (1 << b) > g - 1) break;
for (int c = 0; c < k; c++) {
for (int d = 0; d < k; d++) {
for (int mid = 0; mid < k; mid++) {
go[i][b][c][d] = min(go[i][b][c][d], go[i][b - 1][c][mid] + go[i + (1 << (b - 1))][b - 1][mid][d]);
}
}
}
}
}
while (q--) {
int a, b;
cin >> a >> b;
int x = a / k, y = b / k;
if (x == y) {
cout << -1 << '\n';
continue;
}
int d = y - x;
vector<vector<int>> dp(k, vector<int>(k, 1e9));
for (int i = 0; i < k; i++) {
dp[i][i] = 0;
}
for (int b = 0; (1 << b) <= d; b++) {
if (d & (1 << b)) {
vector<vector<int>> dq(k, vector<int>(k, 1e9));
for (int to = 0; to < k; to++) {
for (int from = 0; from < k; from++) {
for (int mid = 0; mid < k; mid++) {
dq[from][to] = min(dq[from][to], dp[from][mid] + go[x][b][mid][to]);
}
}
}
swap(dp, dq);
x += 1 << b;
}
}
int ans = dp[a % k][b % k];
cout << (ans == 1e9 ? -1 : ans) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
3404 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
28 ms |
3444 KB |
Output is correct |
9 |
Correct |
27 ms |
3432 KB |
Output is correct |
10 |
Correct |
16 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
6564 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
7 ms |
460 KB |
Output is correct |
8 |
Correct |
9 ms |
588 KB |
Output is correct |
9 |
Correct |
28 ms |
4300 KB |
Output is correct |
10 |
Correct |
74 ms |
11968 KB |
Output is correct |
11 |
Correct |
58 ms |
8268 KB |
Output is correct |
12 |
Correct |
75 ms |
10992 KB |
Output is correct |
13 |
Correct |
65 ms |
11980 KB |
Output is correct |
14 |
Correct |
35 ms |
7364 KB |
Output is correct |
15 |
Correct |
45 ms |
10904 KB |
Output is correct |
16 |
Correct |
47 ms |
10912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 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 |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
588 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
20 ms |
4192 KB |
Output is correct |
11 |
Correct |
36 ms |
8068 KB |
Output is correct |
12 |
Correct |
53 ms |
11856 KB |
Output is correct |
13 |
Correct |
62 ms |
12088 KB |
Output is correct |
14 |
Correct |
53 ms |
11468 KB |
Output is correct |
15 |
Correct |
40 ms |
10820 KB |
Output is correct |
16 |
Correct |
40 ms |
10876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 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 |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
588 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
20 ms |
4192 KB |
Output is correct |
11 |
Correct |
36 ms |
8068 KB |
Output is correct |
12 |
Correct |
53 ms |
11856 KB |
Output is correct |
13 |
Correct |
62 ms |
12088 KB |
Output is correct |
14 |
Correct |
53 ms |
11468 KB |
Output is correct |
15 |
Correct |
40 ms |
10820 KB |
Output is correct |
16 |
Correct |
40 ms |
10876 KB |
Output is correct |
17 |
Correct |
38 ms |
8092 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
308 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
312 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
3 ms |
460 KB |
Output is correct |
25 |
Correct |
7 ms |
716 KB |
Output is correct |
26 |
Correct |
6 ms |
640 KB |
Output is correct |
27 |
Correct |
22 ms |
4272 KB |
Output is correct |
28 |
Correct |
66 ms |
11856 KB |
Output is correct |
29 |
Correct |
60 ms |
12104 KB |
Output is correct |
30 |
Correct |
68 ms |
11596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
3404 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
28 ms |
3444 KB |
Output is correct |
9 |
Correct |
27 ms |
3432 KB |
Output is correct |
10 |
Correct |
16 ms |
3404 KB |
Output is correct |
11 |
Correct |
66 ms |
6564 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
7 ms |
460 KB |
Output is correct |
18 |
Correct |
9 ms |
588 KB |
Output is correct |
19 |
Correct |
28 ms |
4300 KB |
Output is correct |
20 |
Correct |
74 ms |
11968 KB |
Output is correct |
21 |
Correct |
58 ms |
8268 KB |
Output is correct |
22 |
Correct |
75 ms |
10992 KB |
Output is correct |
23 |
Correct |
65 ms |
11980 KB |
Output is correct |
24 |
Correct |
35 ms |
7364 KB |
Output is correct |
25 |
Correct |
45 ms |
10904 KB |
Output is correct |
26 |
Correct |
47 ms |
10912 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 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 |
2 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
3 ms |
588 KB |
Output is correct |
35 |
Correct |
3 ms |
588 KB |
Output is correct |
36 |
Correct |
20 ms |
4192 KB |
Output is correct |
37 |
Correct |
36 ms |
8068 KB |
Output is correct |
38 |
Correct |
53 ms |
11856 KB |
Output is correct |
39 |
Correct |
62 ms |
12088 KB |
Output is correct |
40 |
Correct |
53 ms |
11468 KB |
Output is correct |
41 |
Correct |
40 ms |
10820 KB |
Output is correct |
42 |
Correct |
40 ms |
10876 KB |
Output is correct |
43 |
Correct |
38 ms |
8092 KB |
Output is correct |
44 |
Correct |
1 ms |
204 KB |
Output is correct |
45 |
Correct |
0 ms |
308 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
312 KB |
Output is correct |
49 |
Correct |
2 ms |
332 KB |
Output is correct |
50 |
Correct |
3 ms |
460 KB |
Output is correct |
51 |
Correct |
7 ms |
716 KB |
Output is correct |
52 |
Correct |
6 ms |
640 KB |
Output is correct |
53 |
Correct |
22 ms |
4272 KB |
Output is correct |
54 |
Correct |
66 ms |
11856 KB |
Output is correct |
55 |
Correct |
60 ms |
12104 KB |
Output is correct |
56 |
Correct |
68 ms |
11596 KB |
Output is correct |
57 |
Correct |
123 ms |
16172 KB |
Output is correct |
58 |
Correct |
40 ms |
4324 KB |
Output is correct |
59 |
Correct |
51 ms |
8340 KB |
Output is correct |