#include <bits/stdc++.h>
using namespace std;
const int INF = 500000001;
const int MAX_N = 50000;
struct Matrix {
int matr[5][5];
Matrix() {
for(int i = 0; i < 5; ++i)
for(int j = 0; j < 5; ++j)
matr[i][j] = INF;
}
};
Matrix mergeMatrices(int k, Matrix a, Matrix b) {
Matrix rez;
for(int i = 0; i < k; ++i)
for(int j = 0; j < k; ++j)
for(int l = 0; l < k; ++l)
rez.matr[i][j] = min(rez.matr[i][j], a.matr[i][l] + b.matr[l][j]);
return rez;
}
Matrix aint[1+4*MAX_N];
Matrix tranz[MAX_N];
void init(int K, int nod = 1, int l = 0, int r = MAX_N - 1) {
if(l < r) {
int mid = (l + r) / 2;
init(K, 2 * nod, l, mid);
init(K, 2 * nod + 1, mid + 1, r);
aint[nod] = mergeMatrices(K, aint[2 * nod], aint[2 * nod + 1]);
} else {
aint[nod] = tranz[l];
}
}
Matrix query(int K, int i, int j, int l = 0, int r = MAX_N - 1, int nod = 1) {
if(i <= l && r <= j) {
return aint[nod];
} else {
int mid = (l + r) / 2;
Matrix rez;
if(i <= mid) {
rez = query(K, i, j, l, mid, 2 * nod);
if(j > mid)
rez = mergeMatrices(K, rez, query(K, i, j, mid + 1, r, 2 * nod + 1));
} else
rez = query(K, i, j, mid + 1, r, 2 * nod + 1);
return rez;
}
}
int main() {
int K, N, M, O;
scanf("%d%d%d%d", &K, &N, &M, &O);
for(int i = 0; i < M; ++i) {
int a, b, t;
scanf("%d%d%d", &a, &b, &t);
tranz[a / K].matr[a % K][b % K] = min(tranz[a / K].matr[a % K][b % K], t);
}
init(K);
for(int i = 0; i < O; ++i) {
int a, b;
scanf("%d%d", &a, &b);
if(a / K >= b / K)
printf("-1\n");
else {
Matrix rez = query(K, a / K, b / K - 1);
if(rez.matr[a % K][b % K] >= INF)
printf("-1\n");
else
printf("%d\n", rez.matr[a % K][b % K]);
}
}
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &K, &N, &M, &O);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &a, &b, &t);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
25848 KB |
Output is correct |
2 |
Correct |
26 ms |
24824 KB |
Output is correct |
3 |
Correct |
30 ms |
24824 KB |
Output is correct |
4 |
Correct |
30 ms |
24824 KB |
Output is correct |
5 |
Correct |
32 ms |
24824 KB |
Output is correct |
6 |
Correct |
28 ms |
24824 KB |
Output is correct |
7 |
Correct |
28 ms |
24824 KB |
Output is correct |
8 |
Correct |
65 ms |
25720 KB |
Output is correct |
9 |
Correct |
64 ms |
25720 KB |
Output is correct |
10 |
Correct |
44 ms |
24952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
26488 KB |
Output is correct |
2 |
Correct |
27 ms |
24708 KB |
Output is correct |
3 |
Correct |
27 ms |
24824 KB |
Output is correct |
4 |
Correct |
28 ms |
24956 KB |
Output is correct |
5 |
Correct |
30 ms |
24824 KB |
Output is correct |
6 |
Correct |
33 ms |
24828 KB |
Output is correct |
7 |
Correct |
33 ms |
24952 KB |
Output is correct |
8 |
Correct |
34 ms |
24952 KB |
Output is correct |
9 |
Correct |
60 ms |
25720 KB |
Output is correct |
10 |
Correct |
110 ms |
27128 KB |
Output is correct |
11 |
Correct |
84 ms |
26488 KB |
Output is correct |
12 |
Correct |
71 ms |
26360 KB |
Output is correct |
13 |
Correct |
99 ms |
27232 KB |
Output is correct |
14 |
Correct |
66 ms |
26232 KB |
Output is correct |
15 |
Correct |
68 ms |
25976 KB |
Output is correct |
16 |
Correct |
68 ms |
25976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24824 KB |
Output is correct |
2 |
Correct |
27 ms |
24860 KB |
Output is correct |
3 |
Correct |
28 ms |
24824 KB |
Output is correct |
4 |
Correct |
30 ms |
24924 KB |
Output is correct |
5 |
Correct |
34 ms |
24824 KB |
Output is correct |
6 |
Correct |
27 ms |
24796 KB |
Output is correct |
7 |
Correct |
28 ms |
24824 KB |
Output is correct |
8 |
Correct |
39 ms |
24824 KB |
Output is correct |
9 |
Correct |
34 ms |
24824 KB |
Output is correct |
10 |
Correct |
50 ms |
25592 KB |
Output is correct |
11 |
Correct |
69 ms |
26232 KB |
Output is correct |
12 |
Correct |
84 ms |
27000 KB |
Output is correct |
13 |
Correct |
94 ms |
27228 KB |
Output is correct |
14 |
Correct |
78 ms |
26564 KB |
Output is correct |
15 |
Correct |
64 ms |
25976 KB |
Output is correct |
16 |
Correct |
64 ms |
25976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
24824 KB |
Output is correct |
2 |
Correct |
27 ms |
24860 KB |
Output is correct |
3 |
Correct |
28 ms |
24824 KB |
Output is correct |
4 |
Correct |
30 ms |
24924 KB |
Output is correct |
5 |
Correct |
34 ms |
24824 KB |
Output is correct |
6 |
Correct |
27 ms |
24796 KB |
Output is correct |
7 |
Correct |
28 ms |
24824 KB |
Output is correct |
8 |
Correct |
39 ms |
24824 KB |
Output is correct |
9 |
Correct |
34 ms |
24824 KB |
Output is correct |
10 |
Correct |
50 ms |
25592 KB |
Output is correct |
11 |
Correct |
69 ms |
26232 KB |
Output is correct |
12 |
Correct |
84 ms |
27000 KB |
Output is correct |
13 |
Correct |
94 ms |
27228 KB |
Output is correct |
14 |
Correct |
78 ms |
26564 KB |
Output is correct |
15 |
Correct |
64 ms |
25976 KB |
Output is correct |
16 |
Correct |
64 ms |
25976 KB |
Output is correct |
17 |
Correct |
88 ms |
26360 KB |
Output is correct |
18 |
Correct |
31 ms |
24828 KB |
Output is correct |
19 |
Correct |
31 ms |
24764 KB |
Output is correct |
20 |
Correct |
30 ms |
24824 KB |
Output is correct |
21 |
Correct |
33 ms |
24952 KB |
Output is correct |
22 |
Correct |
33 ms |
24784 KB |
Output is correct |
23 |
Correct |
44 ms |
24824 KB |
Output is correct |
24 |
Correct |
35 ms |
24824 KB |
Output is correct |
25 |
Correct |
39 ms |
24824 KB |
Output is correct |
26 |
Correct |
35 ms |
24824 KB |
Output is correct |
27 |
Correct |
63 ms |
25656 KB |
Output is correct |
28 |
Correct |
103 ms |
27000 KB |
Output is correct |
29 |
Correct |
96 ms |
27256 KB |
Output is correct |
30 |
Correct |
80 ms |
26616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
25848 KB |
Output is correct |
2 |
Correct |
26 ms |
24824 KB |
Output is correct |
3 |
Correct |
30 ms |
24824 KB |
Output is correct |
4 |
Correct |
30 ms |
24824 KB |
Output is correct |
5 |
Correct |
32 ms |
24824 KB |
Output is correct |
6 |
Correct |
28 ms |
24824 KB |
Output is correct |
7 |
Correct |
28 ms |
24824 KB |
Output is correct |
8 |
Correct |
65 ms |
25720 KB |
Output is correct |
9 |
Correct |
64 ms |
25720 KB |
Output is correct |
10 |
Correct |
44 ms |
24952 KB |
Output is correct |
11 |
Correct |
78 ms |
26488 KB |
Output is correct |
12 |
Correct |
27 ms |
24708 KB |
Output is correct |
13 |
Correct |
27 ms |
24824 KB |
Output is correct |
14 |
Correct |
28 ms |
24956 KB |
Output is correct |
15 |
Correct |
30 ms |
24824 KB |
Output is correct |
16 |
Correct |
33 ms |
24828 KB |
Output is correct |
17 |
Correct |
33 ms |
24952 KB |
Output is correct |
18 |
Correct |
34 ms |
24952 KB |
Output is correct |
19 |
Correct |
60 ms |
25720 KB |
Output is correct |
20 |
Correct |
110 ms |
27128 KB |
Output is correct |
21 |
Correct |
84 ms |
26488 KB |
Output is correct |
22 |
Correct |
71 ms |
26360 KB |
Output is correct |
23 |
Correct |
99 ms |
27232 KB |
Output is correct |
24 |
Correct |
66 ms |
26232 KB |
Output is correct |
25 |
Correct |
68 ms |
25976 KB |
Output is correct |
26 |
Correct |
68 ms |
25976 KB |
Output is correct |
27 |
Correct |
32 ms |
24824 KB |
Output is correct |
28 |
Correct |
27 ms |
24860 KB |
Output is correct |
29 |
Correct |
28 ms |
24824 KB |
Output is correct |
30 |
Correct |
30 ms |
24924 KB |
Output is correct |
31 |
Correct |
34 ms |
24824 KB |
Output is correct |
32 |
Correct |
27 ms |
24796 KB |
Output is correct |
33 |
Correct |
28 ms |
24824 KB |
Output is correct |
34 |
Correct |
39 ms |
24824 KB |
Output is correct |
35 |
Correct |
34 ms |
24824 KB |
Output is correct |
36 |
Correct |
50 ms |
25592 KB |
Output is correct |
37 |
Correct |
69 ms |
26232 KB |
Output is correct |
38 |
Correct |
84 ms |
27000 KB |
Output is correct |
39 |
Correct |
94 ms |
27228 KB |
Output is correct |
40 |
Correct |
78 ms |
26564 KB |
Output is correct |
41 |
Correct |
64 ms |
25976 KB |
Output is correct |
42 |
Correct |
64 ms |
25976 KB |
Output is correct |
43 |
Correct |
88 ms |
26360 KB |
Output is correct |
44 |
Correct |
31 ms |
24828 KB |
Output is correct |
45 |
Correct |
31 ms |
24764 KB |
Output is correct |
46 |
Correct |
30 ms |
24824 KB |
Output is correct |
47 |
Correct |
33 ms |
24952 KB |
Output is correct |
48 |
Correct |
33 ms |
24784 KB |
Output is correct |
49 |
Correct |
44 ms |
24824 KB |
Output is correct |
50 |
Correct |
35 ms |
24824 KB |
Output is correct |
51 |
Correct |
39 ms |
24824 KB |
Output is correct |
52 |
Correct |
35 ms |
24824 KB |
Output is correct |
53 |
Correct |
63 ms |
25656 KB |
Output is correct |
54 |
Correct |
103 ms |
27000 KB |
Output is correct |
55 |
Correct |
96 ms |
27256 KB |
Output is correct |
56 |
Correct |
80 ms |
26616 KB |
Output is correct |
57 |
Correct |
134 ms |
28144 KB |
Output is correct |
58 |
Correct |
66 ms |
25720 KB |
Output is correct |
59 |
Correct |
85 ms |
26616 KB |
Output is correct |