#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 |
337 ms |
177336 KB |
Output is correct |
2 |
Correct |
67 ms |
176416 KB |
Output is correct |
3 |
Correct |
66 ms |
176308 KB |
Output is correct |
4 |
Correct |
66 ms |
176372 KB |
Output is correct |
5 |
Correct |
73 ms |
176408 KB |
Output is correct |
6 |
Correct |
74 ms |
176384 KB |
Output is correct |
7 |
Correct |
75 ms |
176328 KB |
Output is correct |
8 |
Correct |
347 ms |
177300 KB |
Output is correct |
9 |
Correct |
339 ms |
177296 KB |
Output is correct |
10 |
Correct |
313 ms |
176560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
177624 KB |
Output is correct |
2 |
Correct |
67 ms |
176304 KB |
Output is correct |
3 |
Correct |
68 ms |
176308 KB |
Output is correct |
4 |
Correct |
67 ms |
176308 KB |
Output is correct |
5 |
Correct |
70 ms |
176396 KB |
Output is correct |
6 |
Correct |
69 ms |
176392 KB |
Output is correct |
7 |
Correct |
93 ms |
176400 KB |
Output is correct |
8 |
Correct |
92 ms |
176488 KB |
Output is correct |
9 |
Correct |
331 ms |
177304 KB |
Output is correct |
10 |
Correct |
372 ms |
178740 KB |
Output is correct |
11 |
Correct |
356 ms |
178176 KB |
Output is correct |
12 |
Correct |
338 ms |
177724 KB |
Output is correct |
13 |
Correct |
311 ms |
178832 KB |
Output is correct |
14 |
Correct |
238 ms |
177764 KB |
Output is correct |
15 |
Correct |
231 ms |
177588 KB |
Output is correct |
16 |
Correct |
233 ms |
177628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
176344 KB |
Output is correct |
2 |
Correct |
68 ms |
176384 KB |
Output is correct |
3 |
Correct |
68 ms |
176352 KB |
Output is correct |
4 |
Correct |
72 ms |
176332 KB |
Output is correct |
5 |
Correct |
69 ms |
176424 KB |
Output is correct |
6 |
Correct |
71 ms |
176328 KB |
Output is correct |
7 |
Correct |
74 ms |
176384 KB |
Output is correct |
8 |
Correct |
72 ms |
176436 KB |
Output is correct |
9 |
Correct |
75 ms |
176588 KB |
Output is correct |
10 |
Correct |
305 ms |
177224 KB |
Output is correct |
11 |
Correct |
322 ms |
177852 KB |
Output is correct |
12 |
Correct |
358 ms |
178636 KB |
Output is correct |
13 |
Correct |
358 ms |
178764 KB |
Output is correct |
14 |
Correct |
334 ms |
178144 KB |
Output is correct |
15 |
Correct |
229 ms |
177604 KB |
Output is correct |
16 |
Correct |
228 ms |
177548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
176344 KB |
Output is correct |
2 |
Correct |
68 ms |
176384 KB |
Output is correct |
3 |
Correct |
68 ms |
176352 KB |
Output is correct |
4 |
Correct |
72 ms |
176332 KB |
Output is correct |
5 |
Correct |
69 ms |
176424 KB |
Output is correct |
6 |
Correct |
71 ms |
176328 KB |
Output is correct |
7 |
Correct |
74 ms |
176384 KB |
Output is correct |
8 |
Correct |
72 ms |
176436 KB |
Output is correct |
9 |
Correct |
75 ms |
176588 KB |
Output is correct |
10 |
Correct |
305 ms |
177224 KB |
Output is correct |
11 |
Correct |
322 ms |
177852 KB |
Output is correct |
12 |
Correct |
358 ms |
178636 KB |
Output is correct |
13 |
Correct |
358 ms |
178764 KB |
Output is correct |
14 |
Correct |
334 ms |
178144 KB |
Output is correct |
15 |
Correct |
229 ms |
177604 KB |
Output is correct |
16 |
Correct |
228 ms |
177548 KB |
Output is correct |
17 |
Correct |
340 ms |
177920 KB |
Output is correct |
18 |
Correct |
68 ms |
176392 KB |
Output is correct |
19 |
Correct |
68 ms |
176316 KB |
Output is correct |
20 |
Correct |
68 ms |
176388 KB |
Output is correct |
21 |
Correct |
68 ms |
176360 KB |
Output is correct |
22 |
Correct |
67 ms |
176376 KB |
Output is correct |
23 |
Correct |
82 ms |
176436 KB |
Output is correct |
24 |
Correct |
80 ms |
176392 KB |
Output is correct |
25 |
Correct |
78 ms |
176464 KB |
Output is correct |
26 |
Correct |
79 ms |
176496 KB |
Output is correct |
27 |
Correct |
312 ms |
177172 KB |
Output is correct |
28 |
Correct |
355 ms |
178628 KB |
Output is correct |
29 |
Correct |
370 ms |
178888 KB |
Output is correct |
30 |
Correct |
343 ms |
178260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
177336 KB |
Output is correct |
2 |
Correct |
67 ms |
176416 KB |
Output is correct |
3 |
Correct |
66 ms |
176308 KB |
Output is correct |
4 |
Correct |
66 ms |
176372 KB |
Output is correct |
5 |
Correct |
73 ms |
176408 KB |
Output is correct |
6 |
Correct |
74 ms |
176384 KB |
Output is correct |
7 |
Correct |
75 ms |
176328 KB |
Output is correct |
8 |
Correct |
347 ms |
177300 KB |
Output is correct |
9 |
Correct |
339 ms |
177296 KB |
Output is correct |
10 |
Correct |
313 ms |
176560 KB |
Output is correct |
11 |
Correct |
350 ms |
177624 KB |
Output is correct |
12 |
Correct |
67 ms |
176304 KB |
Output is correct |
13 |
Correct |
68 ms |
176308 KB |
Output is correct |
14 |
Correct |
67 ms |
176308 KB |
Output is correct |
15 |
Correct |
70 ms |
176396 KB |
Output is correct |
16 |
Correct |
69 ms |
176392 KB |
Output is correct |
17 |
Correct |
93 ms |
176400 KB |
Output is correct |
18 |
Correct |
92 ms |
176488 KB |
Output is correct |
19 |
Correct |
331 ms |
177304 KB |
Output is correct |
20 |
Correct |
372 ms |
178740 KB |
Output is correct |
21 |
Correct |
356 ms |
178176 KB |
Output is correct |
22 |
Correct |
338 ms |
177724 KB |
Output is correct |
23 |
Correct |
311 ms |
178832 KB |
Output is correct |
24 |
Correct |
238 ms |
177764 KB |
Output is correct |
25 |
Correct |
231 ms |
177588 KB |
Output is correct |
26 |
Correct |
233 ms |
177628 KB |
Output is correct |
27 |
Correct |
67 ms |
176344 KB |
Output is correct |
28 |
Correct |
68 ms |
176384 KB |
Output is correct |
29 |
Correct |
68 ms |
176352 KB |
Output is correct |
30 |
Correct |
72 ms |
176332 KB |
Output is correct |
31 |
Correct |
69 ms |
176424 KB |
Output is correct |
32 |
Correct |
71 ms |
176328 KB |
Output is correct |
33 |
Correct |
74 ms |
176384 KB |
Output is correct |
34 |
Correct |
72 ms |
176436 KB |
Output is correct |
35 |
Correct |
75 ms |
176588 KB |
Output is correct |
36 |
Correct |
305 ms |
177224 KB |
Output is correct |
37 |
Correct |
322 ms |
177852 KB |
Output is correct |
38 |
Correct |
358 ms |
178636 KB |
Output is correct |
39 |
Correct |
358 ms |
178764 KB |
Output is correct |
40 |
Correct |
334 ms |
178144 KB |
Output is correct |
41 |
Correct |
229 ms |
177604 KB |
Output is correct |
42 |
Correct |
228 ms |
177548 KB |
Output is correct |
43 |
Correct |
340 ms |
177920 KB |
Output is correct |
44 |
Correct |
68 ms |
176392 KB |
Output is correct |
45 |
Correct |
68 ms |
176316 KB |
Output is correct |
46 |
Correct |
68 ms |
176388 KB |
Output is correct |
47 |
Correct |
68 ms |
176360 KB |
Output is correct |
48 |
Correct |
67 ms |
176376 KB |
Output is correct |
49 |
Correct |
82 ms |
176436 KB |
Output is correct |
50 |
Correct |
80 ms |
176392 KB |
Output is correct |
51 |
Correct |
78 ms |
176464 KB |
Output is correct |
52 |
Correct |
79 ms |
176496 KB |
Output is correct |
53 |
Correct |
312 ms |
177172 KB |
Output is correct |
54 |
Correct |
355 ms |
178628 KB |
Output is correct |
55 |
Correct |
370 ms |
178888 KB |
Output is correct |
56 |
Correct |
343 ms |
178260 KB |
Output is correct |
57 |
Correct |
431 ms |
179780 KB |
Output is correct |
58 |
Correct |
339 ms |
177352 KB |
Output is correct |
59 |
Correct |
367 ms |
178144 KB |
Output is correct |