#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 5e4 + 5;
const int mxK = 5;
const int lg = 16;
const long long INF = 1e18;
long long dist[mxN][lg][mxK][mxK];
int k, n, m, q;
void testCase() {
for (int i = 0; i < mxN; i++) {
for (int j = 0; j < lg; j++) {
for (int x = 0; x < mxK; x++) {
for (int l = 0; l < mxK; l++) {
dist[i][j][x][l] = INF;
}
}
}
}
cin >> k >> n >> m >> q;
while (m--) {
int a, b, c;
cin >> a >> b >> c;
dist[a / k][0][a % k][b % k] = min(dist[a / k][0][a % k][b % k], 1LL * c);
}
for (int j = 1; j < lg; j++) {
for (int i = 0; i < n; i++) {
for (int a = 0; a < k; a++) {
for (int b = 0; b < k; b++) {
for (int c = 0; c < k; c++) {
if (i + (1 << j) - 1 >= n) continue;
if (dist[i][j - 1][a][c] == INF) continue;
if (dist[i + (1 << j) - 1][j - 1][c][b] == INF) continue;
dist[i][j][a][b] = min(dist[i][j][a][b], dist[i][j - 1][a][c] + dist[i + (1 << j) - 1][j - 1][c][b]);
}
}
}
}
}
while (q--) {
int a, b;
cin >> a >> b;
int aa = a / k;
int bb = b / k;
long long ans[mxK];
for (int i = 0; i < k; i++) ans[i] = INF;
ans[a % k] = 0;
for (int j = lg - 1; j >= 0; j--) {
if (aa + (1 << j) <= bb) {
long long new_ans[mxK];
for (int i = 0; i < k; i++) new_ans[i] = INF;
for (int from = 0; from < k; from++) {
for (int to = 0; to < k; to++) {
if (ans[from] == INF) continue;
if (dist[aa][j][from][to] == INF) continue;
new_ans[to] = min(new_ans[to], ans[from] + dist[aa][j][from][to]);
}
}
for (int i = 0; i < k; i++) ans[i] = new_ans[i];
aa += (1 << j);
}
}
long long out = ans[b % k];
if (aa > bb || out == INF) out = -1;
cout << out << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
testCase();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
95 ms |
156876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
161 ms |
156876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
156860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
156860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
95 ms |
156876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |