#include <bits/stdc++.h>
using namespace std;
int K, N, M, Q;
vector<vector<vector<vector<int>>>> dp;
int main() {
cin >> K >> N >> M >> Q;
dp.resize((N / K + 1), vector<vector<vector<int>>>(ceil(log2(N / K)) + 1,
vector<vector<int>>(K, vector<int>(K, INT_MAX / 2))));
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) <= (N + K - 1) / K; l++) {
for (int g = 0; g < (N + K - 1) / K - (1 << l); 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, INT_MAX / 2));
for (int j = 0; j < K; j++) {
answer[j][j] = 0;
}
int len = b / K - a / K, lst = a / K;
for (int l = 0; (1 << l) <= len; l++) {
if ((len & (1 << l)) != 0) {
vector<vector<int>> tmp(K, vector<int>(K, INT_MAX / 2));
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] >= INT_MAX / 2) {
cout << "-1\n";
} else {
cout << answer[a % K][b % K] << "\n";
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
182 ms |
75056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
60768 KB |
Output is correct |
2 |
Correct |
0 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 |
22 ms |
1304 KB |
Output is correct |
8 |
Correct |
27 ms |
1088 KB |
Output is correct |
9 |
Correct |
181 ms |
75084 KB |
Output is correct |
10 |
Correct |
246 ms |
53136 KB |
Output is correct |
11 |
Correct |
221 ms |
60848 KB |
Output is correct |
12 |
Correct |
189 ms |
53196 KB |
Output is correct |
13 |
Correct |
183 ms |
26192 KB |
Output is correct |
14 |
Correct |
130 ms |
29900 KB |
Output is correct |
15 |
Correct |
123 ms |
26180 KB |
Output is correct |
16 |
Correct |
120 ms |
26300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
182 ms |
75056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |