#include <bits/stdc++.h>
#define mp make_pair
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<vector<ll> > vvi;
int k;
vvi combine(vvi beg, vvi endd) {
vvi ans(k, vector<ll>(k, 1e18));
for (int i = 0; i < k; i++)
for (int j = 0; j < k; j++)
for (int l = 0; l < k; l++)
ans[i][l] = min(ans[i][l], beg[i][j] + endd[j][l]);
return ans;
}
const int lg = 18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q;
cin >> k >> n >> m >> q;
vector<vector<vvi> > lift(lg, vector<vvi> (n / k + 1, vvi(k, vector<ll>(k, 1e18))));
for (int i = 0; i < m; i++) {
int f, t, p;
cin >>f >> t >> p;
lift[0][f / k][f % k][t % k] = p;
}
int num_lay = n / k + 1;
for (int bit = 1; bit < lg; bit++)
for (int i = num_lay - (1 << (bit - 1)) - 1; i >= 0; i--)
lift[bit][i] = combine(lift[bit - 1][i], lift[bit - 1][i + (1 << (bit - 1))]);
while (q--) {
int f, t;
cin >> f >> t;
int SF = f / k, ST = t / k;
vvi ans(k, vector<ll>(k, 1e18));
ans[f % k][f % k] = 0;
int diff = ST - SF;
for (int i = 0; i < lg; i++) {
if (((1 << i) & diff) != 0) {
ans = combine(ans, lift[i][SF]);
SF += (1 << i);
}
}
if (ans[f % k][t % k] == 1e18)
cout << "-1\n";
else
cout << ans[f % k][t % k] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
82152 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1972 KB |
Output is correct |
7 |
Correct |
4 ms |
1884 KB |
Output is correct |
8 |
Correct |
206 ms |
82228 KB |
Output is correct |
9 |
Correct |
200 ms |
82116 KB |
Output is correct |
10 |
Correct |
188 ms |
82004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
70996 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
11 ms |
2140 KB |
Output is correct |
8 |
Correct |
14 ms |
1992 KB |
Output is correct |
9 |
Correct |
181 ms |
82268 KB |
Output is correct |
10 |
Correct |
173 ms |
64536 KB |
Output is correct |
11 |
Correct |
176 ms |
72212 KB |
Output is correct |
12 |
Correct |
148 ms |
63440 KB |
Output is correct |
13 |
Correct |
119 ms |
46272 KB |
Output is correct |
14 |
Correct |
89 ms |
38864 KB |
Output is correct |
15 |
Correct |
94 ms |
45252 KB |
Output is correct |
16 |
Correct |
91 ms |
45252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
2108 KB |
Output is correct |
7 |
Correct |
3 ms |
1904 KB |
Output is correct |
8 |
Correct |
3 ms |
1884 KB |
Output is correct |
9 |
Correct |
3 ms |
2028 KB |
Output is correct |
10 |
Correct |
171 ms |
82016 KB |
Output is correct |
11 |
Correct |
143 ms |
71936 KB |
Output is correct |
12 |
Correct |
137 ms |
63936 KB |
Output is correct |
13 |
Correct |
146 ms |
64352 KB |
Output is correct |
14 |
Correct |
137 ms |
63688 KB |
Output is correct |
15 |
Correct |
88 ms |
45248 KB |
Output is correct |
16 |
Correct |
87 ms |
45224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
2108 KB |
Output is correct |
7 |
Correct |
3 ms |
1904 KB |
Output is correct |
8 |
Correct |
3 ms |
1884 KB |
Output is correct |
9 |
Correct |
3 ms |
2028 KB |
Output is correct |
10 |
Correct |
171 ms |
82016 KB |
Output is correct |
11 |
Correct |
143 ms |
71936 KB |
Output is correct |
12 |
Correct |
137 ms |
63936 KB |
Output is correct |
13 |
Correct |
146 ms |
64352 KB |
Output is correct |
14 |
Correct |
137 ms |
63688 KB |
Output is correct |
15 |
Correct |
88 ms |
45248 KB |
Output is correct |
16 |
Correct |
87 ms |
45224 KB |
Output is correct |
17 |
Correct |
148 ms |
71940 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
6 ms |
2136 KB |
Output is correct |
24 |
Correct |
7 ms |
1880 KB |
Output is correct |
25 |
Correct |
9 ms |
1884 KB |
Output is correct |
26 |
Correct |
7 ms |
1884 KB |
Output is correct |
27 |
Correct |
179 ms |
82092 KB |
Output is correct |
28 |
Correct |
148 ms |
63952 KB |
Output is correct |
29 |
Correct |
180 ms |
64204 KB |
Output is correct |
30 |
Correct |
143 ms |
63692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
82152 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
1884 KB |
Output is correct |
6 |
Correct |
4 ms |
1972 KB |
Output is correct |
7 |
Correct |
4 ms |
1884 KB |
Output is correct |
8 |
Correct |
206 ms |
82228 KB |
Output is correct |
9 |
Correct |
200 ms |
82116 KB |
Output is correct |
10 |
Correct |
188 ms |
82004 KB |
Output is correct |
11 |
Correct |
157 ms |
70996 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
11 ms |
2140 KB |
Output is correct |
18 |
Correct |
14 ms |
1992 KB |
Output is correct |
19 |
Correct |
181 ms |
82268 KB |
Output is correct |
20 |
Correct |
173 ms |
64536 KB |
Output is correct |
21 |
Correct |
176 ms |
72212 KB |
Output is correct |
22 |
Correct |
148 ms |
63440 KB |
Output is correct |
23 |
Correct |
119 ms |
46272 KB |
Output is correct |
24 |
Correct |
89 ms |
38864 KB |
Output is correct |
25 |
Correct |
94 ms |
45252 KB |
Output is correct |
26 |
Correct |
91 ms |
45252 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
456 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
3 ms |
2108 KB |
Output is correct |
33 |
Correct |
3 ms |
1904 KB |
Output is correct |
34 |
Correct |
3 ms |
1884 KB |
Output is correct |
35 |
Correct |
3 ms |
2028 KB |
Output is correct |
36 |
Correct |
171 ms |
82016 KB |
Output is correct |
37 |
Correct |
143 ms |
71936 KB |
Output is correct |
38 |
Correct |
137 ms |
63936 KB |
Output is correct |
39 |
Correct |
146 ms |
64352 KB |
Output is correct |
40 |
Correct |
137 ms |
63688 KB |
Output is correct |
41 |
Correct |
88 ms |
45248 KB |
Output is correct |
42 |
Correct |
87 ms |
45224 KB |
Output is correct |
43 |
Correct |
148 ms |
71940 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
6 ms |
2136 KB |
Output is correct |
50 |
Correct |
7 ms |
1880 KB |
Output is correct |
51 |
Correct |
9 ms |
1884 KB |
Output is correct |
52 |
Correct |
7 ms |
1884 KB |
Output is correct |
53 |
Correct |
179 ms |
82092 KB |
Output is correct |
54 |
Correct |
148 ms |
63952 KB |
Output is correct |
55 |
Correct |
180 ms |
64204 KB |
Output is correct |
56 |
Correct |
143 ms |
63692 KB |
Output is correct |
57 |
Correct |
201 ms |
79568 KB |
Output is correct |
58 |
Correct |
211 ms |
82184 KB |
Output is correct |
59 |
Correct |
171 ms |
72200 KB |
Output is correct |