#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int K, N, M, O;
vector <vector <pair <int, int>>> adj;
struct node {
vector <vector <ll>> dis;
node () {
dis = vector <vector <ll>> (K, vector <ll> (K, 1e18));
}
};
struct stree {
#define lc v*2
#define rc v*2+1
vector <node> tree;
stree (int s) {
tree.resize(s * 4);
}
node combine(node a, node b, int l, int r) {
node c;
int tm = (l + r) / 2;
for (int l1 = 0; l1 < K; l1++) {
for (int l2 = 0; l2 < K; l2++) {
for (int i = K*tm; i < K*(tm+1); i++) {
for (auto tp : adj[i]) {
int j = tp.first, w = tp.second;
c.dis[l1][l2] = min(c.dis[l1][l2], a.dis[l1][i % K] + w + b.dis[j % K][l2]);
}
}
}
}
return c;
}
void pushup(int v, int l, int r) {
node a = tree[lc];
node b = tree[rc];
tree[v] = combine(a, b, l, r);
}
void build(int v, int tl, int tr) {
if (tl == tr) {
for (int i = 0; i < K; i++)
tree[v].dis[i][i] = 0;
return;
}
int tm = (tl + tr) / 2;
build(lc, tl, tm);
build(rc, tm+1, tr);
pushup(v, tl, tr);
}
node query(int v, int tl, int tr, int l, int r) {
if (tl >= l && tr <= r)
return tree[v];
int tm = (tl + tr) / 2;
if (r <= tm)
return query(lc, tl, tm, l, r);
if (l >= tm+1)
return query(rc, tm+1, tr, l, r);
node a = query(lc, tl, tm, l, r);
node b = query(rc, tm+1, tr, l, r);
return combine(a, b, tl, tr);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> K >> N >> M >> O;
adj.resize(N+1);
for (int i = 0; i < M; i++) {
int a, b, w;
cin >> a >> b >> w;
adj[a].push_back({b, w});
}
stree tree((N-1)/K + 1);
tree.build(1, 0, (N-1)/K);
while (O--) {
int a, b;
cin >> a >> b;
node res = tree.query(1, 0, (N-1)/K, a/K, b/K);
ll ans = res.dis[a % K][b % K];
if (ans == 1e18)
ans = -1;
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
21224 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
644 KB |
Output is correct |
6 |
Correct |
5 ms |
860 KB |
Output is correct |
7 |
Correct |
5 ms |
860 KB |
Output is correct |
8 |
Correct |
101 ms |
21332 KB |
Output is correct |
9 |
Correct |
72 ms |
21076 KB |
Output is correct |
10 |
Correct |
52 ms |
18776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
19536 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
10 ms |
860 KB |
Output is correct |
8 |
Correct |
13 ms |
948 KB |
Output is correct |
9 |
Correct |
55 ms |
21096 KB |
Output is correct |
10 |
Correct |
95 ms |
19028 KB |
Output is correct |
11 |
Correct |
64 ms |
19796 KB |
Output is correct |
12 |
Correct |
79 ms |
17236 KB |
Output is correct |
13 |
Correct |
79 ms |
15184 KB |
Output is correct |
14 |
Correct |
41 ms |
11796 KB |
Output is correct |
15 |
Correct |
52 ms |
12880 KB |
Output is correct |
16 |
Correct |
59 ms |
12880 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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
42 ms |
21212 KB |
Output is correct |
11 |
Correct |
44 ms |
19540 KB |
Output is correct |
12 |
Correct |
54 ms |
19024 KB |
Output is correct |
13 |
Correct |
54 ms |
19540 KB |
Output is correct |
14 |
Correct |
46 ms |
18256 KB |
Output is correct |
15 |
Correct |
32 ms |
12632 KB |
Output is correct |
16 |
Correct |
33 ms |
12636 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 |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
860 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
42 ms |
21212 KB |
Output is correct |
11 |
Correct |
44 ms |
19540 KB |
Output is correct |
12 |
Correct |
54 ms |
19024 KB |
Output is correct |
13 |
Correct |
54 ms |
19540 KB |
Output is correct |
14 |
Correct |
46 ms |
18256 KB |
Output is correct |
15 |
Correct |
32 ms |
12632 KB |
Output is correct |
16 |
Correct |
33 ms |
12636 KB |
Output is correct |
17 |
Correct |
51 ms |
19444 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 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 |
860 KB |
Output is correct |
24 |
Correct |
7 ms |
860 KB |
Output is correct |
25 |
Correct |
27 ms |
936 KB |
Output is correct |
26 |
Correct |
16 ms |
860 KB |
Output is correct |
27 |
Correct |
52 ms |
21144 KB |
Output is correct |
28 |
Correct |
70 ms |
19028 KB |
Output is correct |
29 |
Correct |
73 ms |
19676 KB |
Output is correct |
30 |
Correct |
62 ms |
18260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
21224 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
644 KB |
Output is correct |
6 |
Correct |
5 ms |
860 KB |
Output is correct |
7 |
Correct |
5 ms |
860 KB |
Output is correct |
8 |
Correct |
101 ms |
21332 KB |
Output is correct |
9 |
Correct |
72 ms |
21076 KB |
Output is correct |
10 |
Correct |
52 ms |
18776 KB |
Output is correct |
11 |
Correct |
65 ms |
19536 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
10 ms |
860 KB |
Output is correct |
18 |
Correct |
13 ms |
948 KB |
Output is correct |
19 |
Correct |
55 ms |
21096 KB |
Output is correct |
20 |
Correct |
95 ms |
19028 KB |
Output is correct |
21 |
Correct |
64 ms |
19796 KB |
Output is correct |
22 |
Correct |
79 ms |
17236 KB |
Output is correct |
23 |
Correct |
79 ms |
15184 KB |
Output is correct |
24 |
Correct |
41 ms |
11796 KB |
Output is correct |
25 |
Correct |
52 ms |
12880 KB |
Output is correct |
26 |
Correct |
59 ms |
12880 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 |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
3 ms |
860 KB |
Output is correct |
35 |
Correct |
2 ms |
860 KB |
Output is correct |
36 |
Correct |
42 ms |
21212 KB |
Output is correct |
37 |
Correct |
44 ms |
19540 KB |
Output is correct |
38 |
Correct |
54 ms |
19024 KB |
Output is correct |
39 |
Correct |
54 ms |
19540 KB |
Output is correct |
40 |
Correct |
46 ms |
18256 KB |
Output is correct |
41 |
Correct |
32 ms |
12632 KB |
Output is correct |
42 |
Correct |
33 ms |
12636 KB |
Output is correct |
43 |
Correct |
51 ms |
19444 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 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 |
860 KB |
Output is correct |
50 |
Correct |
7 ms |
860 KB |
Output is correct |
51 |
Correct |
27 ms |
936 KB |
Output is correct |
52 |
Correct |
16 ms |
860 KB |
Output is correct |
53 |
Correct |
52 ms |
21144 KB |
Output is correct |
54 |
Correct |
70 ms |
19028 KB |
Output is correct |
55 |
Correct |
73 ms |
19676 KB |
Output is correct |
56 |
Correct |
62 ms |
18260 KB |
Output is correct |
57 |
Correct |
199 ms |
23640 KB |
Output is correct |
58 |
Correct |
93 ms |
21320 KB |
Output is correct |
59 |
Correct |
81 ms |
19796 KB |
Output is correct |