#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
const int MAX_N = 5e4 + 10;
const int MAX_B = 20;
const int MAX_K = 5;
const int INF = 1e9 + 7;
int N, K;
int dist[MAX_N][MAX_B][MAX_K][MAX_K];
int now[MAX_K][MAX_K], prv[MAX_K][MAX_K];
void init() {
for(int i = 0; i <= N / K; i++) {
for(int j = 0; j < MAX_B; j++) {
for(int k = 0; k < MAX_K; k++) {
for(int l = 0; l < MAX_K; l++) {
dist[i][j][k][l] = INF;
}
}
}
}
}
void FloydWarshall(int c[MAX_K][MAX_K], int a[MAX_K][MAX_K], int b[MAX_K][MAX_K]) {
for(int k = 0; k < K; k++) {
for(int i = 0; i < K; i++) {
for(int j = 0; j < K; j++) {
c[i][j] = min(c[i][j], a[i][k] + b[k][j]);
}
}
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
init();
int M, O;
cin >> K >> N >> M >> O;
while(M--) {
int a, b, t;
cin >> a >> b >> t;
dist[a / K][0][a % K][b % K] = min(dist[a / K][0][a % K][b % K], t);
}
for(int j = 1; (1<<j) <= N / K; j++) {
for(int i = 0; i + (1<<j) - 1 <= N / K; i++) {
FloydWarshall(dist[i][j], dist[i][j - 1], dist[i + (1<<(j - 1))][j - 1]);
}
}
while(O--) {
int a, b;
cin >> a >> b;
if(a / K == b / K) {
cout << "-1\n";
continue;
}
for(int i = 0; i < K; i++) {
for(int j = 0; j < K; j++) {
prv[i][j] = INF;
if(i == j) {
prv[i][j] = 0;
}
}
}
int s = a / K, t = b / K;
for(int j = MAX_B - 1; j >= 0; j--) {
if(s + (1<<j) > t) {
continue;
}
for(int k = 0; k < K; k++) {
for(int l = 0; l < K; l++) {
now[k][l] = prv[k][l];
prv[k][l] = INF;
}
}
FloydWarshall(prv, now, dist[s][j]);
s += (1<<j);
}
if(prv[a % K][b % K] == INF) {
cout << "-1\n";
}
else {
cout << prv[a % K][b % K] << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
340 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
468 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
3 ms |
468 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2 ms |
468 KB |
Execution killed with signal 8 |
2 |
Halted |
0 ms |
0 KB |
- |