#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
#ifdef LOCAL
#include "algo/debug.h"
#endif
#define f first
#define s second
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
template<class T> int lwb(V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
const int max_k = 5, max_n = 5e4;
int k, n, m, q, jump[16][max_n][max_k][max_k];
void solve() {
cin >> k >> n >> m >> q;
for (int stp = 0; stp < 16; stp++) {
for (int i = 0; i < n; i++) {
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
jump[stp][i][x][y] = inf;
}
}
}
}
for (int i = 0; i < m; i++) {
int u, v, w; cin >> u >> v >> w;
jump[0][u/k][u%k][v%k] = w;
}
for (int stp = 1; stp < 16; stp++) {
for (int i = 0; i < (n-1)/k; i++) if (i + (1 << (stp-1)) <= (n-1)/k){
for (int z = 0; z < k; z++) {
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
jump[stp][i][x][y] = min(jump[stp][i][x][y], jump[stp-1][i][x][z]+jump[stp-1][i+(1 << (stp-1))][z][y]);
}
}
}
}
}
for (int i = 0; i < q; i++) {
vi dist(k, inf);
int u, v; cin >> u >> v;
int gap = v/k - u/k;
int pos = u/k;
int x = u%k;
dist[x] = 0;
for (int stp = 15; stp >= 0; stp--) if (gap & (1 << stp)) {
auto DIST = dist;
dist.assign(k, inf);
for (int z = 0; z < k; z++) {
for (int y = 0; y < k; y++) {
dist[y] = min(dist[y], DIST[z]+jump[stp][pos][z][y]);
}
}
pos += 1 << stp;
}
int ans = dist[v%k];
if (ans == inf) ans = -1;
cout << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
79700 KB |
Output is correct |
2 |
Correct |
3 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31068 KB |
Output is correct |
4 |
Correct |
4 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
33416 KB |
Output is correct |
6 |
Correct |
5 ms |
33372 KB |
Output is correct |
7 |
Correct |
4 ms |
33368 KB |
Output is correct |
8 |
Correct |
42 ms |
79452 KB |
Output is correct |
9 |
Correct |
42 ms |
79452 KB |
Output is correct |
10 |
Correct |
34 ms |
78688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
80236 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
4 ms |
31068 KB |
Output is correct |
4 |
Correct |
4 ms |
31068 KB |
Output is correct |
5 |
Correct |
4 ms |
31068 KB |
Output is correct |
6 |
Correct |
4 ms |
31068 KB |
Output is correct |
7 |
Correct |
8 ms |
33404 KB |
Output is correct |
8 |
Correct |
8 ms |
33372 KB |
Output is correct |
9 |
Correct |
36 ms |
79612 KB |
Output is correct |
10 |
Correct |
49 ms |
80976 KB |
Output is correct |
11 |
Correct |
41 ms |
80468 KB |
Output is correct |
12 |
Correct |
42 ms |
79996 KB |
Output is correct |
13 |
Correct |
49 ms |
80976 KB |
Output is correct |
14 |
Correct |
33 ms |
79964 KB |
Output is correct |
15 |
Correct |
37 ms |
79816 KB |
Output is correct |
16 |
Correct |
38 ms |
79708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
31064 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
4 ms |
31068 KB |
Output is correct |
4 |
Correct |
4 ms |
31236 KB |
Output is correct |
5 |
Correct |
4 ms |
31240 KB |
Output is correct |
6 |
Correct |
4 ms |
33368 KB |
Output is correct |
7 |
Correct |
5 ms |
33372 KB |
Output is correct |
8 |
Correct |
5 ms |
33452 KB |
Output is correct |
9 |
Correct |
5 ms |
33368 KB |
Output is correct |
10 |
Correct |
30 ms |
79440 KB |
Output is correct |
11 |
Correct |
33 ms |
80016 KB |
Output is correct |
12 |
Correct |
43 ms |
80888 KB |
Output is correct |
13 |
Correct |
44 ms |
80936 KB |
Output is correct |
14 |
Correct |
44 ms |
80596 KB |
Output is correct |
15 |
Correct |
35 ms |
79708 KB |
Output is correct |
16 |
Correct |
34 ms |
79708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
31064 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
4 ms |
31068 KB |
Output is correct |
4 |
Correct |
4 ms |
31236 KB |
Output is correct |
5 |
Correct |
4 ms |
31240 KB |
Output is correct |
6 |
Correct |
4 ms |
33368 KB |
Output is correct |
7 |
Correct |
5 ms |
33372 KB |
Output is correct |
8 |
Correct |
5 ms |
33452 KB |
Output is correct |
9 |
Correct |
5 ms |
33368 KB |
Output is correct |
10 |
Correct |
30 ms |
79440 KB |
Output is correct |
11 |
Correct |
33 ms |
80016 KB |
Output is correct |
12 |
Correct |
43 ms |
80888 KB |
Output is correct |
13 |
Correct |
44 ms |
80936 KB |
Output is correct |
14 |
Correct |
44 ms |
80596 KB |
Output is correct |
15 |
Correct |
35 ms |
79708 KB |
Output is correct |
16 |
Correct |
34 ms |
79708 KB |
Output is correct |
17 |
Correct |
36 ms |
80292 KB |
Output is correct |
18 |
Correct |
4 ms |
31068 KB |
Output is correct |
19 |
Correct |
4 ms |
31064 KB |
Output is correct |
20 |
Correct |
4 ms |
31068 KB |
Output is correct |
21 |
Correct |
4 ms |
31068 KB |
Output is correct |
22 |
Correct |
4 ms |
31068 KB |
Output is correct |
23 |
Correct |
7 ms |
33880 KB |
Output is correct |
24 |
Correct |
5 ms |
33436 KB |
Output is correct |
25 |
Correct |
6 ms |
33372 KB |
Output is correct |
26 |
Correct |
6 ms |
33372 KB |
Output is correct |
27 |
Correct |
33 ms |
79452 KB |
Output is correct |
28 |
Correct |
45 ms |
80760 KB |
Output is correct |
29 |
Correct |
47 ms |
80988 KB |
Output is correct |
30 |
Correct |
41 ms |
80468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
79700 KB |
Output is correct |
2 |
Correct |
3 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31068 KB |
Output is correct |
4 |
Correct |
4 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
33416 KB |
Output is correct |
6 |
Correct |
5 ms |
33372 KB |
Output is correct |
7 |
Correct |
4 ms |
33368 KB |
Output is correct |
8 |
Correct |
42 ms |
79452 KB |
Output is correct |
9 |
Correct |
42 ms |
79452 KB |
Output is correct |
10 |
Correct |
34 ms |
78688 KB |
Output is correct |
11 |
Correct |
44 ms |
80236 KB |
Output is correct |
12 |
Correct |
4 ms |
31068 KB |
Output is correct |
13 |
Correct |
4 ms |
31068 KB |
Output is correct |
14 |
Correct |
4 ms |
31068 KB |
Output is correct |
15 |
Correct |
4 ms |
31068 KB |
Output is correct |
16 |
Correct |
4 ms |
31068 KB |
Output is correct |
17 |
Correct |
8 ms |
33404 KB |
Output is correct |
18 |
Correct |
8 ms |
33372 KB |
Output is correct |
19 |
Correct |
36 ms |
79612 KB |
Output is correct |
20 |
Correct |
49 ms |
80976 KB |
Output is correct |
21 |
Correct |
41 ms |
80468 KB |
Output is correct |
22 |
Correct |
42 ms |
79996 KB |
Output is correct |
23 |
Correct |
49 ms |
80976 KB |
Output is correct |
24 |
Correct |
33 ms |
79964 KB |
Output is correct |
25 |
Correct |
37 ms |
79816 KB |
Output is correct |
26 |
Correct |
38 ms |
79708 KB |
Output is correct |
27 |
Correct |
4 ms |
31064 KB |
Output is correct |
28 |
Correct |
4 ms |
31068 KB |
Output is correct |
29 |
Correct |
4 ms |
31068 KB |
Output is correct |
30 |
Correct |
4 ms |
31236 KB |
Output is correct |
31 |
Correct |
4 ms |
31240 KB |
Output is correct |
32 |
Correct |
4 ms |
33368 KB |
Output is correct |
33 |
Correct |
5 ms |
33372 KB |
Output is correct |
34 |
Correct |
5 ms |
33452 KB |
Output is correct |
35 |
Correct |
5 ms |
33368 KB |
Output is correct |
36 |
Correct |
30 ms |
79440 KB |
Output is correct |
37 |
Correct |
33 ms |
80016 KB |
Output is correct |
38 |
Correct |
43 ms |
80888 KB |
Output is correct |
39 |
Correct |
44 ms |
80936 KB |
Output is correct |
40 |
Correct |
44 ms |
80596 KB |
Output is correct |
41 |
Correct |
35 ms |
79708 KB |
Output is correct |
42 |
Correct |
34 ms |
79708 KB |
Output is correct |
43 |
Correct |
36 ms |
80292 KB |
Output is correct |
44 |
Correct |
4 ms |
31068 KB |
Output is correct |
45 |
Correct |
4 ms |
31064 KB |
Output is correct |
46 |
Correct |
4 ms |
31068 KB |
Output is correct |
47 |
Correct |
4 ms |
31068 KB |
Output is correct |
48 |
Correct |
4 ms |
31068 KB |
Output is correct |
49 |
Correct |
7 ms |
33880 KB |
Output is correct |
50 |
Correct |
5 ms |
33436 KB |
Output is correct |
51 |
Correct |
6 ms |
33372 KB |
Output is correct |
52 |
Correct |
6 ms |
33372 KB |
Output is correct |
53 |
Correct |
33 ms |
79452 KB |
Output is correct |
54 |
Correct |
45 ms |
80760 KB |
Output is correct |
55 |
Correct |
47 ms |
80988 KB |
Output is correct |
56 |
Correct |
41 ms |
80468 KB |
Output is correct |
57 |
Correct |
72 ms |
82004 KB |
Output is correct |
58 |
Correct |
41 ms |
79448 KB |
Output is correct |
59 |
Correct |
44 ms |
80464 KB |
Output is correct |