#include <iostream>
#include <array>
#include <cassert>
#include <algorithm>
#include <string>
#include <bitset>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#ifndef LOCAL
#include <bits/stdc++.h>
#define cerr if(false)cerr
#endif
using namespace std;
using ll = long long;
const int N = 5e4 + 66;
const int K = 5;
int k;
using mat = array<array<ll, K>, K>;
mat g[N];
const ll inf = 1e18 + 126;
mat compute(mat a, mat b) {
mat c; for (int i = 0 ; i < K ; ++ i) for (int j = 0 ; j < K ; ++ j) c[i][j] = inf;
for (int i = 0 ; i < k ; ++ i) for (int j = 0 ; j < k ; ++ j) for (int l = 0 ; l < k ; ++ l) {
c[i][j] = min(c[i][j], a[i][l] + b[l][j]);
}
return c;
}
const int LOG = 18;
mat sp[N][LOG];
mat compute(int l, int r) {
mat cur; for (int i = 0 ; i < K ; ++ i) for (int j = 0 ; j < K ; ++ j) cur[i][j] = inf;
bool st = false;
int len = r - l + 1;
cerr << l << " " << r << " sdf\n";
for (int i = 0 ; i < LOG ; ++ i) {
if (len >> i & 1) {
cerr << "[" << l << " " << l + (1 << i) - 1 << "]\n";
if (!st) cur = sp[l][i], st = true;
else cur = compute(cur, sp[l][i]);
l += 1 << i;
}
}
return cur;
}
mat mid;
int qa[N], qb[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> k >> n >> m >> q;
for (int b = 0 ; b < n ; ++ b) {
for (int i = 0 ; i < k ; ++ i) {
for (int j = 0 ; j < k ; ++ j) {
g[b][i][j] = inf;
}
}
}
while (m--) {
int a, b, c;
cin >> a >> b >> c;
int ba = a / k, bb = b / k;
assert(ba + 1 == bb);
int ia = a % k, ib = b % k;
g[ba][ia][ib] = c;
}
for (int i = 0 ; i < n ; ++ i) {
sp[i][0] = g[i];
}
for (int j = 1 ; j < LOG ; ++ j) {
for (int i = 0 ; i + (1 << j) - 1 < n ; ++ i) {
sp[i][j] = compute(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
}
}
for (int i = 0 ; i < q ; ++ i) {
cin >> qa[i] >> qb[i];
int ba = qa[i] / k, bb = qb[i] / k;
int ia = qa[i] % k, ib = qb[i] % k;
cerr << qa[i] << " " << qb[i] << "|" << ba << " " << bb << "|" << ia << " " << ib << endl;
ll ans;
if (ba == bb) {
ans = inf;
} else if (ba == bb - 1) {
ans = g[ba][ia][ib];
} else {
ans = inf;
int l = ba + 1, r = bb - 2;
mid = compute(l, r);
for (int s = 0 ; s < k ; ++ s) {
for (int t = 0 ; t < k ; ++ t) {
cerr << mid[s][t] << " ";
ans = min(ans, (ba == bb - 2 && s == t ? 0 : mid[s][t]) + g[ba][ia][s] + g[bb - 1][t][ib]);
}
cerr << endl;
}
}
if (ans >= inf) ans = -1;
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
186336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
4052 KB |
Output is correct |
6 |
Correct |
5 ms |
4052 KB |
Output is correct |
7 |
Correct |
3 ms |
4052 KB |
Output is correct |
8 |
Correct |
233 ms |
187240 KB |
Output is correct |
9 |
Correct |
225 ms |
187088 KB |
Output is correct |
10 |
Correct |
217 ms |
186456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
186292 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
8 ms |
4180 KB |
Output is correct |
8 |
Correct |
10 ms |
4180 KB |
Output is correct |
9 |
Correct |
216 ms |
187108 KB |
Output is correct |
10 |
Correct |
236 ms |
188636 KB |
Output is correct |
11 |
Correct |
222 ms |
188024 KB |
Output is correct |
12 |
Correct |
224 ms |
187496 KB |
Output is correct |
13 |
Correct |
189 ms |
114240 KB |
Output is correct |
14 |
Correct |
140 ms |
113276 KB |
Output is correct |
15 |
Correct |
171 ms |
113064 KB |
Output is correct |
16 |
Correct |
188 ms |
113068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
4052 KB |
Output is correct |
7 |
Correct |
3 ms |
4052 KB |
Output is correct |
8 |
Correct |
5 ms |
4080 KB |
Output is correct |
9 |
Correct |
5 ms |
4040 KB |
Output is correct |
10 |
Correct |
200 ms |
187024 KB |
Output is correct |
11 |
Correct |
201 ms |
187840 KB |
Output is correct |
12 |
Correct |
226 ms |
188380 KB |
Output is correct |
13 |
Correct |
231 ms |
188596 KB |
Output is correct |
14 |
Correct |
229 ms |
188024 KB |
Output is correct |
15 |
Correct |
165 ms |
112952 KB |
Output is correct |
16 |
Correct |
163 ms |
113100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
3 ms |
4052 KB |
Output is correct |
7 |
Correct |
3 ms |
4052 KB |
Output is correct |
8 |
Correct |
5 ms |
4080 KB |
Output is correct |
9 |
Correct |
5 ms |
4040 KB |
Output is correct |
10 |
Correct |
200 ms |
187024 KB |
Output is correct |
11 |
Correct |
201 ms |
187840 KB |
Output is correct |
12 |
Correct |
226 ms |
188380 KB |
Output is correct |
13 |
Correct |
231 ms |
188596 KB |
Output is correct |
14 |
Correct |
229 ms |
188024 KB |
Output is correct |
15 |
Correct |
165 ms |
112952 KB |
Output is correct |
16 |
Correct |
163 ms |
113100 KB |
Output is correct |
17 |
Correct |
218 ms |
187800 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
4048 KB |
Output is correct |
24 |
Correct |
4 ms |
4052 KB |
Output is correct |
25 |
Correct |
6 ms |
4180 KB |
Output is correct |
26 |
Correct |
6 ms |
4164 KB |
Output is correct |
27 |
Correct |
203 ms |
187000 KB |
Output is correct |
28 |
Correct |
259 ms |
188416 KB |
Output is correct |
29 |
Correct |
228 ms |
188652 KB |
Output is correct |
30 |
Correct |
233 ms |
188004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
186336 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
4052 KB |
Output is correct |
6 |
Correct |
5 ms |
4052 KB |
Output is correct |
7 |
Correct |
3 ms |
4052 KB |
Output is correct |
8 |
Correct |
233 ms |
187240 KB |
Output is correct |
9 |
Correct |
225 ms |
187088 KB |
Output is correct |
10 |
Correct |
217 ms |
186456 KB |
Output is correct |
11 |
Correct |
214 ms |
186292 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
8 ms |
4180 KB |
Output is correct |
18 |
Correct |
10 ms |
4180 KB |
Output is correct |
19 |
Correct |
216 ms |
187108 KB |
Output is correct |
20 |
Correct |
236 ms |
188636 KB |
Output is correct |
21 |
Correct |
222 ms |
188024 KB |
Output is correct |
22 |
Correct |
224 ms |
187496 KB |
Output is correct |
23 |
Correct |
189 ms |
114240 KB |
Output is correct |
24 |
Correct |
140 ms |
113276 KB |
Output is correct |
25 |
Correct |
171 ms |
113064 KB |
Output is correct |
26 |
Correct |
188 ms |
113068 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
4052 KB |
Output is correct |
33 |
Correct |
3 ms |
4052 KB |
Output is correct |
34 |
Correct |
5 ms |
4080 KB |
Output is correct |
35 |
Correct |
5 ms |
4040 KB |
Output is correct |
36 |
Correct |
200 ms |
187024 KB |
Output is correct |
37 |
Correct |
201 ms |
187840 KB |
Output is correct |
38 |
Correct |
226 ms |
188380 KB |
Output is correct |
39 |
Correct |
231 ms |
188596 KB |
Output is correct |
40 |
Correct |
229 ms |
188024 KB |
Output is correct |
41 |
Correct |
165 ms |
112952 KB |
Output is correct |
42 |
Correct |
163 ms |
113100 KB |
Output is correct |
43 |
Correct |
218 ms |
187800 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
324 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
4 ms |
4048 KB |
Output is correct |
50 |
Correct |
4 ms |
4052 KB |
Output is correct |
51 |
Correct |
6 ms |
4180 KB |
Output is correct |
52 |
Correct |
6 ms |
4164 KB |
Output is correct |
53 |
Correct |
203 ms |
187000 KB |
Output is correct |
54 |
Correct |
259 ms |
188416 KB |
Output is correct |
55 |
Correct |
228 ms |
188652 KB |
Output is correct |
56 |
Correct |
233 ms |
188004 KB |
Output is correct |
57 |
Correct |
278 ms |
189632 KB |
Output is correct |
58 |
Correct |
211 ms |
187160 KB |
Output is correct |
59 |
Correct |
220 ms |
188064 KB |
Output is correct |