#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 z = 0; z < 5; z++) {
res.a[i][j] = min(res.a[i][j], a.a[i][z] + b.a[z][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 |
317 ms |
177428 KB |
Output is correct |
2 |
Correct |
70 ms |
176356 KB |
Output is correct |
3 |
Correct |
75 ms |
176304 KB |
Output is correct |
4 |
Correct |
77 ms |
176312 KB |
Output is correct |
5 |
Correct |
75 ms |
176336 KB |
Output is correct |
6 |
Correct |
72 ms |
176432 KB |
Output is correct |
7 |
Correct |
73 ms |
176364 KB |
Output is correct |
8 |
Correct |
319 ms |
177336 KB |
Output is correct |
9 |
Correct |
314 ms |
177284 KB |
Output is correct |
10 |
Correct |
295 ms |
176468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
343 ms |
177968 KB |
Output is correct |
2 |
Correct |
75 ms |
176332 KB |
Output is correct |
3 |
Correct |
74 ms |
176332 KB |
Output is correct |
4 |
Correct |
68 ms |
176332 KB |
Output is correct |
5 |
Correct |
71 ms |
176420 KB |
Output is correct |
6 |
Correct |
68 ms |
176352 KB |
Output is correct |
7 |
Correct |
100 ms |
176476 KB |
Output is correct |
8 |
Correct |
97 ms |
176652 KB |
Output is correct |
9 |
Correct |
354 ms |
177320 KB |
Output is correct |
10 |
Correct |
377 ms |
178788 KB |
Output is correct |
11 |
Correct |
345 ms |
178236 KB |
Output is correct |
12 |
Correct |
338 ms |
177688 KB |
Output is correct |
13 |
Correct |
281 ms |
178812 KB |
Output is correct |
14 |
Correct |
247 ms |
177840 KB |
Output is correct |
15 |
Correct |
229 ms |
177748 KB |
Output is correct |
16 |
Correct |
239 ms |
177652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
176408 KB |
Output is correct |
2 |
Correct |
70 ms |
176424 KB |
Output is correct |
3 |
Correct |
71 ms |
176396 KB |
Output is correct |
4 |
Correct |
69 ms |
176428 KB |
Output is correct |
5 |
Correct |
71 ms |
176460 KB |
Output is correct |
6 |
Correct |
76 ms |
176432 KB |
Output is correct |
7 |
Correct |
72 ms |
176332 KB |
Output is correct |
8 |
Correct |
73 ms |
176440 KB |
Output is correct |
9 |
Correct |
73 ms |
176440 KB |
Output is correct |
10 |
Correct |
285 ms |
177224 KB |
Output is correct |
11 |
Correct |
315 ms |
177868 KB |
Output is correct |
12 |
Correct |
345 ms |
178592 KB |
Output is correct |
13 |
Correct |
363 ms |
178864 KB |
Output is correct |
14 |
Correct |
331 ms |
178304 KB |
Output is correct |
15 |
Correct |
224 ms |
177684 KB |
Output is correct |
16 |
Correct |
212 ms |
177600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
176408 KB |
Output is correct |
2 |
Correct |
70 ms |
176424 KB |
Output is correct |
3 |
Correct |
71 ms |
176396 KB |
Output is correct |
4 |
Correct |
69 ms |
176428 KB |
Output is correct |
5 |
Correct |
71 ms |
176460 KB |
Output is correct |
6 |
Correct |
76 ms |
176432 KB |
Output is correct |
7 |
Correct |
72 ms |
176332 KB |
Output is correct |
8 |
Correct |
73 ms |
176440 KB |
Output is correct |
9 |
Correct |
73 ms |
176440 KB |
Output is correct |
10 |
Correct |
285 ms |
177224 KB |
Output is correct |
11 |
Correct |
315 ms |
177868 KB |
Output is correct |
12 |
Correct |
345 ms |
178592 KB |
Output is correct |
13 |
Correct |
363 ms |
178864 KB |
Output is correct |
14 |
Correct |
331 ms |
178304 KB |
Output is correct |
15 |
Correct |
224 ms |
177684 KB |
Output is correct |
16 |
Correct |
212 ms |
177600 KB |
Output is correct |
17 |
Correct |
344 ms |
177984 KB |
Output is correct |
18 |
Correct |
78 ms |
176408 KB |
Output is correct |
19 |
Correct |
67 ms |
176332 KB |
Output is correct |
20 |
Correct |
68 ms |
176348 KB |
Output is correct |
21 |
Correct |
70 ms |
176316 KB |
Output is correct |
22 |
Correct |
67 ms |
176376 KB |
Output is correct |
23 |
Correct |
81 ms |
176356 KB |
Output is correct |
24 |
Correct |
81 ms |
176448 KB |
Output is correct |
25 |
Correct |
79 ms |
176460 KB |
Output is correct |
26 |
Correct |
79 ms |
176412 KB |
Output is correct |
27 |
Correct |
303 ms |
177308 KB |
Output is correct |
28 |
Correct |
377 ms |
178716 KB |
Output is correct |
29 |
Correct |
381 ms |
178892 KB |
Output is correct |
30 |
Correct |
347 ms |
178252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
317 ms |
177428 KB |
Output is correct |
2 |
Correct |
70 ms |
176356 KB |
Output is correct |
3 |
Correct |
75 ms |
176304 KB |
Output is correct |
4 |
Correct |
77 ms |
176312 KB |
Output is correct |
5 |
Correct |
75 ms |
176336 KB |
Output is correct |
6 |
Correct |
72 ms |
176432 KB |
Output is correct |
7 |
Correct |
73 ms |
176364 KB |
Output is correct |
8 |
Correct |
319 ms |
177336 KB |
Output is correct |
9 |
Correct |
314 ms |
177284 KB |
Output is correct |
10 |
Correct |
295 ms |
176468 KB |
Output is correct |
11 |
Correct |
343 ms |
177968 KB |
Output is correct |
12 |
Correct |
75 ms |
176332 KB |
Output is correct |
13 |
Correct |
74 ms |
176332 KB |
Output is correct |
14 |
Correct |
68 ms |
176332 KB |
Output is correct |
15 |
Correct |
71 ms |
176420 KB |
Output is correct |
16 |
Correct |
68 ms |
176352 KB |
Output is correct |
17 |
Correct |
100 ms |
176476 KB |
Output is correct |
18 |
Correct |
97 ms |
176652 KB |
Output is correct |
19 |
Correct |
354 ms |
177320 KB |
Output is correct |
20 |
Correct |
377 ms |
178788 KB |
Output is correct |
21 |
Correct |
345 ms |
178236 KB |
Output is correct |
22 |
Correct |
338 ms |
177688 KB |
Output is correct |
23 |
Correct |
281 ms |
178812 KB |
Output is correct |
24 |
Correct |
247 ms |
177840 KB |
Output is correct |
25 |
Correct |
229 ms |
177748 KB |
Output is correct |
26 |
Correct |
239 ms |
177652 KB |
Output is correct |
27 |
Correct |
79 ms |
176408 KB |
Output is correct |
28 |
Correct |
70 ms |
176424 KB |
Output is correct |
29 |
Correct |
71 ms |
176396 KB |
Output is correct |
30 |
Correct |
69 ms |
176428 KB |
Output is correct |
31 |
Correct |
71 ms |
176460 KB |
Output is correct |
32 |
Correct |
76 ms |
176432 KB |
Output is correct |
33 |
Correct |
72 ms |
176332 KB |
Output is correct |
34 |
Correct |
73 ms |
176440 KB |
Output is correct |
35 |
Correct |
73 ms |
176440 KB |
Output is correct |
36 |
Correct |
285 ms |
177224 KB |
Output is correct |
37 |
Correct |
315 ms |
177868 KB |
Output is correct |
38 |
Correct |
345 ms |
178592 KB |
Output is correct |
39 |
Correct |
363 ms |
178864 KB |
Output is correct |
40 |
Correct |
331 ms |
178304 KB |
Output is correct |
41 |
Correct |
224 ms |
177684 KB |
Output is correct |
42 |
Correct |
212 ms |
177600 KB |
Output is correct |
43 |
Correct |
344 ms |
177984 KB |
Output is correct |
44 |
Correct |
78 ms |
176408 KB |
Output is correct |
45 |
Correct |
67 ms |
176332 KB |
Output is correct |
46 |
Correct |
68 ms |
176348 KB |
Output is correct |
47 |
Correct |
70 ms |
176316 KB |
Output is correct |
48 |
Correct |
67 ms |
176376 KB |
Output is correct |
49 |
Correct |
81 ms |
176356 KB |
Output is correct |
50 |
Correct |
81 ms |
176448 KB |
Output is correct |
51 |
Correct |
79 ms |
176460 KB |
Output is correct |
52 |
Correct |
79 ms |
176412 KB |
Output is correct |
53 |
Correct |
303 ms |
177308 KB |
Output is correct |
54 |
Correct |
377 ms |
178716 KB |
Output is correct |
55 |
Correct |
381 ms |
178892 KB |
Output is correct |
56 |
Correct |
347 ms |
178252 KB |
Output is correct |
57 |
Correct |
445 ms |
179712 KB |
Output is correct |
58 |
Correct |
360 ms |
177332 KB |
Output is correct |
59 |
Correct |
359 ms |
178248 KB |
Output is correct |