#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 50005
#define log 17
#define K 5
#define oo 1e18
int dp[N][log][K][K], res[K][K], tmp[K][K], k, n, m, o;
void add(int rt[K][K], int a[K][K], int b[K][K]){
for (int z = 0; z < k; z++){
for (int x = 0; x < k; x++){
for (int y = 0; y < k; y++){
rt[x][y] = min(rt[x][y], a[x][z] + b[z][y]);
}
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> k >> n >> m >> o;
for (int i = 0; i < n; i++){
for (int j = 0; j < log; j++){
for (int k = 0; k < K; k++){
for (int k1 = 0; k1 < K; k1++){
dp[i][j][k][k1] = oo;
}
}
}
}
while(m--){
int a, b, t;
cin >> a >> b >> t;
dp[a / k][0][a % k][b % k] = t;
}
for (int j = 1; j < log; j++){
for (int i = 0; i + (1 << j) < (n + k - 1) / k; i++){
add(dp[i][j], dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
}
}
while (o--){
int a, b;
cin >> a >> b;
for (int i = 0; i < K; i++){
for (int j = 0; j < K; j++){
res[i][j] = oo;
if (i == j) res[i][j] = 0;
}
}
int da = a / k, db = b / k;
for (int j = log - 1; j >= 0; j--){
if (da + (1 << j) <= db){
for (int ii = 0; ii < K; ii++){
for (int jj = 0; jj < K; jj++){
tmp[ii][jj] = oo;
}
}
add(tmp, res, dp[da][j]);
for (int ii = 0; ii < K; ii++){
for (int jj = 0; jj < K; jj++){
res[ii][jj] = tmp[ii][jj];
}
}
da += (1 << j);
}
}
if (res[a % k][b % k] == oo) cout << -1 << "\n"; else cout << res[a % k][b % k] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
167764 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
35 ms |
167476 KB |
Output is correct |
9 |
Correct |
39 ms |
167516 KB |
Output is correct |
10 |
Correct |
34 ms |
166808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
168272 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
4700 KB |
Output is correct |
8 |
Correct |
3 ms |
4700 KB |
Output is correct |
9 |
Correct |
42 ms |
167676 KB |
Output is correct |
10 |
Correct |
56 ms |
168940 KB |
Output is correct |
11 |
Correct |
63 ms |
168556 KB |
Output is correct |
12 |
Correct |
49 ms |
168060 KB |
Output is correct |
13 |
Correct |
40 ms |
103516 KB |
Output is correct |
14 |
Correct |
31 ms |
102616 KB |
Output is correct |
15 |
Correct |
31 ms |
102228 KB |
Output is correct |
16 |
Correct |
30 ms |
102236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
35 ms |
167504 KB |
Output is correct |
11 |
Correct |
44 ms |
168312 KB |
Output is correct |
12 |
Correct |
51 ms |
168784 KB |
Output is correct |
13 |
Correct |
50 ms |
169208 KB |
Output is correct |
14 |
Correct |
46 ms |
168560 KB |
Output is correct |
15 |
Correct |
28 ms |
102224 KB |
Output is correct |
16 |
Correct |
28 ms |
102236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
464 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
35 ms |
167504 KB |
Output is correct |
11 |
Correct |
44 ms |
168312 KB |
Output is correct |
12 |
Correct |
51 ms |
168784 KB |
Output is correct |
13 |
Correct |
50 ms |
169208 KB |
Output is correct |
14 |
Correct |
46 ms |
168560 KB |
Output is correct |
15 |
Correct |
28 ms |
102224 KB |
Output is correct |
16 |
Correct |
28 ms |
102236 KB |
Output is correct |
17 |
Correct |
49 ms |
168180 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
2 ms |
4700 KB |
Output is correct |
25 |
Correct |
3 ms |
4700 KB |
Output is correct |
26 |
Correct |
4 ms |
4700 KB |
Output is correct |
27 |
Correct |
36 ms |
167512 KB |
Output is correct |
28 |
Correct |
52 ms |
168988 KB |
Output is correct |
29 |
Correct |
61 ms |
169044 KB |
Output is correct |
30 |
Correct |
49 ms |
168540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
167764 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
35 ms |
167476 KB |
Output is correct |
9 |
Correct |
39 ms |
167516 KB |
Output is correct |
10 |
Correct |
34 ms |
166808 KB |
Output is correct |
11 |
Correct |
51 ms |
168272 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
18 |
Correct |
3 ms |
4700 KB |
Output is correct |
19 |
Correct |
42 ms |
167676 KB |
Output is correct |
20 |
Correct |
56 ms |
168940 KB |
Output is correct |
21 |
Correct |
63 ms |
168556 KB |
Output is correct |
22 |
Correct |
49 ms |
168060 KB |
Output is correct |
23 |
Correct |
40 ms |
103516 KB |
Output is correct |
24 |
Correct |
31 ms |
102616 KB |
Output is correct |
25 |
Correct |
31 ms |
102228 KB |
Output is correct |
26 |
Correct |
30 ms |
102236 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
352 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
464 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
1 ms |
4700 KB |
Output is correct |
34 |
Correct |
2 ms |
4700 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
35 ms |
167504 KB |
Output is correct |
37 |
Correct |
44 ms |
168312 KB |
Output is correct |
38 |
Correct |
51 ms |
168784 KB |
Output is correct |
39 |
Correct |
50 ms |
169208 KB |
Output is correct |
40 |
Correct |
46 ms |
168560 KB |
Output is correct |
41 |
Correct |
28 ms |
102224 KB |
Output is correct |
42 |
Correct |
28 ms |
102236 KB |
Output is correct |
43 |
Correct |
49 ms |
168180 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
2 ms |
4700 KB |
Output is correct |
50 |
Correct |
2 ms |
4700 KB |
Output is correct |
51 |
Correct |
3 ms |
4700 KB |
Output is correct |
52 |
Correct |
4 ms |
4700 KB |
Output is correct |
53 |
Correct |
36 ms |
167512 KB |
Output is correct |
54 |
Correct |
52 ms |
168988 KB |
Output is correct |
55 |
Correct |
61 ms |
169044 KB |
Output is correct |
56 |
Correct |
49 ms |
168540 KB |
Output is correct |
57 |
Correct |
64 ms |
170068 KB |
Output is correct |
58 |
Correct |
46 ms |
167508 KB |
Output is correct |
59 |
Correct |
54 ms |
168500 KB |
Output is correct |