#include <bits/stdc++.h>
#define X first
#define Y second
#define MP make_pair
#define ll long long
using namespace std;
const int N = 1e6 + 1, M = 1e7 + 1, mod = 1e9 + 7;
const ll INF = 1e18;
ll sp[50001][17][5][5], ans[5][5], tmp[5][5];
int k, n, m, q;
void comb(ll (&t)[5][5], const ll (&a)[5][5], const ll (&b)[5][5]){
for(int x = 0;x < k;x++){
for(int y = 0;y < k;y++){
t[x][y] = INF;
for(int z = 0;z < k;z++){
t[x][y] = min(t[x][y], a[x][z] + b[z][y]);
}
}
}
}
int main () {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> k >> n >> m >> q;
memset(sp, 0x3f, sizeof(sp));
for(int i = 0, x, y, c;i < m;i++){
cin >> x >> y >> c;
//cerr << x / k << " " << y / k << "\n";
sp[x / k][0][x % k][y % k] = c;
}
for(int i = 1;i < 17;i++){
for(int j = 0;j + (1 << i) < (n + k - 1) / k;j++){
comb(sp[j][i], sp[j][i - 1], sp[j + (1 << (i - 1))][i - 1]);
}
}
for(int l, r;q--;){
cin >> l >> r;
memset(ans, 0x3f, sizeof(ans));
for(int i = 0;i < k;i++) ans[i][i] = 0;
for(int st = l / k, fn = r / k, i = 16;i >= 0;i--){
if(st + (1 << i) <= fn){
//cerr << sp[st][0][l % k][r % k] << "\n";
comb(tmp, ans, sp[st][i]);
memcpy(ans, tmp, sizeof(ans));
st += (1 << i);
}
}
ll z = ans[l % k][r % k];
if(z >= INF)
z = -1;
cout << z << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
167608 KB |
Output is correct |
2 |
Correct |
70 ms |
166576 KB |
Output is correct |
3 |
Correct |
67 ms |
166596 KB |
Output is correct |
4 |
Correct |
73 ms |
166592 KB |
Output is correct |
5 |
Correct |
69 ms |
166592 KB |
Output is correct |
6 |
Correct |
69 ms |
166600 KB |
Output is correct |
7 |
Correct |
77 ms |
166596 KB |
Output is correct |
8 |
Correct |
131 ms |
167492 KB |
Output is correct |
9 |
Correct |
116 ms |
167664 KB |
Output is correct |
10 |
Correct |
91 ms |
166696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
168292 KB |
Output is correct |
2 |
Correct |
76 ms |
166600 KB |
Output is correct |
3 |
Correct |
73 ms |
166644 KB |
Output is correct |
4 |
Correct |
67 ms |
166600 KB |
Output is correct |
5 |
Correct |
68 ms |
166632 KB |
Output is correct |
6 |
Correct |
67 ms |
166640 KB |
Output is correct |
7 |
Correct |
80 ms |
166740 KB |
Output is correct |
8 |
Correct |
73 ms |
166784 KB |
Output is correct |
9 |
Correct |
111 ms |
167460 KB |
Output is correct |
10 |
Correct |
155 ms |
168900 KB |
Output is correct |
11 |
Correct |
152 ms |
168328 KB |
Output is correct |
12 |
Correct |
134 ms |
167876 KB |
Output is correct |
13 |
Correct |
137 ms |
169032 KB |
Output is correct |
14 |
Correct |
113 ms |
168004 KB |
Output is correct |
15 |
Correct |
115 ms |
167760 KB |
Output is correct |
16 |
Correct |
113 ms |
167804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
166644 KB |
Output is correct |
2 |
Correct |
69 ms |
166556 KB |
Output is correct |
3 |
Correct |
70 ms |
166620 KB |
Output is correct |
4 |
Correct |
69 ms |
166596 KB |
Output is correct |
5 |
Correct |
69 ms |
166560 KB |
Output is correct |
6 |
Correct |
70 ms |
166552 KB |
Output is correct |
7 |
Correct |
77 ms |
166788 KB |
Output is correct |
8 |
Correct |
71 ms |
166596 KB |
Output is correct |
9 |
Correct |
70 ms |
166620 KB |
Output is correct |
10 |
Correct |
106 ms |
167332 KB |
Output is correct |
11 |
Correct |
146 ms |
168132 KB |
Output is correct |
12 |
Correct |
149 ms |
168880 KB |
Output is correct |
13 |
Correct |
149 ms |
169028 KB |
Output is correct |
14 |
Correct |
137 ms |
168452 KB |
Output is correct |
15 |
Correct |
108 ms |
167824 KB |
Output is correct |
16 |
Correct |
112 ms |
167748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
166644 KB |
Output is correct |
2 |
Correct |
69 ms |
166556 KB |
Output is correct |
3 |
Correct |
70 ms |
166620 KB |
Output is correct |
4 |
Correct |
69 ms |
166596 KB |
Output is correct |
5 |
Correct |
69 ms |
166560 KB |
Output is correct |
6 |
Correct |
70 ms |
166552 KB |
Output is correct |
7 |
Correct |
77 ms |
166788 KB |
Output is correct |
8 |
Correct |
71 ms |
166596 KB |
Output is correct |
9 |
Correct |
70 ms |
166620 KB |
Output is correct |
10 |
Correct |
106 ms |
167332 KB |
Output is correct |
11 |
Correct |
146 ms |
168132 KB |
Output is correct |
12 |
Correct |
149 ms |
168880 KB |
Output is correct |
13 |
Correct |
149 ms |
169028 KB |
Output is correct |
14 |
Correct |
137 ms |
168452 KB |
Output is correct |
15 |
Correct |
108 ms |
167824 KB |
Output is correct |
16 |
Correct |
112 ms |
167748 KB |
Output is correct |
17 |
Correct |
146 ms |
168156 KB |
Output is correct |
18 |
Correct |
71 ms |
166532 KB |
Output is correct |
19 |
Correct |
70 ms |
166544 KB |
Output is correct |
20 |
Correct |
70 ms |
166596 KB |
Output is correct |
21 |
Correct |
69 ms |
166644 KB |
Output is correct |
22 |
Correct |
70 ms |
166616 KB |
Output is correct |
23 |
Correct |
71 ms |
166660 KB |
Output is correct |
24 |
Correct |
71 ms |
166660 KB |
Output is correct |
25 |
Correct |
73 ms |
166724 KB |
Output is correct |
26 |
Correct |
72 ms |
166672 KB |
Output is correct |
27 |
Correct |
109 ms |
167468 KB |
Output is correct |
28 |
Correct |
150 ms |
168772 KB |
Output is correct |
29 |
Correct |
156 ms |
169080 KB |
Output is correct |
30 |
Correct |
146 ms |
168600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
167608 KB |
Output is correct |
2 |
Correct |
70 ms |
166576 KB |
Output is correct |
3 |
Correct |
67 ms |
166596 KB |
Output is correct |
4 |
Correct |
73 ms |
166592 KB |
Output is correct |
5 |
Correct |
69 ms |
166592 KB |
Output is correct |
6 |
Correct |
69 ms |
166600 KB |
Output is correct |
7 |
Correct |
77 ms |
166596 KB |
Output is correct |
8 |
Correct |
131 ms |
167492 KB |
Output is correct |
9 |
Correct |
116 ms |
167664 KB |
Output is correct |
10 |
Correct |
91 ms |
166696 KB |
Output is correct |
11 |
Correct |
166 ms |
168292 KB |
Output is correct |
12 |
Correct |
76 ms |
166600 KB |
Output is correct |
13 |
Correct |
73 ms |
166644 KB |
Output is correct |
14 |
Correct |
67 ms |
166600 KB |
Output is correct |
15 |
Correct |
68 ms |
166632 KB |
Output is correct |
16 |
Correct |
67 ms |
166640 KB |
Output is correct |
17 |
Correct |
80 ms |
166740 KB |
Output is correct |
18 |
Correct |
73 ms |
166784 KB |
Output is correct |
19 |
Correct |
111 ms |
167460 KB |
Output is correct |
20 |
Correct |
155 ms |
168900 KB |
Output is correct |
21 |
Correct |
152 ms |
168328 KB |
Output is correct |
22 |
Correct |
134 ms |
167876 KB |
Output is correct |
23 |
Correct |
137 ms |
169032 KB |
Output is correct |
24 |
Correct |
113 ms |
168004 KB |
Output is correct |
25 |
Correct |
115 ms |
167760 KB |
Output is correct |
26 |
Correct |
113 ms |
167804 KB |
Output is correct |
27 |
Correct |
70 ms |
166644 KB |
Output is correct |
28 |
Correct |
69 ms |
166556 KB |
Output is correct |
29 |
Correct |
70 ms |
166620 KB |
Output is correct |
30 |
Correct |
69 ms |
166596 KB |
Output is correct |
31 |
Correct |
69 ms |
166560 KB |
Output is correct |
32 |
Correct |
70 ms |
166552 KB |
Output is correct |
33 |
Correct |
77 ms |
166788 KB |
Output is correct |
34 |
Correct |
71 ms |
166596 KB |
Output is correct |
35 |
Correct |
70 ms |
166620 KB |
Output is correct |
36 |
Correct |
106 ms |
167332 KB |
Output is correct |
37 |
Correct |
146 ms |
168132 KB |
Output is correct |
38 |
Correct |
149 ms |
168880 KB |
Output is correct |
39 |
Correct |
149 ms |
169028 KB |
Output is correct |
40 |
Correct |
137 ms |
168452 KB |
Output is correct |
41 |
Correct |
108 ms |
167824 KB |
Output is correct |
42 |
Correct |
112 ms |
167748 KB |
Output is correct |
43 |
Correct |
146 ms |
168156 KB |
Output is correct |
44 |
Correct |
71 ms |
166532 KB |
Output is correct |
45 |
Correct |
70 ms |
166544 KB |
Output is correct |
46 |
Correct |
70 ms |
166596 KB |
Output is correct |
47 |
Correct |
69 ms |
166644 KB |
Output is correct |
48 |
Correct |
70 ms |
166616 KB |
Output is correct |
49 |
Correct |
71 ms |
166660 KB |
Output is correct |
50 |
Correct |
71 ms |
166660 KB |
Output is correct |
51 |
Correct |
73 ms |
166724 KB |
Output is correct |
52 |
Correct |
72 ms |
166672 KB |
Output is correct |
53 |
Correct |
109 ms |
167468 KB |
Output is correct |
54 |
Correct |
150 ms |
168772 KB |
Output is correct |
55 |
Correct |
156 ms |
169080 KB |
Output is correct |
56 |
Correct |
146 ms |
168600 KB |
Output is correct |
57 |
Correct |
180 ms |
170012 KB |
Output is correct |
58 |
Correct |
116 ms |
167516 KB |
Output is correct |
59 |
Correct |
163 ms |
168472 KB |
Output is correct |