#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;
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 |
27 ms |
3440 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
27 ms |
3452 KB |
Output is correct |
9 |
Correct |
26 ms |
3436 KB |
Output is correct |
10 |
Correct |
16 ms |
3452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
6584 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 |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
7 ms |
396 KB |
Output is correct |
8 |
Correct |
8 ms |
460 KB |
Output is correct |
9 |
Correct |
27 ms |
3448 KB |
Output is correct |
10 |
Correct |
68 ms |
9696 KB |
Output is correct |
11 |
Correct |
44 ms |
6596 KB |
Output is correct |
12 |
Correct |
54 ms |
9684 KB |
Output is correct |
13 |
Correct |
63 ms |
9824 KB |
Output is correct |
14 |
Correct |
34 ms |
5836 KB |
Output is correct |
15 |
Correct |
45 ms |
9704 KB |
Output is correct |
16 |
Correct |
44 ms |
9676 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 |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
18 ms |
3404 KB |
Output is correct |
11 |
Correct |
32 ms |
6476 KB |
Output is correct |
12 |
Correct |
61 ms |
9688 KB |
Output is correct |
13 |
Correct |
52 ms |
9676 KB |
Output is correct |
14 |
Correct |
47 ms |
9676 KB |
Output is correct |
15 |
Correct |
36 ms |
9676 KB |
Output is correct |
16 |
Correct |
36 ms |
9572 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 |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
18 ms |
3404 KB |
Output is correct |
11 |
Correct |
32 ms |
6476 KB |
Output is correct |
12 |
Correct |
61 ms |
9688 KB |
Output is correct |
13 |
Correct |
52 ms |
9676 KB |
Output is correct |
14 |
Correct |
47 ms |
9676 KB |
Output is correct |
15 |
Correct |
36 ms |
9676 KB |
Output is correct |
16 |
Correct |
36 ms |
9572 KB |
Output is correct |
17 |
Correct |
36 ms |
6476 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
284 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
3 ms |
460 KB |
Output is correct |
25 |
Correct |
6 ms |
648 KB |
Output is correct |
26 |
Correct |
5 ms |
460 KB |
Output is correct |
27 |
Correct |
21 ms |
3444 KB |
Output is correct |
28 |
Correct |
54 ms |
9676 KB |
Output is correct |
29 |
Correct |
58 ms |
9692 KB |
Output is correct |
30 |
Correct |
49 ms |
9696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
3440 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
27 ms |
3452 KB |
Output is correct |
9 |
Correct |
26 ms |
3436 KB |
Output is correct |
10 |
Correct |
16 ms |
3452 KB |
Output is correct |
11 |
Correct |
46 ms |
6584 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 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 |
396 KB |
Output is correct |
18 |
Correct |
8 ms |
460 KB |
Output is correct |
19 |
Correct |
27 ms |
3448 KB |
Output is correct |
20 |
Correct |
68 ms |
9696 KB |
Output is correct |
21 |
Correct |
44 ms |
6596 KB |
Output is correct |
22 |
Correct |
54 ms |
9684 KB |
Output is correct |
23 |
Correct |
63 ms |
9824 KB |
Output is correct |
24 |
Correct |
34 ms |
5836 KB |
Output is correct |
25 |
Correct |
45 ms |
9704 KB |
Output is correct |
26 |
Correct |
44 ms |
9676 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 |
0 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
2 ms |
588 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
18 ms |
3404 KB |
Output is correct |
37 |
Correct |
32 ms |
6476 KB |
Output is correct |
38 |
Correct |
61 ms |
9688 KB |
Output is correct |
39 |
Correct |
52 ms |
9676 KB |
Output is correct |
40 |
Correct |
47 ms |
9676 KB |
Output is correct |
41 |
Correct |
36 ms |
9676 KB |
Output is correct |
42 |
Correct |
36 ms |
9572 KB |
Output is correct |
43 |
Correct |
36 ms |
6476 KB |
Output is correct |
44 |
Correct |
0 ms |
204 KB |
Output is correct |
45 |
Correct |
0 ms |
284 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
2 ms |
332 KB |
Output is correct |
50 |
Correct |
3 ms |
460 KB |
Output is correct |
51 |
Correct |
6 ms |
648 KB |
Output is correct |
52 |
Correct |
5 ms |
460 KB |
Output is correct |
53 |
Correct |
21 ms |
3444 KB |
Output is correct |
54 |
Correct |
54 ms |
9676 KB |
Output is correct |
55 |
Correct |
58 ms |
9692 KB |
Output is correct |
56 |
Correct |
49 ms |
9696 KB |
Output is correct |
57 |
Correct |
112 ms |
12812 KB |
Output is correct |
58 |
Correct |
27 ms |
3452 KB |
Output is correct |
59 |
Correct |
45 ms |
6560 KB |
Output is correct |