#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e4 + 7;
const ll INF = 1e18;
int k, n, m, o;
struct Matrix {
ll mat[5][5];
Matrix() {
for (int x = 0; x < 5; ++x) {
for (int y = 0; y < 5; ++y) {
mat[x][y] = INF;
}
}
}
Matrix operator * (Matrix other) {
Matrix c;
for (int x = 0; x < k; ++x) {
for (int y = 0; y < k; ++y) {
for (int z = 0; z < k; ++z) {
c.mat[x][y] = min(c.mat[x][y], mat[x][z] + other.mat[z][y]);
}
}
}
return c;
}
};
Matrix dist[N][17];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> k >> n >> m >> o;
while (m--) {
int a, b, t;
cin >> a >> b >> t;
dist[a / k][0].mat[a % k][b % k] = t;
}
int layers = (n + k - 1) / k;
for (int j = 1; j < 17; ++j) {
for (int i = 0; i + (1 << j) < layers; ++i) {
dist[i][j] = dist[i][j - 1] * dist[i + (1 << (j - 1))][j - 1];;
}
}
while (o--) {
int a, b;
cin >> a >> b;
Matrix ans;
for (int i = 0; i < 5; ++i) {
ans.mat[i][i] = 0;
}
int cur = a / k;
for (int i = 16; i >= 0; --i) {
if (cur + (1 << i) <= b / k) {
ans = ans * dist[cur][i];
cur += (1 << i);
}
}
cout << (ans.mat[a % k][b % k] == INF ? -1 : ans.mat[a % k][b % k]) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
166740 KB |
Output is correct |
2 |
Correct |
20 ms |
166760 KB |
Output is correct |
3 |
Correct |
20 ms |
166744 KB |
Output is correct |
4 |
Correct |
19 ms |
166748 KB |
Output is correct |
5 |
Correct |
21 ms |
166748 KB |
Output is correct |
6 |
Correct |
20 ms |
166744 KB |
Output is correct |
7 |
Correct |
21 ms |
166744 KB |
Output is correct |
8 |
Correct |
87 ms |
166744 KB |
Output is correct |
9 |
Correct |
86 ms |
166744 KB |
Output is correct |
10 |
Correct |
76 ms |
166748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
167276 KB |
Output is correct |
2 |
Correct |
20 ms |
166736 KB |
Output is correct |
3 |
Correct |
21 ms |
166736 KB |
Output is correct |
4 |
Correct |
19 ms |
166744 KB |
Output is correct |
5 |
Correct |
21 ms |
166736 KB |
Output is correct |
6 |
Correct |
18 ms |
166748 KB |
Output is correct |
7 |
Correct |
23 ms |
166748 KB |
Output is correct |
8 |
Correct |
23 ms |
166692 KB |
Output is correct |
9 |
Correct |
88 ms |
166796 KB |
Output is correct |
10 |
Correct |
55 ms |
166736 KB |
Output is correct |
11 |
Correct |
60 ms |
166748 KB |
Output is correct |
12 |
Correct |
49 ms |
166740 KB |
Output is correct |
13 |
Correct |
51 ms |
166748 KB |
Output is correct |
14 |
Correct |
41 ms |
167328 KB |
Output is correct |
15 |
Correct |
40 ms |
168020 KB |
Output is correct |
16 |
Correct |
40 ms |
167508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
166740 KB |
Output is correct |
2 |
Correct |
18 ms |
166748 KB |
Output is correct |
3 |
Correct |
18 ms |
166748 KB |
Output is correct |
4 |
Correct |
20 ms |
166820 KB |
Output is correct |
5 |
Correct |
19 ms |
166748 KB |
Output is correct |
6 |
Correct |
20 ms |
166608 KB |
Output is correct |
7 |
Correct |
19 ms |
166748 KB |
Output is correct |
8 |
Correct |
19 ms |
166756 KB |
Output is correct |
9 |
Correct |
19 ms |
166696 KB |
Output is correct |
10 |
Correct |
82 ms |
167248 KB |
Output is correct |
11 |
Correct |
54 ms |
168276 KB |
Output is correct |
12 |
Correct |
53 ms |
168868 KB |
Output is correct |
13 |
Correct |
57 ms |
169296 KB |
Output is correct |
14 |
Correct |
56 ms |
168484 KB |
Output is correct |
15 |
Correct |
36 ms |
167084 KB |
Output is correct |
16 |
Correct |
36 ms |
167820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
166740 KB |
Output is correct |
2 |
Correct |
18 ms |
166748 KB |
Output is correct |
3 |
Correct |
18 ms |
166748 KB |
Output is correct |
4 |
Correct |
20 ms |
166820 KB |
Output is correct |
5 |
Correct |
19 ms |
166748 KB |
Output is correct |
6 |
Correct |
20 ms |
166608 KB |
Output is correct |
7 |
Correct |
19 ms |
166748 KB |
Output is correct |
8 |
Correct |
19 ms |
166756 KB |
Output is correct |
9 |
Correct |
19 ms |
166696 KB |
Output is correct |
10 |
Correct |
82 ms |
167248 KB |
Output is correct |
11 |
Correct |
54 ms |
168276 KB |
Output is correct |
12 |
Correct |
53 ms |
168868 KB |
Output is correct |
13 |
Correct |
57 ms |
169296 KB |
Output is correct |
14 |
Correct |
56 ms |
168484 KB |
Output is correct |
15 |
Correct |
36 ms |
167084 KB |
Output is correct |
16 |
Correct |
36 ms |
167820 KB |
Output is correct |
17 |
Correct |
56 ms |
168272 KB |
Output is correct |
18 |
Correct |
22 ms |
166740 KB |
Output is correct |
19 |
Correct |
18 ms |
166604 KB |
Output is correct |
20 |
Correct |
20 ms |
166748 KB |
Output is correct |
21 |
Correct |
20 ms |
166748 KB |
Output is correct |
22 |
Correct |
20 ms |
166748 KB |
Output is correct |
23 |
Correct |
22 ms |
166740 KB |
Output is correct |
24 |
Correct |
20 ms |
166740 KB |
Output is correct |
25 |
Correct |
21 ms |
166748 KB |
Output is correct |
26 |
Correct |
21 ms |
166656 KB |
Output is correct |
27 |
Correct |
81 ms |
167420 KB |
Output is correct |
28 |
Correct |
52 ms |
167628 KB |
Output is correct |
29 |
Correct |
55 ms |
169040 KB |
Output is correct |
30 |
Correct |
52 ms |
168064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
166740 KB |
Output is correct |
2 |
Correct |
20 ms |
166760 KB |
Output is correct |
3 |
Correct |
20 ms |
166744 KB |
Output is correct |
4 |
Correct |
19 ms |
166748 KB |
Output is correct |
5 |
Correct |
21 ms |
166748 KB |
Output is correct |
6 |
Correct |
20 ms |
166744 KB |
Output is correct |
7 |
Correct |
21 ms |
166744 KB |
Output is correct |
8 |
Correct |
87 ms |
166744 KB |
Output is correct |
9 |
Correct |
86 ms |
166744 KB |
Output is correct |
10 |
Correct |
76 ms |
166748 KB |
Output is correct |
11 |
Correct |
59 ms |
167276 KB |
Output is correct |
12 |
Correct |
20 ms |
166736 KB |
Output is correct |
13 |
Correct |
21 ms |
166736 KB |
Output is correct |
14 |
Correct |
19 ms |
166744 KB |
Output is correct |
15 |
Correct |
21 ms |
166736 KB |
Output is correct |
16 |
Correct |
18 ms |
166748 KB |
Output is correct |
17 |
Correct |
23 ms |
166748 KB |
Output is correct |
18 |
Correct |
23 ms |
166692 KB |
Output is correct |
19 |
Correct |
88 ms |
166796 KB |
Output is correct |
20 |
Correct |
55 ms |
166736 KB |
Output is correct |
21 |
Correct |
60 ms |
166748 KB |
Output is correct |
22 |
Correct |
49 ms |
166740 KB |
Output is correct |
23 |
Correct |
51 ms |
166748 KB |
Output is correct |
24 |
Correct |
41 ms |
167328 KB |
Output is correct |
25 |
Correct |
40 ms |
168020 KB |
Output is correct |
26 |
Correct |
40 ms |
167508 KB |
Output is correct |
27 |
Correct |
20 ms |
166740 KB |
Output is correct |
28 |
Correct |
18 ms |
166748 KB |
Output is correct |
29 |
Correct |
18 ms |
166748 KB |
Output is correct |
30 |
Correct |
20 ms |
166820 KB |
Output is correct |
31 |
Correct |
19 ms |
166748 KB |
Output is correct |
32 |
Correct |
20 ms |
166608 KB |
Output is correct |
33 |
Correct |
19 ms |
166748 KB |
Output is correct |
34 |
Correct |
19 ms |
166756 KB |
Output is correct |
35 |
Correct |
19 ms |
166696 KB |
Output is correct |
36 |
Correct |
82 ms |
167248 KB |
Output is correct |
37 |
Correct |
54 ms |
168276 KB |
Output is correct |
38 |
Correct |
53 ms |
168868 KB |
Output is correct |
39 |
Correct |
57 ms |
169296 KB |
Output is correct |
40 |
Correct |
56 ms |
168484 KB |
Output is correct |
41 |
Correct |
36 ms |
167084 KB |
Output is correct |
42 |
Correct |
36 ms |
167820 KB |
Output is correct |
43 |
Correct |
56 ms |
168272 KB |
Output is correct |
44 |
Correct |
22 ms |
166740 KB |
Output is correct |
45 |
Correct |
18 ms |
166604 KB |
Output is correct |
46 |
Correct |
20 ms |
166748 KB |
Output is correct |
47 |
Correct |
20 ms |
166748 KB |
Output is correct |
48 |
Correct |
20 ms |
166748 KB |
Output is correct |
49 |
Correct |
22 ms |
166740 KB |
Output is correct |
50 |
Correct |
20 ms |
166740 KB |
Output is correct |
51 |
Correct |
21 ms |
166748 KB |
Output is correct |
52 |
Correct |
21 ms |
166656 KB |
Output is correct |
53 |
Correct |
81 ms |
167420 KB |
Output is correct |
54 |
Correct |
52 ms |
167628 KB |
Output is correct |
55 |
Correct |
55 ms |
169040 KB |
Output is correct |
56 |
Correct |
52 ms |
168064 KB |
Output is correct |
57 |
Correct |
68 ms |
170064 KB |
Output is correct |
58 |
Correct |
99 ms |
167768 KB |
Output is correct |
59 |
Correct |
61 ms |
168276 KB |
Output is correct |