#include <cmath>
#include <iostream>
#include <set>
#include <climits>
#include <cstdio>
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <iomanip>
#include <unordered_map>
#include <type_traits>
#include <string>
#include <queue>
#define ll long long
#include <map>
using namespace std;
int64_t dp[50050][32][5][5];
int K, N, M, O;
vector<vector<int64_t>> f (int in1, int in2) {
vector<vector<int64_t>> cur(K);
for (int i = 0; i < K; i++) {
cur[i].assign(K, INT_MAX);
cur[i][i] = 0;
}
int dif = abs(in1 - in2);
while (dif > 0) {
vector<vector<int64_t>> new_cur = cur;
for (int i = 0; i < K; i++) {
new_cur[i].assign(K, INT_MAX);
}
int lg2 = log2(dif);
for (int i = 0; i < K; i++) {
for (int j = 0; j < K; j++) {
for (int k = 0; k < K; k++) {
new_cur[i][j] = min(new_cur[i][j], cur[i][k] + dp[in1][lg2][k][j]);
}
}
}
dif -= (1 << lg2);
in1 += (1 << lg2);
cur = new_cur;
}
return cur;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> K >> N >> M >> O;
N++;
while (N % K != K - 1) {
N++;
}
for (int j = 0; j < 32; j++) {
for (int i = 0; i < N/K; i++) {
for (int x = 0; x < K; x++) {
for (int y = 0; y < K; y++) {
dp[i][j][x][y] = INT_MAX;
}
}
}
}
while (M--) {
int64_t a, b, w;
cin >> a >> b >> w;
dp[a/K][0][a % K][b % K] = min(w, dp[a/K][0][a % K][b % K]);
}
for (int j = 1; j < 32; j++) {
for (int i = 0; i < N/K; i++) {
for (int x = 0; x < K; x++) {
for (int y = 0; y < K; y++) {
for (int z = 0; z < K; z++) {
if (i + (1 << (j - 1)) < N/K)
dp[i][j][x][y] = min(dp[i][j][x][y], dp[i][j - 1][x][z] + dp[i + (1 << (j - 1))][j - 1][z][y]);
}
}
}
}
}
while (O--) {
int a, b;
cin >> a >> b;
if (a/K == b/K) {
cout << "-1\n";
continue;
}
vector<vector<int64_t>> vec = f(a/K, b/K);
if (vec[a % K][b % K] == INT_MAX) {
cout << "-1\n";
continue;
}
cout << vec[a % K][b % K] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
313420 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6612 KB |
Output is correct |
7 |
Correct |
4 ms |
6612 KB |
Output is correct |
8 |
Correct |
179 ms |
314256 KB |
Output is correct |
9 |
Correct |
177 ms |
314308 KB |
Output is correct |
10 |
Correct |
179 ms |
313468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
156868 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
324 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
10 ms |
6616 KB |
Output is correct |
8 |
Correct |
11 ms |
3540 KB |
Output is correct |
9 |
Correct |
170 ms |
314320 KB |
Output is correct |
10 |
Correct |
165 ms |
106956 KB |
Output is correct |
11 |
Correct |
154 ms |
158636 KB |
Output is correct |
12 |
Correct |
146 ms |
105988 KB |
Output is correct |
13 |
Correct |
126 ms |
40288 KB |
Output is correct |
14 |
Correct |
87 ms |
64396 KB |
Output is correct |
15 |
Correct |
103 ms |
39164 KB |
Output is correct |
16 |
Correct |
101 ms |
39116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
6612 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
3 ms |
1636 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
164 ms |
314180 KB |
Output is correct |
11 |
Correct |
135 ms |
158284 KB |
Output is correct |
12 |
Correct |
143 ms |
106824 KB |
Output is correct |
13 |
Correct |
145 ms |
107084 KB |
Output is correct |
14 |
Correct |
140 ms |
106580 KB |
Output is correct |
15 |
Correct |
95 ms |
39020 KB |
Output is correct |
16 |
Correct |
84 ms |
38988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
4 ms |
6612 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 KB |
Output is correct |
8 |
Correct |
3 ms |
1636 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
164 ms |
314180 KB |
Output is correct |
11 |
Correct |
135 ms |
158284 KB |
Output is correct |
12 |
Correct |
143 ms |
106824 KB |
Output is correct |
13 |
Correct |
145 ms |
107084 KB |
Output is correct |
14 |
Correct |
140 ms |
106580 KB |
Output is correct |
15 |
Correct |
95 ms |
39020 KB |
Output is correct |
16 |
Correct |
84 ms |
38988 KB |
Output is correct |
17 |
Correct |
143 ms |
158448 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
6 ms |
6612 KB |
Output is correct |
24 |
Correct |
8 ms |
3540 KB |
Output is correct |
25 |
Correct |
8 ms |
1620 KB |
Output is correct |
26 |
Correct |
7 ms |
1876 KB |
Output is correct |
27 |
Correct |
165 ms |
314264 KB |
Output is correct |
28 |
Correct |
148 ms |
106924 KB |
Output is correct |
29 |
Correct |
148 ms |
107124 KB |
Output is correct |
30 |
Correct |
142 ms |
106444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
313420 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6612 KB |
Output is correct |
7 |
Correct |
4 ms |
6612 KB |
Output is correct |
8 |
Correct |
179 ms |
314256 KB |
Output is correct |
9 |
Correct |
177 ms |
314308 KB |
Output is correct |
10 |
Correct |
179 ms |
313468 KB |
Output is correct |
11 |
Correct |
151 ms |
156868 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
324 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
10 ms |
6616 KB |
Output is correct |
18 |
Correct |
11 ms |
3540 KB |
Output is correct |
19 |
Correct |
170 ms |
314320 KB |
Output is correct |
20 |
Correct |
165 ms |
106956 KB |
Output is correct |
21 |
Correct |
154 ms |
158636 KB |
Output is correct |
22 |
Correct |
146 ms |
105988 KB |
Output is correct |
23 |
Correct |
126 ms |
40288 KB |
Output is correct |
24 |
Correct |
87 ms |
64396 KB |
Output is correct |
25 |
Correct |
103 ms |
39164 KB |
Output is correct |
26 |
Correct |
101 ms |
39116 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
4 ms |
6612 KB |
Output is correct |
33 |
Correct |
2 ms |
3412 KB |
Output is correct |
34 |
Correct |
3 ms |
1636 KB |
Output is correct |
35 |
Correct |
2 ms |
1876 KB |
Output is correct |
36 |
Correct |
164 ms |
314180 KB |
Output is correct |
37 |
Correct |
135 ms |
158284 KB |
Output is correct |
38 |
Correct |
143 ms |
106824 KB |
Output is correct |
39 |
Correct |
145 ms |
107084 KB |
Output is correct |
40 |
Correct |
140 ms |
106580 KB |
Output is correct |
41 |
Correct |
95 ms |
39020 KB |
Output is correct |
42 |
Correct |
84 ms |
38988 KB |
Output is correct |
43 |
Correct |
143 ms |
158448 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
328 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
6 ms |
6612 KB |
Output is correct |
50 |
Correct |
8 ms |
3540 KB |
Output is correct |
51 |
Correct |
8 ms |
1620 KB |
Output is correct |
52 |
Correct |
7 ms |
1876 KB |
Output is correct |
53 |
Correct |
165 ms |
314264 KB |
Output is correct |
54 |
Correct |
148 ms |
106924 KB |
Output is correct |
55 |
Correct |
148 ms |
107124 KB |
Output is correct |
56 |
Correct |
142 ms |
106444 KB |
Output is correct |
57 |
Correct |
180 ms |
81936 KB |
Output is correct |
58 |
Correct |
193 ms |
314308 KB |
Output is correct |
59 |
Correct |
156 ms |
158608 KB |
Output is correct |