#include <bits/stdc++.h>
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
using namespace std;
const int N = 5e5 + 5, K = 5, INF = 6e8;
int w[N][K][K];
int n, m, k, o;
struct Node {
int w[K][K];
void fill(int val) {
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
w[i][j] = val;
}
}
}
} t[N * 4];
Node mrg(Node a, Node b, int w[K][K]) {
Node res;
res.fill(INF);
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++) {
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
res.w[i][y] = min(res.w[i][y], a.w[i][j] + w[j][x] + b.w[x][y]);
}
}
}
}
// for (int i = 0; i < k; i++) {
// for (int j = 0; j < k; j++) {
// cout << i << " " << j << " " << a.w[i][j] << "\n";
// }
// }
// cout << "\n";
// for (int i = 0; i < k; i++) {
// for (int j = 0; j < k; j++) {
// cout << i << " " << j << " " << b.w[i][j] << "\n";
// }
// }
// cout << "\n";
// for (int i = 0; i < k; i++) {
// for (int j = 0; j < k; j++) {
// cout << i << " " << j << " " << res.w[i][j] << "\n";
// }
// }
// cout << "\n";
// for (int i = 0; i < k; i++) {
// for (int j = 0; j < k; j++) {
// cout << i << " " << j << " " << w[i][j] << "\n";
// }
// }
// cout << "\n";
// cout << "\n";
return res;
}
void build(int v, int L, int R) {
if (L == R) {
t[v].fill(INF);
for (int i = 0; i < k; i++)
t[v].w[i][i] = 0;
}
else {
int m = (L + R) >> 1;
build(v * 2, L, m);
build(v * 2 + 1, m + 1, R);
t[v] = mrg(t[v * 2], t[v * 2 + 1], w[m]);
}
}
Node que(int l, int r, int v, int L, int R) {
if (l == L && r == R)
return t[v];
int m = (L + R) >> 1;
if (r <= m)
return que(l, min(m, r), v * 2, L, m);
if (l > m)
return que(max(m + 1, l), r, v * 2 + 1, m + 1, R);
return mrg(que(l, min(m, r), v * 2, L, m),
que(max(m + 1, l), r, v * 2 + 1, m + 1, R), w[m]);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> k >> n >> m >> o;
int ctb = (n - 1) / k + 1;
for (int i = 0; i < ctb; i++) {
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
w[i][x][y] = INF;
}
}
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
int p = a / k;
a %= k;
b %= k;
w[p][a][b] = min(w[p][a][b], c);
// cout << p << " " << a << " " << b << " " << c << "\n";
}
build(1, 0, ctb - 1);
for (int i = 0; i < o; i++) {
int a, b;
cin >> a >> b;
int x = a / k,
y = b / k;
if (x == y) {
cout << "-1\n";
}
else {
Node res = que(x, y, 1, 0, ctb - 1);
int ans = res.w[a % k][b % k];
cout << (ans < INF ? ans : -1) << "\n";
// for (int i = 0; i < k; i++) {
// for (int j = 0; j < k; j++) {
// cout << i << " " << j << " " << res.w[i][j] << "\n";
// }
// }
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
18296 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
4 ms |
632 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
58 ms |
18168 KB |
Output is correct |
9 |
Correct |
54 ms |
18168 KB |
Output is correct |
10 |
Correct |
34 ms |
18172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
9336 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
7 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
14 ms |
632 KB |
Output is correct |
8 |
Correct |
8 ms |
632 KB |
Output is correct |
9 |
Correct |
55 ms |
18168 KB |
Output is correct |
10 |
Correct |
141 ms |
6472 KB |
Output is correct |
11 |
Correct |
53 ms |
9464 KB |
Output is correct |
12 |
Correct |
48 ms |
6520 KB |
Output is correct |
13 |
Correct |
75 ms |
2680 KB |
Output is correct |
14 |
Correct |
40 ms |
4728 KB |
Output is correct |
15 |
Correct |
51 ms |
2652 KB |
Output is correct |
16 |
Correct |
50 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
508 KB |
Output is correct |
8 |
Correct |
4 ms |
504 KB |
Output is correct |
9 |
Correct |
4 ms |
508 KB |
Output is correct |
10 |
Correct |
42 ms |
18168 KB |
Output is correct |
11 |
Correct |
44 ms |
9328 KB |
Output is correct |
12 |
Correct |
58 ms |
6520 KB |
Output is correct |
13 |
Correct |
64 ms |
6392 KB |
Output is correct |
14 |
Correct |
47 ms |
6392 KB |
Output is correct |
15 |
Correct |
35 ms |
2680 KB |
Output is correct |
16 |
Correct |
35 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
508 KB |
Output is correct |
8 |
Correct |
4 ms |
504 KB |
Output is correct |
9 |
Correct |
4 ms |
508 KB |
Output is correct |
10 |
Correct |
42 ms |
18168 KB |
Output is correct |
11 |
Correct |
44 ms |
9328 KB |
Output is correct |
12 |
Correct |
58 ms |
6520 KB |
Output is correct |
13 |
Correct |
64 ms |
6392 KB |
Output is correct |
14 |
Correct |
47 ms |
6392 KB |
Output is correct |
15 |
Correct |
35 ms |
2680 KB |
Output is correct |
16 |
Correct |
35 ms |
2680 KB |
Output is correct |
17 |
Correct |
51 ms |
9360 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
376 KB |
Output is correct |
23 |
Correct |
4 ms |
632 KB |
Output is correct |
24 |
Correct |
5 ms |
504 KB |
Output is correct |
25 |
Correct |
19 ms |
504 KB |
Output is correct |
26 |
Correct |
11 ms |
504 KB |
Output is correct |
27 |
Correct |
44 ms |
18168 KB |
Output is correct |
28 |
Correct |
66 ms |
6392 KB |
Output is correct |
29 |
Correct |
70 ms |
6460 KB |
Output is correct |
30 |
Correct |
58 ms |
6392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
18296 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
4 ms |
632 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
58 ms |
18168 KB |
Output is correct |
9 |
Correct |
54 ms |
18168 KB |
Output is correct |
10 |
Correct |
34 ms |
18172 KB |
Output is correct |
11 |
Correct |
54 ms |
9336 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
7 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
14 ms |
632 KB |
Output is correct |
18 |
Correct |
8 ms |
632 KB |
Output is correct |
19 |
Correct |
55 ms |
18168 KB |
Output is correct |
20 |
Correct |
141 ms |
6472 KB |
Output is correct |
21 |
Correct |
53 ms |
9464 KB |
Output is correct |
22 |
Correct |
48 ms |
6520 KB |
Output is correct |
23 |
Correct |
75 ms |
2680 KB |
Output is correct |
24 |
Correct |
40 ms |
4728 KB |
Output is correct |
25 |
Correct |
51 ms |
2652 KB |
Output is correct |
26 |
Correct |
50 ms |
2680 KB |
Output is correct |
27 |
Correct |
2 ms |
376 KB |
Output is correct |
28 |
Correct |
2 ms |
376 KB |
Output is correct |
29 |
Correct |
2 ms |
376 KB |
Output is correct |
30 |
Correct |
2 ms |
376 KB |
Output is correct |
31 |
Correct |
2 ms |
376 KB |
Output is correct |
32 |
Correct |
3 ms |
632 KB |
Output is correct |
33 |
Correct |
3 ms |
508 KB |
Output is correct |
34 |
Correct |
4 ms |
504 KB |
Output is correct |
35 |
Correct |
4 ms |
508 KB |
Output is correct |
36 |
Correct |
42 ms |
18168 KB |
Output is correct |
37 |
Correct |
44 ms |
9328 KB |
Output is correct |
38 |
Correct |
58 ms |
6520 KB |
Output is correct |
39 |
Correct |
64 ms |
6392 KB |
Output is correct |
40 |
Correct |
47 ms |
6392 KB |
Output is correct |
41 |
Correct |
35 ms |
2680 KB |
Output is correct |
42 |
Correct |
35 ms |
2680 KB |
Output is correct |
43 |
Correct |
51 ms |
9360 KB |
Output is correct |
44 |
Correct |
2 ms |
376 KB |
Output is correct |
45 |
Correct |
2 ms |
376 KB |
Output is correct |
46 |
Correct |
2 ms |
376 KB |
Output is correct |
47 |
Correct |
2 ms |
380 KB |
Output is correct |
48 |
Correct |
2 ms |
376 KB |
Output is correct |
49 |
Correct |
4 ms |
632 KB |
Output is correct |
50 |
Correct |
5 ms |
504 KB |
Output is correct |
51 |
Correct |
19 ms |
504 KB |
Output is correct |
52 |
Correct |
11 ms |
504 KB |
Output is correct |
53 |
Correct |
44 ms |
18168 KB |
Output is correct |
54 |
Correct |
66 ms |
6392 KB |
Output is correct |
55 |
Correct |
70 ms |
6460 KB |
Output is correct |
56 |
Correct |
58 ms |
6392 KB |
Output is correct |
57 |
Correct |
135 ms |
5004 KB |
Output is correct |
58 |
Correct |
55 ms |
19064 KB |
Output is correct |
59 |
Correct |
63 ms |
11000 KB |
Output is correct |