#include <bits/stdc++.h>
using namespace std;
#define int long long
using T = array<array<int, 5>, 6>;
const int MAXN = 1e5, INF = 1e16;
vector<array<int, 2>> g[MAXN];
int n, k, m, o, s;
T ID, a[1<<17];
T comb(T x, T y){
if(x[5][0] > y[5][0]) swap(x, y);
int z = y[5][0];
if(z == INF) return x;
T res = ID;
res[5][0] = x[5][0];
for(int i=0; i<k; ++i)
for(int u=0; u<k; ++u)
for(auto &[v, w] : g[(z-1)*k+u])
for(int j=0; j<k; ++j)
res[i][j] = min(res[i][j], x[i][u] + w + y[v%k][j]);
return res;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> k >> n >> m >> o;
while(m--){
int u, v, w; cin >> u >> v >> w;
g[u].push_back({v, w});
}
for(int i=0; i<6; ++i) for(int j=0; j<5; ++j) ID[i][j] = (i!=j) * INF;
s = (n + k - 1) / k;
fill(a, a+2*s, ID);
for(int i=0; i<s; ++i) a[i+s][5][0] = i;
for(int i=0; i<5; ++i) ID[i][i] = INF;
for(int i=s; --i; ) a[i] = comb(a[2*i], a[2*i+1]);
while(o--){
int i, j; cin >> i >> j;
int l = i/k, r = j/k;
T lx = ID, rx = ID;
for(l+=s, r+=s+1; l<r; l/=2, r/=2){
if(l & 1) lx = comb(lx, a[l++]);
if(r & 1) rx = comb(rx, a[--r]);
}
i = comb(lx, rx)[i % k][j % k];
cout << (i == INF ? -1 : i) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
28248 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
4 ms |
3148 KB |
Output is correct |
6 |
Correct |
4 ms |
3156 KB |
Output is correct |
7 |
Correct |
5 ms |
3200 KB |
Output is correct |
8 |
Correct |
98 ms |
28364 KB |
Output is correct |
9 |
Correct |
72 ms |
28236 KB |
Output is correct |
10 |
Correct |
46 ms |
26276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
17440 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 |
14 ms |
3264 KB |
Output is correct |
8 |
Correct |
16 ms |
3072 KB |
Output is correct |
9 |
Correct |
65 ms |
28364 KB |
Output is correct |
10 |
Correct |
129 ms |
15044 KB |
Output is correct |
11 |
Correct |
87 ms |
17732 KB |
Output is correct |
12 |
Correct |
90 ms |
13176 KB |
Output is correct |
13 |
Correct |
118 ms |
10836 KB |
Output is correct |
14 |
Correct |
57 ms |
10592 KB |
Output is correct |
15 |
Correct |
79 ms |
8124 KB |
Output is correct |
16 |
Correct |
66 ms |
8080 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 |
3 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 |
3148 KB |
Output is correct |
7 |
Correct |
4 ms |
2892 KB |
Output is correct |
8 |
Correct |
5 ms |
2892 KB |
Output is correct |
9 |
Correct |
3 ms |
2812 KB |
Output is correct |
10 |
Correct |
44 ms |
28280 KB |
Output is correct |
11 |
Correct |
53 ms |
17552 KB |
Output is correct |
12 |
Correct |
88 ms |
15004 KB |
Output is correct |
13 |
Correct |
85 ms |
15432 KB |
Output is correct |
14 |
Correct |
63 ms |
14204 KB |
Output is correct |
15 |
Correct |
40 ms |
8124 KB |
Output is correct |
16 |
Correct |
49 ms |
8048 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 |
3 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 |
3148 KB |
Output is correct |
7 |
Correct |
4 ms |
2892 KB |
Output is correct |
8 |
Correct |
5 ms |
2892 KB |
Output is correct |
9 |
Correct |
3 ms |
2812 KB |
Output is correct |
10 |
Correct |
44 ms |
28280 KB |
Output is correct |
11 |
Correct |
53 ms |
17552 KB |
Output is correct |
12 |
Correct |
88 ms |
15004 KB |
Output is correct |
13 |
Correct |
85 ms |
15432 KB |
Output is correct |
14 |
Correct |
63 ms |
14204 KB |
Output is correct |
15 |
Correct |
40 ms |
8124 KB |
Output is correct |
16 |
Correct |
49 ms |
8048 KB |
Output is correct |
17 |
Correct |
78 ms |
17564 KB |
Output is correct |
18 |
Correct |
2 ms |
2636 KB |
Output is correct |
19 |
Correct |
3 ms |
2636 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 |
6 ms |
3148 KB |
Output is correct |
24 |
Correct |
8 ms |
3020 KB |
Output is correct |
25 |
Correct |
21 ms |
2968 KB |
Output is correct |
26 |
Correct |
14 ms |
2952 KB |
Output is correct |
27 |
Correct |
60 ms |
28288 KB |
Output is correct |
28 |
Correct |
121 ms |
14980 KB |
Output is correct |
29 |
Correct |
113 ms |
15532 KB |
Output is correct |
30 |
Correct |
77 ms |
14304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
28248 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
4 ms |
3148 KB |
Output is correct |
6 |
Correct |
4 ms |
3156 KB |
Output is correct |
7 |
Correct |
5 ms |
3200 KB |
Output is correct |
8 |
Correct |
98 ms |
28364 KB |
Output is correct |
9 |
Correct |
72 ms |
28236 KB |
Output is correct |
10 |
Correct |
46 ms |
26276 KB |
Output is correct |
11 |
Correct |
100 ms |
17440 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 |
14 ms |
3264 KB |
Output is correct |
18 |
Correct |
16 ms |
3072 KB |
Output is correct |
19 |
Correct |
65 ms |
28364 KB |
Output is correct |
20 |
Correct |
129 ms |
15044 KB |
Output is correct |
21 |
Correct |
87 ms |
17732 KB |
Output is correct |
22 |
Correct |
90 ms |
13176 KB |
Output is correct |
23 |
Correct |
118 ms |
10836 KB |
Output is correct |
24 |
Correct |
57 ms |
10592 KB |
Output is correct |
25 |
Correct |
79 ms |
8124 KB |
Output is correct |
26 |
Correct |
66 ms |
8080 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2636 KB |
Output is correct |
29 |
Correct |
3 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 |
3148 KB |
Output is correct |
33 |
Correct |
4 ms |
2892 KB |
Output is correct |
34 |
Correct |
5 ms |
2892 KB |
Output is correct |
35 |
Correct |
3 ms |
2812 KB |
Output is correct |
36 |
Correct |
44 ms |
28280 KB |
Output is correct |
37 |
Correct |
53 ms |
17552 KB |
Output is correct |
38 |
Correct |
88 ms |
15004 KB |
Output is correct |
39 |
Correct |
85 ms |
15432 KB |
Output is correct |
40 |
Correct |
63 ms |
14204 KB |
Output is correct |
41 |
Correct |
40 ms |
8124 KB |
Output is correct |
42 |
Correct |
49 ms |
8048 KB |
Output is correct |
43 |
Correct |
78 ms |
17564 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
3 ms |
2636 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 |
6 ms |
3148 KB |
Output is correct |
50 |
Correct |
8 ms |
3020 KB |
Output is correct |
51 |
Correct |
21 ms |
2968 KB |
Output is correct |
52 |
Correct |
14 ms |
2952 KB |
Output is correct |
53 |
Correct |
60 ms |
28288 KB |
Output is correct |
54 |
Correct |
121 ms |
14980 KB |
Output is correct |
55 |
Correct |
113 ms |
15532 KB |
Output is correct |
56 |
Correct |
77 ms |
14304 KB |
Output is correct |
57 |
Correct |
200 ms |
13636 KB |
Output is correct |
58 |
Correct |
84 ms |
28276 KB |
Output is correct |
59 |
Correct |
99 ms |
17680 KB |
Output is correct |