#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 - 1) / 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);
int M, O;
cin >> K >> N >> M >> O;
init();
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 - 1) / K; j++) {
for(int i = 0; i + (1<<j) - 1 <= (N - 1) / 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 |
Correct |
83 ms |
99032 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
2264 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
2 ms |
2264 KB |
Output is correct |
8 |
Correct |
77 ms |
99036 KB |
Output is correct |
9 |
Correct |
81 ms |
99028 KB |
Output is correct |
10 |
Correct |
73 ms |
98212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
50828 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
2360 KB |
Output is correct |
8 |
Correct |
6 ms |
1364 KB |
Output is correct |
9 |
Correct |
77 ms |
99056 KB |
Output is correct |
10 |
Correct |
80 ms |
35180 KB |
Output is correct |
11 |
Correct |
78 ms |
51020 KB |
Output is correct |
12 |
Correct |
71 ms |
34212 KB |
Output is correct |
13 |
Correct |
87 ms |
14412 KB |
Output is correct |
14 |
Correct |
51 ms |
21328 KB |
Output is correct |
15 |
Correct |
40 ms |
13212 KB |
Output is correct |
16 |
Correct |
41 ms |
13288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 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 |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
76 ms |
98904 KB |
Output is correct |
11 |
Correct |
68 ms |
50760 KB |
Output is correct |
12 |
Correct |
72 ms |
35084 KB |
Output is correct |
13 |
Correct |
87 ms |
35340 KB |
Output is correct |
14 |
Correct |
64 ms |
34732 KB |
Output is correct |
15 |
Correct |
35 ms |
13188 KB |
Output is correct |
16 |
Correct |
37 ms |
13204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 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 |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
76 ms |
98904 KB |
Output is correct |
11 |
Correct |
68 ms |
50760 KB |
Output is correct |
12 |
Correct |
72 ms |
35084 KB |
Output is correct |
13 |
Correct |
87 ms |
35340 KB |
Output is correct |
14 |
Correct |
64 ms |
34732 KB |
Output is correct |
15 |
Correct |
35 ms |
13188 KB |
Output is correct |
16 |
Correct |
37 ms |
13204 KB |
Output is correct |
17 |
Correct |
97 ms |
50800 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
276 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
3 ms |
2260 KB |
Output is correct |
24 |
Correct |
2 ms |
1364 KB |
Output is correct |
25 |
Correct |
5 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
852 KB |
Output is correct |
27 |
Correct |
80 ms |
98904 KB |
Output is correct |
28 |
Correct |
86 ms |
35092 KB |
Output is correct |
29 |
Correct |
87 ms |
35380 KB |
Output is correct |
30 |
Correct |
79 ms |
34712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
99032 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
0 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
2264 KB |
Output is correct |
6 |
Correct |
2 ms |
2260 KB |
Output is correct |
7 |
Correct |
2 ms |
2264 KB |
Output is correct |
8 |
Correct |
77 ms |
99036 KB |
Output is correct |
9 |
Correct |
81 ms |
99028 KB |
Output is correct |
10 |
Correct |
73 ms |
98212 KB |
Output is correct |
11 |
Correct |
73 ms |
50828 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
4 ms |
2360 KB |
Output is correct |
18 |
Correct |
6 ms |
1364 KB |
Output is correct |
19 |
Correct |
77 ms |
99056 KB |
Output is correct |
20 |
Correct |
80 ms |
35180 KB |
Output is correct |
21 |
Correct |
78 ms |
51020 KB |
Output is correct |
22 |
Correct |
71 ms |
34212 KB |
Output is correct |
23 |
Correct |
87 ms |
14412 KB |
Output is correct |
24 |
Correct |
51 ms |
21328 KB |
Output is correct |
25 |
Correct |
40 ms |
13212 KB |
Output is correct |
26 |
Correct |
41 ms |
13288 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
2260 KB |
Output is correct |
33 |
Correct |
1 ms |
1236 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
76 ms |
98904 KB |
Output is correct |
37 |
Correct |
68 ms |
50760 KB |
Output is correct |
38 |
Correct |
72 ms |
35084 KB |
Output is correct |
39 |
Correct |
87 ms |
35340 KB |
Output is correct |
40 |
Correct |
64 ms |
34732 KB |
Output is correct |
41 |
Correct |
35 ms |
13188 KB |
Output is correct |
42 |
Correct |
37 ms |
13204 KB |
Output is correct |
43 |
Correct |
97 ms |
50800 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
276 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
3 ms |
2260 KB |
Output is correct |
50 |
Correct |
2 ms |
1364 KB |
Output is correct |
51 |
Correct |
5 ms |
724 KB |
Output is correct |
52 |
Correct |
4 ms |
852 KB |
Output is correct |
53 |
Correct |
80 ms |
98904 KB |
Output is correct |
54 |
Correct |
86 ms |
35092 KB |
Output is correct |
55 |
Correct |
87 ms |
35380 KB |
Output is correct |
56 |
Correct |
79 ms |
34712 KB |
Output is correct |
57 |
Correct |
106 ms |
28084 KB |
Output is correct |
58 |
Correct |
102 ms |
99000 KB |
Output is correct |
59 |
Correct |
84 ms |
50960 KB |
Output is correct |