#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, o;
cin >> k >> n >> m >> o;
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 < o; 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 |
329 ms |
176716 KB |
Output is correct |
2 |
Correct |
73 ms |
176304 KB |
Output is correct |
3 |
Correct |
75 ms |
176428 KB |
Output is correct |
4 |
Correct |
76 ms |
176364 KB |
Output is correct |
5 |
Correct |
80 ms |
176476 KB |
Output is correct |
6 |
Correct |
74 ms |
176436 KB |
Output is correct |
7 |
Correct |
72 ms |
176424 KB |
Output is correct |
8 |
Correct |
347 ms |
176672 KB |
Output is correct |
9 |
Correct |
345 ms |
176664 KB |
Output is correct |
10 |
Correct |
275 ms |
176464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
345 ms |
176796 KB |
Output is correct |
2 |
Correct |
69 ms |
176392 KB |
Output is correct |
3 |
Correct |
76 ms |
176324 KB |
Output is correct |
4 |
Correct |
74 ms |
176336 KB |
Output is correct |
5 |
Correct |
67 ms |
176340 KB |
Output is correct |
6 |
Correct |
68 ms |
176384 KB |
Output is correct |
7 |
Correct |
108 ms |
176568 KB |
Output is correct |
8 |
Correct |
100 ms |
176476 KB |
Output is correct |
9 |
Correct |
308 ms |
176692 KB |
Output is correct |
10 |
Correct |
406 ms |
176848 KB |
Output is correct |
11 |
Correct |
358 ms |
176680 KB |
Output is correct |
12 |
Correct |
327 ms |
176632 KB |
Output is correct |
13 |
Correct |
290 ms |
176684 KB |
Output is correct |
14 |
Correct |
243 ms |
176676 KB |
Output is correct |
15 |
Correct |
229 ms |
176584 KB |
Output is correct |
16 |
Correct |
229 ms |
176620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
176364 KB |
Output is correct |
2 |
Correct |
68 ms |
176348 KB |
Output is correct |
3 |
Correct |
73 ms |
176416 KB |
Output is correct |
4 |
Correct |
70 ms |
176416 KB |
Output is correct |
5 |
Correct |
80 ms |
176372 KB |
Output is correct |
6 |
Correct |
71 ms |
176400 KB |
Output is correct |
7 |
Correct |
71 ms |
176332 KB |
Output is correct |
8 |
Correct |
84 ms |
176460 KB |
Output is correct |
9 |
Correct |
72 ms |
176436 KB |
Output is correct |
10 |
Correct |
280 ms |
176676 KB |
Output is correct |
11 |
Correct |
332 ms |
176680 KB |
Output is correct |
12 |
Correct |
364 ms |
176680 KB |
Output is correct |
13 |
Correct |
392 ms |
176676 KB |
Output is correct |
14 |
Correct |
370 ms |
176668 KB |
Output is correct |
15 |
Correct |
238 ms |
176648 KB |
Output is correct |
16 |
Correct |
236 ms |
176640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
176364 KB |
Output is correct |
2 |
Correct |
68 ms |
176348 KB |
Output is correct |
3 |
Correct |
73 ms |
176416 KB |
Output is correct |
4 |
Correct |
70 ms |
176416 KB |
Output is correct |
5 |
Correct |
80 ms |
176372 KB |
Output is correct |
6 |
Correct |
71 ms |
176400 KB |
Output is correct |
7 |
Correct |
71 ms |
176332 KB |
Output is correct |
8 |
Correct |
84 ms |
176460 KB |
Output is correct |
9 |
Correct |
72 ms |
176436 KB |
Output is correct |
10 |
Correct |
280 ms |
176676 KB |
Output is correct |
11 |
Correct |
332 ms |
176680 KB |
Output is correct |
12 |
Correct |
364 ms |
176680 KB |
Output is correct |
13 |
Correct |
392 ms |
176676 KB |
Output is correct |
14 |
Correct |
370 ms |
176668 KB |
Output is correct |
15 |
Correct |
238 ms |
176648 KB |
Output is correct |
16 |
Correct |
236 ms |
176640 KB |
Output is correct |
17 |
Correct |
365 ms |
176936 KB |
Output is correct |
18 |
Correct |
72 ms |
176372 KB |
Output is correct |
19 |
Correct |
91 ms |
176384 KB |
Output is correct |
20 |
Correct |
83 ms |
176372 KB |
Output is correct |
21 |
Correct |
89 ms |
176332 KB |
Output is correct |
22 |
Correct |
73 ms |
176376 KB |
Output is correct |
23 |
Correct |
84 ms |
176352 KB |
Output is correct |
24 |
Correct |
80 ms |
176492 KB |
Output is correct |
25 |
Correct |
82 ms |
176516 KB |
Output is correct |
26 |
Correct |
81 ms |
176396 KB |
Output is correct |
27 |
Correct |
295 ms |
177260 KB |
Output is correct |
28 |
Correct |
408 ms |
177328 KB |
Output is correct |
29 |
Correct |
405 ms |
177312 KB |
Output is correct |
30 |
Correct |
361 ms |
177424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
176716 KB |
Output is correct |
2 |
Correct |
73 ms |
176304 KB |
Output is correct |
3 |
Correct |
75 ms |
176428 KB |
Output is correct |
4 |
Correct |
76 ms |
176364 KB |
Output is correct |
5 |
Correct |
80 ms |
176476 KB |
Output is correct |
6 |
Correct |
74 ms |
176436 KB |
Output is correct |
7 |
Correct |
72 ms |
176424 KB |
Output is correct |
8 |
Correct |
347 ms |
176672 KB |
Output is correct |
9 |
Correct |
345 ms |
176664 KB |
Output is correct |
10 |
Correct |
275 ms |
176464 KB |
Output is correct |
11 |
Correct |
345 ms |
176796 KB |
Output is correct |
12 |
Correct |
69 ms |
176392 KB |
Output is correct |
13 |
Correct |
76 ms |
176324 KB |
Output is correct |
14 |
Correct |
74 ms |
176336 KB |
Output is correct |
15 |
Correct |
67 ms |
176340 KB |
Output is correct |
16 |
Correct |
68 ms |
176384 KB |
Output is correct |
17 |
Correct |
108 ms |
176568 KB |
Output is correct |
18 |
Correct |
100 ms |
176476 KB |
Output is correct |
19 |
Correct |
308 ms |
176692 KB |
Output is correct |
20 |
Correct |
406 ms |
176848 KB |
Output is correct |
21 |
Correct |
358 ms |
176680 KB |
Output is correct |
22 |
Correct |
327 ms |
176632 KB |
Output is correct |
23 |
Correct |
290 ms |
176684 KB |
Output is correct |
24 |
Correct |
243 ms |
176676 KB |
Output is correct |
25 |
Correct |
229 ms |
176584 KB |
Output is correct |
26 |
Correct |
229 ms |
176620 KB |
Output is correct |
27 |
Correct |
67 ms |
176364 KB |
Output is correct |
28 |
Correct |
68 ms |
176348 KB |
Output is correct |
29 |
Correct |
73 ms |
176416 KB |
Output is correct |
30 |
Correct |
70 ms |
176416 KB |
Output is correct |
31 |
Correct |
80 ms |
176372 KB |
Output is correct |
32 |
Correct |
71 ms |
176400 KB |
Output is correct |
33 |
Correct |
71 ms |
176332 KB |
Output is correct |
34 |
Correct |
84 ms |
176460 KB |
Output is correct |
35 |
Correct |
72 ms |
176436 KB |
Output is correct |
36 |
Correct |
280 ms |
176676 KB |
Output is correct |
37 |
Correct |
332 ms |
176680 KB |
Output is correct |
38 |
Correct |
364 ms |
176680 KB |
Output is correct |
39 |
Correct |
392 ms |
176676 KB |
Output is correct |
40 |
Correct |
370 ms |
176668 KB |
Output is correct |
41 |
Correct |
238 ms |
176648 KB |
Output is correct |
42 |
Correct |
236 ms |
176640 KB |
Output is correct |
43 |
Correct |
365 ms |
176936 KB |
Output is correct |
44 |
Correct |
72 ms |
176372 KB |
Output is correct |
45 |
Correct |
91 ms |
176384 KB |
Output is correct |
46 |
Correct |
83 ms |
176372 KB |
Output is correct |
47 |
Correct |
89 ms |
176332 KB |
Output is correct |
48 |
Correct |
73 ms |
176376 KB |
Output is correct |
49 |
Correct |
84 ms |
176352 KB |
Output is correct |
50 |
Correct |
80 ms |
176492 KB |
Output is correct |
51 |
Correct |
82 ms |
176516 KB |
Output is correct |
52 |
Correct |
81 ms |
176396 KB |
Output is correct |
53 |
Correct |
295 ms |
177260 KB |
Output is correct |
54 |
Correct |
408 ms |
177328 KB |
Output is correct |
55 |
Correct |
405 ms |
177312 KB |
Output is correct |
56 |
Correct |
361 ms |
177424 KB |
Output is correct |
57 |
Correct |
470 ms |
177344 KB |
Output is correct |
58 |
Correct |
365 ms |
177320 KB |
Output is correct |
59 |
Correct |
360 ms |
177316 KB |
Output is correct |