#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
int n, m, q, k;
int a[50001];
vector<ii> adj[50001], adj_re[50001];
int val[20][50001][5][5];
int comb(int x, int y) {
return min(x, y); // Replace min with any associative operation
}
void init(int l, int r, int lev) {
if (l == r)
return;
int mi = (l + r) / 2;
for (int i = 0; i < k; i++) {
for (int j = 0; j < k; j++)
val[lev][mi][i][j] = 1e9;
val[lev][mi][i][i] = 0;
}
for (int i = mi - 1; i >= l; i--) {
for (int j = 0; j < k; j++) {
for (int z = 0 ; z < k; z++)
val[lev][i][j][z] = 1e9;
int u = i * k + j;
for (ii _ : adj[u]) {
int v = _.first, c = _.second;
int nxt = v % k;
for (int z = 0; z < k; z++)
val[lev][i][j][z] = comb(val[lev][i][j][z], val[lev][i + 1][nxt][z] + c);
}
}
}
for (int i = mi + 1; i <= r; i++) {
for (int j = 0; j < k; j++) {
for (int z = 0 ; z < k; z++)
val[lev][i][j][z] = 1e9;
int u = i * k + j;
for (ii _ : adj_re[u]) {
int v = _.first, c = _.second;
int nxt = v % k;
for (int z = 0; z < k; z++)
val[lev][i][j][z] = comb(val[lev][i - 1][nxt][z] + c, val[lev][i][j][z]);
}
}
}
if (r - l <= 1) return;
init(l, mi, lev + 1);
init(mi, r, lev + 1);
}
int main(int argc, char** argv)
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> k >> n >> m >> q;
for (int i = 0; i < m; i++) {
int u, v, c; cin >> u >> v >> c;
adj[u].push_back(ii(v, c));
adj_re[v].push_back(ii(u, c));
}
init(0, (n - 1)/k, 0);
while (q--) {
int u, v; cin >> u >> v;
int id1 = u / k, id2 = v / k;
if (id1 == id2) cout << -1 << '\n';
else {
int lev = 0;
int l = 0, r = (n - 1)/k;
while(1) {
int mi = (l + r) / 2;
if (id1 <= mi && mi <= id2) break;
else {
if (mi < id1) l = mi;
else r = mi;
lev++;
}
}
int ans = 1e9;
for (int i = 0; i < k; i++)
ans = min(ans, val[lev][id1][u % k][i] + val[lev][id2][v % k][i]);
if (ans >= 1e9) cout << -1 << '\n';
else cout << ans << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
89028 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2676 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
3872 KB |
Output is correct |
6 |
Correct |
4 ms |
3844 KB |
Output is correct |
7 |
Correct |
3 ms |
3916 KB |
Output is correct |
8 |
Correct |
94 ms |
89880 KB |
Output is correct |
9 |
Correct |
112 ms |
89596 KB |
Output is correct |
10 |
Correct |
47 ms |
85872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
44996 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
6 ms |
3908 KB |
Output is correct |
8 |
Correct |
6 ms |
3276 KB |
Output is correct |
9 |
Correct |
94 ms |
88952 KB |
Output is correct |
10 |
Correct |
149 ms |
35568 KB |
Output is correct |
11 |
Correct |
114 ms |
46788 KB |
Output is correct |
12 |
Correct |
91 ms |
32524 KB |
Output is correct |
13 |
Correct |
132 ms |
18668 KB |
Output is correct |
14 |
Correct |
87 ms |
22152 KB |
Output is correct |
15 |
Correct |
74 ms |
14788 KB |
Output is correct |
16 |
Correct |
72 ms |
14784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
3788 KB |
Output is correct |
7 |
Correct |
3 ms |
3276 KB |
Output is correct |
8 |
Correct |
4 ms |
3120 KB |
Output is correct |
9 |
Correct |
4 ms |
3020 KB |
Output is correct |
10 |
Correct |
90 ms |
88920 KB |
Output is correct |
11 |
Correct |
102 ms |
46532 KB |
Output is correct |
12 |
Correct |
135 ms |
35396 KB |
Output is correct |
13 |
Correct |
175 ms |
36092 KB |
Output is correct |
14 |
Correct |
118 ms |
34404 KB |
Output is correct |
15 |
Correct |
68 ms |
14772 KB |
Output is correct |
16 |
Correct |
64 ms |
14692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
3788 KB |
Output is correct |
7 |
Correct |
3 ms |
3276 KB |
Output is correct |
8 |
Correct |
4 ms |
3120 KB |
Output is correct |
9 |
Correct |
4 ms |
3020 KB |
Output is correct |
10 |
Correct |
90 ms |
88920 KB |
Output is correct |
11 |
Correct |
102 ms |
46532 KB |
Output is correct |
12 |
Correct |
135 ms |
35396 KB |
Output is correct |
13 |
Correct |
175 ms |
36092 KB |
Output is correct |
14 |
Correct |
118 ms |
34404 KB |
Output is correct |
15 |
Correct |
68 ms |
14772 KB |
Output is correct |
16 |
Correct |
64 ms |
14692 KB |
Output is correct |
17 |
Correct |
95 ms |
46636 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
2 ms |
2676 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
4 ms |
3836 KB |
Output is correct |
24 |
Correct |
4 ms |
3276 KB |
Output is correct |
25 |
Correct |
5 ms |
3148 KB |
Output is correct |
26 |
Correct |
6 ms |
3076 KB |
Output is correct |
27 |
Correct |
95 ms |
89724 KB |
Output is correct |
28 |
Correct |
134 ms |
35524 KB |
Output is correct |
29 |
Correct |
152 ms |
36176 KB |
Output is correct |
30 |
Correct |
119 ms |
34380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
89028 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2676 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
3 ms |
3872 KB |
Output is correct |
6 |
Correct |
4 ms |
3844 KB |
Output is correct |
7 |
Correct |
3 ms |
3916 KB |
Output is correct |
8 |
Correct |
94 ms |
89880 KB |
Output is correct |
9 |
Correct |
112 ms |
89596 KB |
Output is correct |
10 |
Correct |
47 ms |
85872 KB |
Output is correct |
11 |
Correct |
106 ms |
44996 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2636 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
6 ms |
3908 KB |
Output is correct |
18 |
Correct |
6 ms |
3276 KB |
Output is correct |
19 |
Correct |
94 ms |
88952 KB |
Output is correct |
20 |
Correct |
149 ms |
35568 KB |
Output is correct |
21 |
Correct |
114 ms |
46788 KB |
Output is correct |
22 |
Correct |
91 ms |
32524 KB |
Output is correct |
23 |
Correct |
132 ms |
18668 KB |
Output is correct |
24 |
Correct |
87 ms |
22152 KB |
Output is correct |
25 |
Correct |
74 ms |
14788 KB |
Output is correct |
26 |
Correct |
72 ms |
14784 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2636 KB |
Output is correct |
31 |
Correct |
2 ms |
2636 KB |
Output is correct |
32 |
Correct |
3 ms |
3788 KB |
Output is correct |
33 |
Correct |
3 ms |
3276 KB |
Output is correct |
34 |
Correct |
4 ms |
3120 KB |
Output is correct |
35 |
Correct |
4 ms |
3020 KB |
Output is correct |
36 |
Correct |
90 ms |
88920 KB |
Output is correct |
37 |
Correct |
102 ms |
46532 KB |
Output is correct |
38 |
Correct |
135 ms |
35396 KB |
Output is correct |
39 |
Correct |
175 ms |
36092 KB |
Output is correct |
40 |
Correct |
118 ms |
34404 KB |
Output is correct |
41 |
Correct |
68 ms |
14772 KB |
Output is correct |
42 |
Correct |
64 ms |
14692 KB |
Output is correct |
43 |
Correct |
95 ms |
46636 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
2 ms |
2676 KB |
Output is correct |
46 |
Correct |
2 ms |
2636 KB |
Output is correct |
47 |
Correct |
2 ms |
2636 KB |
Output is correct |
48 |
Correct |
2 ms |
2636 KB |
Output is correct |
49 |
Correct |
4 ms |
3836 KB |
Output is correct |
50 |
Correct |
4 ms |
3276 KB |
Output is correct |
51 |
Correct |
5 ms |
3148 KB |
Output is correct |
52 |
Correct |
6 ms |
3076 KB |
Output is correct |
53 |
Correct |
95 ms |
89724 KB |
Output is correct |
54 |
Correct |
134 ms |
35524 KB |
Output is correct |
55 |
Correct |
152 ms |
36176 KB |
Output is correct |
56 |
Correct |
119 ms |
34380 KB |
Output is correct |
57 |
Correct |
205 ms |
29884 KB |
Output is correct |
58 |
Correct |
93 ms |
89900 KB |
Output is correct |
59 |
Correct |
111 ms |
46768 KB |
Output is correct |