#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 5E4 + 5;
constexpr int L = 20;
constexpr int Inf = 1E9 + 5;
int ans[N][L][5][5];
void combine(int c[5][5], int a[5][5], int b[5][5]) {
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
for(int k = 0; k < 5; k++) {
c[i][j] = std::min(c[i][j], a[i][k] + b[k][j]);
}
}
}
}
void solve() {
std::fill_n(&ans[0][0][0][0], N * L * 5 * 5, Inf);
int k, n, m, o;
std::cin >> k >> n >> m >> o;
for(int i = 1; i <= m; i++) {
int a, b, t;
std::cin >> a >> b >> t;
ans[a / k][0][a % k][b % k] = t;
}
for(int lg = 1; lg < L; lg++) {
for(int i = 0; i + (1 << lg) < (n + k - 1) / k; i++) {
combine(ans[i][lg], ans[i][lg - 1], ans[i + (1 << (lg - 1))][lg - 1]);
}
}
while(o--) {
int a, b;
std::cin >> a >> b;
int res[5][5], cur = a / k;
std::fill_n(&res[0][0], 5 * 5, Inf);
for(int i = 0; i < 5; i++) res[i][i] = 0;
for(int lg = L - 1; lg >= 0; lg--) {
if(cur + (1 << lg) <= b / k) {
int tmp[5][5];
std::fill_n(&tmp[0][0], 5 * 5, Inf);
combine(tmp, res, ans[cur][lg]);
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
res[i][j] = tmp[i][j];
}
}
cur += (1 << lg);
}
}
std::cout << (res[a % k][b % k] == Inf ? -1 : res[a % k][b % k]) << "\n";
}
return;
}
signed main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL); std::cout.tie(NULL);
int t = 1; //std::cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
99160 KB |
Output is correct |
2 |
Correct |
27 ms |
98132 KB |
Output is correct |
3 |
Correct |
26 ms |
98136 KB |
Output is correct |
4 |
Correct |
27 ms |
98140 KB |
Output is correct |
5 |
Correct |
29 ms |
98132 KB |
Output is correct |
6 |
Correct |
29 ms |
98264 KB |
Output is correct |
7 |
Correct |
30 ms |
98140 KB |
Output is correct |
8 |
Correct |
230 ms |
99060 KB |
Output is correct |
9 |
Correct |
236 ms |
98896 KB |
Output is correct |
10 |
Correct |
217 ms |
98384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
99316 KB |
Output is correct |
2 |
Correct |
28 ms |
98128 KB |
Output is correct |
3 |
Correct |
28 ms |
98140 KB |
Output is correct |
4 |
Correct |
28 ms |
98128 KB |
Output is correct |
5 |
Correct |
27 ms |
98132 KB |
Output is correct |
6 |
Correct |
28 ms |
98140 KB |
Output is correct |
7 |
Correct |
38 ms |
98384 KB |
Output is correct |
8 |
Correct |
38 ms |
98420 KB |
Output is correct |
9 |
Correct |
214 ms |
98896 KB |
Output is correct |
10 |
Correct |
91 ms |
99604 KB |
Output is correct |
11 |
Correct |
112 ms |
99316 KB |
Output is correct |
12 |
Correct |
80 ms |
99156 KB |
Output is correct |
13 |
Correct |
68 ms |
99668 KB |
Output is correct |
14 |
Correct |
61 ms |
99316 KB |
Output is correct |
15 |
Correct |
54 ms |
99020 KB |
Output is correct |
16 |
Correct |
52 ms |
98896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
98132 KB |
Output is correct |
2 |
Correct |
28 ms |
98132 KB |
Output is correct |
3 |
Correct |
27 ms |
98140 KB |
Output is correct |
4 |
Correct |
28 ms |
98128 KB |
Output is correct |
5 |
Correct |
28 ms |
98128 KB |
Output is correct |
6 |
Correct |
29 ms |
98140 KB |
Output is correct |
7 |
Correct |
28 ms |
98204 KB |
Output is correct |
8 |
Correct |
28 ms |
98132 KB |
Output is correct |
9 |
Correct |
27 ms |
98312 KB |
Output is correct |
10 |
Correct |
218 ms |
99132 KB |
Output is correct |
11 |
Correct |
92 ms |
99156 KB |
Output is correct |
12 |
Correct |
86 ms |
99412 KB |
Output is correct |
13 |
Correct |
81 ms |
99668 KB |
Output is correct |
14 |
Correct |
75 ms |
99536 KB |
Output is correct |
15 |
Correct |
51 ms |
99068 KB |
Output is correct |
16 |
Correct |
50 ms |
98904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
98132 KB |
Output is correct |
2 |
Correct |
28 ms |
98132 KB |
Output is correct |
3 |
Correct |
27 ms |
98140 KB |
Output is correct |
4 |
Correct |
28 ms |
98128 KB |
Output is correct |
5 |
Correct |
28 ms |
98128 KB |
Output is correct |
6 |
Correct |
29 ms |
98140 KB |
Output is correct |
7 |
Correct |
28 ms |
98204 KB |
Output is correct |
8 |
Correct |
28 ms |
98132 KB |
Output is correct |
9 |
Correct |
27 ms |
98312 KB |
Output is correct |
10 |
Correct |
218 ms |
99132 KB |
Output is correct |
11 |
Correct |
92 ms |
99156 KB |
Output is correct |
12 |
Correct |
86 ms |
99412 KB |
Output is correct |
13 |
Correct |
81 ms |
99668 KB |
Output is correct |
14 |
Correct |
75 ms |
99536 KB |
Output is correct |
15 |
Correct |
51 ms |
99068 KB |
Output is correct |
16 |
Correct |
50 ms |
98904 KB |
Output is correct |
17 |
Correct |
99 ms |
99316 KB |
Output is correct |
18 |
Correct |
27 ms |
98128 KB |
Output is correct |
19 |
Correct |
28 ms |
98132 KB |
Output is correct |
20 |
Correct |
30 ms |
98140 KB |
Output is correct |
21 |
Correct |
27 ms |
98076 KB |
Output is correct |
22 |
Correct |
27 ms |
98136 KB |
Output is correct |
23 |
Correct |
31 ms |
98100 KB |
Output is correct |
24 |
Correct |
31 ms |
98304 KB |
Output is correct |
25 |
Correct |
30 ms |
98316 KB |
Output is correct |
26 |
Correct |
30 ms |
98196 KB |
Output is correct |
27 |
Correct |
228 ms |
98984 KB |
Output is correct |
28 |
Correct |
87 ms |
99596 KB |
Output is correct |
29 |
Correct |
87 ms |
99692 KB |
Output is correct |
30 |
Correct |
80 ms |
99596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
231 ms |
99160 KB |
Output is correct |
2 |
Correct |
27 ms |
98132 KB |
Output is correct |
3 |
Correct |
26 ms |
98136 KB |
Output is correct |
4 |
Correct |
27 ms |
98140 KB |
Output is correct |
5 |
Correct |
29 ms |
98132 KB |
Output is correct |
6 |
Correct |
29 ms |
98264 KB |
Output is correct |
7 |
Correct |
30 ms |
98140 KB |
Output is correct |
8 |
Correct |
230 ms |
99060 KB |
Output is correct |
9 |
Correct |
236 ms |
98896 KB |
Output is correct |
10 |
Correct |
217 ms |
98384 KB |
Output is correct |
11 |
Correct |
107 ms |
99316 KB |
Output is correct |
12 |
Correct |
28 ms |
98128 KB |
Output is correct |
13 |
Correct |
28 ms |
98140 KB |
Output is correct |
14 |
Correct |
28 ms |
98128 KB |
Output is correct |
15 |
Correct |
27 ms |
98132 KB |
Output is correct |
16 |
Correct |
28 ms |
98140 KB |
Output is correct |
17 |
Correct |
38 ms |
98384 KB |
Output is correct |
18 |
Correct |
38 ms |
98420 KB |
Output is correct |
19 |
Correct |
214 ms |
98896 KB |
Output is correct |
20 |
Correct |
91 ms |
99604 KB |
Output is correct |
21 |
Correct |
112 ms |
99316 KB |
Output is correct |
22 |
Correct |
80 ms |
99156 KB |
Output is correct |
23 |
Correct |
68 ms |
99668 KB |
Output is correct |
24 |
Correct |
61 ms |
99316 KB |
Output is correct |
25 |
Correct |
54 ms |
99020 KB |
Output is correct |
26 |
Correct |
52 ms |
98896 KB |
Output is correct |
27 |
Correct |
27 ms |
98132 KB |
Output is correct |
28 |
Correct |
28 ms |
98132 KB |
Output is correct |
29 |
Correct |
27 ms |
98140 KB |
Output is correct |
30 |
Correct |
28 ms |
98128 KB |
Output is correct |
31 |
Correct |
28 ms |
98128 KB |
Output is correct |
32 |
Correct |
29 ms |
98140 KB |
Output is correct |
33 |
Correct |
28 ms |
98204 KB |
Output is correct |
34 |
Correct |
28 ms |
98132 KB |
Output is correct |
35 |
Correct |
27 ms |
98312 KB |
Output is correct |
36 |
Correct |
218 ms |
99132 KB |
Output is correct |
37 |
Correct |
92 ms |
99156 KB |
Output is correct |
38 |
Correct |
86 ms |
99412 KB |
Output is correct |
39 |
Correct |
81 ms |
99668 KB |
Output is correct |
40 |
Correct |
75 ms |
99536 KB |
Output is correct |
41 |
Correct |
51 ms |
99068 KB |
Output is correct |
42 |
Correct |
50 ms |
98904 KB |
Output is correct |
43 |
Correct |
99 ms |
99316 KB |
Output is correct |
44 |
Correct |
27 ms |
98128 KB |
Output is correct |
45 |
Correct |
28 ms |
98132 KB |
Output is correct |
46 |
Correct |
30 ms |
98140 KB |
Output is correct |
47 |
Correct |
27 ms |
98076 KB |
Output is correct |
48 |
Correct |
27 ms |
98136 KB |
Output is correct |
49 |
Correct |
31 ms |
98100 KB |
Output is correct |
50 |
Correct |
31 ms |
98304 KB |
Output is correct |
51 |
Correct |
30 ms |
98316 KB |
Output is correct |
52 |
Correct |
30 ms |
98196 KB |
Output is correct |
53 |
Correct |
228 ms |
98984 KB |
Output is correct |
54 |
Correct |
87 ms |
99596 KB |
Output is correct |
55 |
Correct |
87 ms |
99692 KB |
Output is correct |
56 |
Correct |
80 ms |
99596 KB |
Output is correct |
57 |
Correct |
98 ms |
100180 KB |
Output is correct |
58 |
Correct |
230 ms |
99064 KB |
Output is correct |
59 |
Correct |
117 ms |
99324 KB |
Output is correct |