#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mxn = 3e5 + 10;
const int LOG = 20;
int ans[6][6], tmp[6][6];
int dp[mxn][LOG][6][6];
int k, n, m, q;
void ono_min(int &s, int t) {
if(s > t) s = t;
}
int box(int u) {
return u / k;
}
void build() {
for(int i = 1; i <= LOG; i++) {
for(int Lbox = 0; Lbox <= box(n - 1); Lbox++) {
int Mbox = Lbox + (1 << (i - 1));
int Rbox = Lbox + (1 << i);
if(Rbox > box(n - 1)) continue;
for(int l = 0; l < k; l++)
for(int r = 0; r < k; r++)
for(int m = 0; m < k; m++)
ono_min(dp[Lbox][i][l][r], dp[Lbox][i - 1][l][m] + dp[Mbox][i - 1][m][r]);
// cout << " > " << Lbox << ' ' << Rbox << '\n';
// for(int l = 0; l < k; l++)
// for(int r = 0; r < k; r++) {
// int Lnode = Lbox * k + l;
// int Rnode = Rbox * k + r;
// cout << Lnode << ' ' << Rnode << ": " << dp[Lbox][i][l][r] << '\n';
// }
}
}
}
int main() {
cin >> k >> n >> m >> q;
for(int i = 0; i <= box(n - 1); i++)
for(int j = 0; j < LOG; j++)
for(int l = 0; l < k; l++)
for(int r = 0; r < k; r++)
dp[i][j][l][r] = inf;
for(int i = 0; i < m; i++) {
int s, f, t;
cin >> s >> f >> t;
dp[box(s)][0][s % k][f % k] = t;
}
build();
while(q--) {
int Lnode, Rnode;
cin >> Lnode >> Rnode;
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++)
ans[i][j] = inf;
for(int i = 0; i < k; i++)
ans[i][i] = 0;
int Lbox = box(Lnode), Rbox = box(Rnode);
for(int lg = LOG - 1; lg >= 0; lg--) {
if(Lbox + (1 << lg) <= Rbox) {
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++)
tmp[i][j] = inf;
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++)
for(int m = 0; m < k; m++)
ono_min(tmp[i][j], ans[i][m] + dp[Lbox][lg][m][j]);
for(int i = 0; i < k; i++)
for(int j = 0; j < k; j++) {
// int Lnode = Lbox * k + i;
// int Rnode = Rbox * k + j;
// cout << Lnode << ' ' << Rnode << ": " << tmp[i][j] << '\n';
ans[i][j] = tmp[i][j];
}
Lbox += (1 << lg);
}
}
cout << ((ans[Lnode % k][Rnode % k] == inf) ? -1 : ans[Lnode % k][Rnode % k]) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
141184 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 |
5 ms |
3132 KB |
Output is correct |
6 |
Correct |
4 ms |
3028 KB |
Output is correct |
7 |
Correct |
4 ms |
3144 KB |
Output is correct |
8 |
Correct |
128 ms |
142032 KB |
Output is correct |
9 |
Correct |
127 ms |
142044 KB |
Output is correct |
10 |
Correct |
89 ms |
141356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
70744 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
19 ms |
3208 KB |
Output is correct |
8 |
Correct |
20 ms |
1748 KB |
Output is correct |
9 |
Correct |
125 ms |
142084 KB |
Output is correct |
10 |
Correct |
179 ms |
49536 KB |
Output is correct |
11 |
Correct |
146 ms |
72524 KB |
Output is correct |
12 |
Correct |
126 ms |
48588 KB |
Output is correct |
13 |
Correct |
141 ms |
19500 KB |
Output is correct |
14 |
Correct |
94 ms |
29864 KB |
Output is correct |
15 |
Correct |
91 ms |
18344 KB |
Output is correct |
16 |
Correct |
95 ms |
18344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
3 ms |
3028 KB |
Output is correct |
7 |
Correct |
2 ms |
1748 KB |
Output is correct |
8 |
Correct |
4 ms |
924 KB |
Output is correct |
9 |
Correct |
3 ms |
980 KB |
Output is correct |
10 |
Correct |
106 ms |
141980 KB |
Output is correct |
11 |
Correct |
125 ms |
72200 KB |
Output is correct |
12 |
Correct |
145 ms |
49412 KB |
Output is correct |
13 |
Correct |
152 ms |
49680 KB |
Output is correct |
14 |
Correct |
133 ms |
49032 KB |
Output is correct |
15 |
Correct |
91 ms |
18328 KB |
Output is correct |
16 |
Correct |
87 ms |
18376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
308 KB |
Output is correct |
6 |
Correct |
3 ms |
3028 KB |
Output is correct |
7 |
Correct |
2 ms |
1748 KB |
Output is correct |
8 |
Correct |
4 ms |
924 KB |
Output is correct |
9 |
Correct |
3 ms |
980 KB |
Output is correct |
10 |
Correct |
106 ms |
141980 KB |
Output is correct |
11 |
Correct |
125 ms |
72200 KB |
Output is correct |
12 |
Correct |
145 ms |
49412 KB |
Output is correct |
13 |
Correct |
152 ms |
49680 KB |
Output is correct |
14 |
Correct |
133 ms |
49032 KB |
Output is correct |
15 |
Correct |
91 ms |
18328 KB |
Output is correct |
16 |
Correct |
87 ms |
18376 KB |
Output is correct |
17 |
Correct |
129 ms |
72256 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
8 ms |
3156 KB |
Output is correct |
24 |
Correct |
8 ms |
1748 KB |
Output is correct |
25 |
Correct |
12 ms |
956 KB |
Output is correct |
26 |
Correct |
12 ms |
1088 KB |
Output is correct |
27 |
Correct |
114 ms |
142096 KB |
Output is correct |
28 |
Correct |
153 ms |
49456 KB |
Output is correct |
29 |
Correct |
168 ms |
49668 KB |
Output is correct |
30 |
Correct |
166 ms |
49028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
141184 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 |
5 ms |
3132 KB |
Output is correct |
6 |
Correct |
4 ms |
3028 KB |
Output is correct |
7 |
Correct |
4 ms |
3144 KB |
Output is correct |
8 |
Correct |
128 ms |
142032 KB |
Output is correct |
9 |
Correct |
127 ms |
142044 KB |
Output is correct |
10 |
Correct |
89 ms |
141356 KB |
Output is correct |
11 |
Correct |
140 ms |
70744 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
19 ms |
3208 KB |
Output is correct |
18 |
Correct |
20 ms |
1748 KB |
Output is correct |
19 |
Correct |
125 ms |
142084 KB |
Output is correct |
20 |
Correct |
179 ms |
49536 KB |
Output is correct |
21 |
Correct |
146 ms |
72524 KB |
Output is correct |
22 |
Correct |
126 ms |
48588 KB |
Output is correct |
23 |
Correct |
141 ms |
19500 KB |
Output is correct |
24 |
Correct |
94 ms |
29864 KB |
Output is correct |
25 |
Correct |
91 ms |
18344 KB |
Output is correct |
26 |
Correct |
95 ms |
18344 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
308 KB |
Output is correct |
32 |
Correct |
3 ms |
3028 KB |
Output is correct |
33 |
Correct |
2 ms |
1748 KB |
Output is correct |
34 |
Correct |
4 ms |
924 KB |
Output is correct |
35 |
Correct |
3 ms |
980 KB |
Output is correct |
36 |
Correct |
106 ms |
141980 KB |
Output is correct |
37 |
Correct |
125 ms |
72200 KB |
Output is correct |
38 |
Correct |
145 ms |
49412 KB |
Output is correct |
39 |
Correct |
152 ms |
49680 KB |
Output is correct |
40 |
Correct |
133 ms |
49032 KB |
Output is correct |
41 |
Correct |
91 ms |
18328 KB |
Output is correct |
42 |
Correct |
87 ms |
18376 KB |
Output is correct |
43 |
Correct |
129 ms |
72256 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
312 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
8 ms |
3156 KB |
Output is correct |
50 |
Correct |
8 ms |
1748 KB |
Output is correct |
51 |
Correct |
12 ms |
956 KB |
Output is correct |
52 |
Correct |
12 ms |
1088 KB |
Output is correct |
53 |
Correct |
114 ms |
142096 KB |
Output is correct |
54 |
Correct |
153 ms |
49456 KB |
Output is correct |
55 |
Correct |
168 ms |
49668 KB |
Output is correct |
56 |
Correct |
166 ms |
49028 KB |
Output is correct |
57 |
Correct |
218 ms |
38884 KB |
Output is correct |
58 |
Correct |
128 ms |
142176 KB |
Output is correct |
59 |
Correct |
145 ms |
72512 KB |
Output is correct |