#include <iostream>
using namespace std;
struct MATRIX {
long long int a[5][5];
MATRIX() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
a[i][j] = 999999999;
}
}
}
};
MATRIX f[50005][18];
MATRIX mul(MATRIX a, MATRIX b){
MATRIX res;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
res.a[i][j] = 999999999;
for (int k = 0; k < 5; k++) {
res.a[i][j] = min(res.a[i][j], a.a[i][k] + b.a[k][j]);
}
}
}
return res;
}
int main() {
int n, k, m, q;
cin >> k >> n >> m >> q;
for (int i = 1; i <= m; i++) {
int a, b, t;
cin >> a >> b >> t;
f[a / k][0].a[a % k][b % k] = t;
}
for (int i = 1; (1 << i) <= n; i++){
for (int j = 0; j + (1 << i) - 1 <= n; j++) {
f[j][i] = mul(f[j][i - 1], f[j + (1 << (i - 1))][i - 1]);
}
}
for (int jj = 0; jj < q; jj++) {
int a, b;
cin >> a >> b;
MATRIX s;
for (int j = 0; j < 5; j++) {
s.a[j][j] = 0;
}
if (a == b) {
cout << 0 << endl;
continue;
}
int h = a / k, g = b / k;
if (g <= h) {
cout << -1 << endl;
}
else {
int l = g - h;
for (int j = 16; j >= 0; j--) {
if (l & (1 << j)) {
s = mul(s, f[h][j]);
h += (1 << j);
}
}
if (s.a[a % k][b % k] == 999999999) cout << -1 << endl;
else cout << s.a[a % k][b % k] << endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
261 ms |
176464 KB |
Output is correct |
2 |
Correct |
24 ms |
176464 KB |
Output is correct |
3 |
Correct |
22 ms |
176464 KB |
Output is correct |
4 |
Correct |
23 ms |
176464 KB |
Output is correct |
5 |
Correct |
27 ms |
176476 KB |
Output is correct |
6 |
Correct |
28 ms |
176464 KB |
Output is correct |
7 |
Correct |
27 ms |
176464 KB |
Output is correct |
8 |
Correct |
251 ms |
176528 KB |
Output is correct |
9 |
Correct |
236 ms |
176564 KB |
Output is correct |
10 |
Correct |
191 ms |
176572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
176564 KB |
Output is correct |
2 |
Correct |
23 ms |
176464 KB |
Output is correct |
3 |
Correct |
23 ms |
176464 KB |
Output is correct |
4 |
Correct |
25 ms |
176556 KB |
Output is correct |
5 |
Correct |
25 ms |
176456 KB |
Output is correct |
6 |
Correct |
25 ms |
176464 KB |
Output is correct |
7 |
Correct |
50 ms |
176636 KB |
Output is correct |
8 |
Correct |
49 ms |
176456 KB |
Output is correct |
9 |
Correct |
211 ms |
176456 KB |
Output is correct |
10 |
Correct |
268 ms |
176532 KB |
Output is correct |
11 |
Correct |
253 ms |
176456 KB |
Output is correct |
12 |
Correct |
225 ms |
176456 KB |
Output is correct |
13 |
Correct |
177 ms |
176476 KB |
Output is correct |
14 |
Correct |
149 ms |
176464 KB |
Output is correct |
15 |
Correct |
135 ms |
176464 KB |
Output is correct |
16 |
Correct |
137 ms |
176464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
176464 KB |
Output is correct |
2 |
Correct |
24 ms |
176468 KB |
Output is correct |
3 |
Correct |
23 ms |
176464 KB |
Output is correct |
4 |
Correct |
25 ms |
176456 KB |
Output is correct |
5 |
Correct |
23 ms |
176464 KB |
Output is correct |
6 |
Correct |
26 ms |
176564 KB |
Output is correct |
7 |
Correct |
29 ms |
176480 KB |
Output is correct |
8 |
Correct |
27 ms |
176528 KB |
Output is correct |
9 |
Correct |
27 ms |
176464 KB |
Output is correct |
10 |
Correct |
173 ms |
176544 KB |
Output is correct |
11 |
Correct |
197 ms |
176672 KB |
Output is correct |
12 |
Correct |
226 ms |
176464 KB |
Output is correct |
13 |
Correct |
227 ms |
176456 KB |
Output is correct |
14 |
Correct |
209 ms |
176568 KB |
Output is correct |
15 |
Correct |
140 ms |
176504 KB |
Output is correct |
16 |
Correct |
130 ms |
176352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
176464 KB |
Output is correct |
2 |
Correct |
24 ms |
176468 KB |
Output is correct |
3 |
Correct |
23 ms |
176464 KB |
Output is correct |
4 |
Correct |
25 ms |
176456 KB |
Output is correct |
5 |
Correct |
23 ms |
176464 KB |
Output is correct |
6 |
Correct |
26 ms |
176564 KB |
Output is correct |
7 |
Correct |
29 ms |
176480 KB |
Output is correct |
8 |
Correct |
27 ms |
176528 KB |
Output is correct |
9 |
Correct |
27 ms |
176464 KB |
Output is correct |
10 |
Correct |
173 ms |
176544 KB |
Output is correct |
11 |
Correct |
197 ms |
176672 KB |
Output is correct |
12 |
Correct |
226 ms |
176464 KB |
Output is correct |
13 |
Correct |
227 ms |
176456 KB |
Output is correct |
14 |
Correct |
209 ms |
176568 KB |
Output is correct |
15 |
Correct |
140 ms |
176504 KB |
Output is correct |
16 |
Correct |
130 ms |
176352 KB |
Output is correct |
17 |
Correct |
191 ms |
176552 KB |
Output is correct |
18 |
Correct |
21 ms |
176456 KB |
Output is correct |
19 |
Correct |
21 ms |
176456 KB |
Output is correct |
20 |
Correct |
21 ms |
176520 KB |
Output is correct |
21 |
Correct |
22 ms |
176744 KB |
Output is correct |
22 |
Correct |
21 ms |
176504 KB |
Output is correct |
23 |
Correct |
32 ms |
176464 KB |
Output is correct |
24 |
Correct |
29 ms |
176464 KB |
Output is correct |
25 |
Correct |
34 ms |
176456 KB |
Output is correct |
26 |
Correct |
31 ms |
176464 KB |
Output is correct |
27 |
Correct |
170 ms |
176464 KB |
Output is correct |
28 |
Correct |
206 ms |
176568 KB |
Output is correct |
29 |
Correct |
210 ms |
176624 KB |
Output is correct |
30 |
Correct |
194 ms |
176456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
261 ms |
176464 KB |
Output is correct |
2 |
Correct |
24 ms |
176464 KB |
Output is correct |
3 |
Correct |
22 ms |
176464 KB |
Output is correct |
4 |
Correct |
23 ms |
176464 KB |
Output is correct |
5 |
Correct |
27 ms |
176476 KB |
Output is correct |
6 |
Correct |
28 ms |
176464 KB |
Output is correct |
7 |
Correct |
27 ms |
176464 KB |
Output is correct |
8 |
Correct |
251 ms |
176528 KB |
Output is correct |
9 |
Correct |
236 ms |
176564 KB |
Output is correct |
10 |
Correct |
191 ms |
176572 KB |
Output is correct |
11 |
Correct |
259 ms |
176564 KB |
Output is correct |
12 |
Correct |
23 ms |
176464 KB |
Output is correct |
13 |
Correct |
23 ms |
176464 KB |
Output is correct |
14 |
Correct |
25 ms |
176556 KB |
Output is correct |
15 |
Correct |
25 ms |
176456 KB |
Output is correct |
16 |
Correct |
25 ms |
176464 KB |
Output is correct |
17 |
Correct |
50 ms |
176636 KB |
Output is correct |
18 |
Correct |
49 ms |
176456 KB |
Output is correct |
19 |
Correct |
211 ms |
176456 KB |
Output is correct |
20 |
Correct |
268 ms |
176532 KB |
Output is correct |
21 |
Correct |
253 ms |
176456 KB |
Output is correct |
22 |
Correct |
225 ms |
176456 KB |
Output is correct |
23 |
Correct |
177 ms |
176476 KB |
Output is correct |
24 |
Correct |
149 ms |
176464 KB |
Output is correct |
25 |
Correct |
135 ms |
176464 KB |
Output is correct |
26 |
Correct |
137 ms |
176464 KB |
Output is correct |
27 |
Correct |
24 ms |
176464 KB |
Output is correct |
28 |
Correct |
24 ms |
176468 KB |
Output is correct |
29 |
Correct |
23 ms |
176464 KB |
Output is correct |
30 |
Correct |
25 ms |
176456 KB |
Output is correct |
31 |
Correct |
23 ms |
176464 KB |
Output is correct |
32 |
Correct |
26 ms |
176564 KB |
Output is correct |
33 |
Correct |
29 ms |
176480 KB |
Output is correct |
34 |
Correct |
27 ms |
176528 KB |
Output is correct |
35 |
Correct |
27 ms |
176464 KB |
Output is correct |
36 |
Correct |
173 ms |
176544 KB |
Output is correct |
37 |
Correct |
197 ms |
176672 KB |
Output is correct |
38 |
Correct |
226 ms |
176464 KB |
Output is correct |
39 |
Correct |
227 ms |
176456 KB |
Output is correct |
40 |
Correct |
209 ms |
176568 KB |
Output is correct |
41 |
Correct |
140 ms |
176504 KB |
Output is correct |
42 |
Correct |
130 ms |
176352 KB |
Output is correct |
43 |
Correct |
191 ms |
176552 KB |
Output is correct |
44 |
Correct |
21 ms |
176456 KB |
Output is correct |
45 |
Correct |
21 ms |
176456 KB |
Output is correct |
46 |
Correct |
21 ms |
176520 KB |
Output is correct |
47 |
Correct |
22 ms |
176744 KB |
Output is correct |
48 |
Correct |
21 ms |
176504 KB |
Output is correct |
49 |
Correct |
32 ms |
176464 KB |
Output is correct |
50 |
Correct |
29 ms |
176464 KB |
Output is correct |
51 |
Correct |
34 ms |
176456 KB |
Output is correct |
52 |
Correct |
31 ms |
176464 KB |
Output is correct |
53 |
Correct |
170 ms |
176464 KB |
Output is correct |
54 |
Correct |
206 ms |
176568 KB |
Output is correct |
55 |
Correct |
210 ms |
176624 KB |
Output is correct |
56 |
Correct |
194 ms |
176456 KB |
Output is correct |
57 |
Correct |
266 ms |
176628 KB |
Output is correct |
58 |
Correct |
230 ms |
176572 KB |
Output is correct |
59 |
Correct |
244 ms |
176456 KB |
Output is correct |