#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';
}
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 |
26 ms |
3532 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 |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
28 ms |
3436 KB |
Output is correct |
9 |
Correct |
26 ms |
3436 KB |
Output is correct |
10 |
Correct |
15 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
6668 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
3532 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 |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
28 ms |
3436 KB |
Output is correct |
9 |
Correct |
26 ms |
3436 KB |
Output is correct |
10 |
Correct |
15 ms |
3404 KB |
Output is correct |
11 |
Correct |
43 ms |
6668 KB |
Output is correct |
12 |
Correct |
0 ms |
312 KB |
Output is correct |
13 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |