#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
struct Edge {
int v, w;
};
const int MXN = 5e4+5;
const int INF = 1e9;
int k, n, m, o;
// [a][b][c] = from a move 2^b blocks ahead to position = c (mod k)
int jump[MXN][16][5];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n >> m >> o;
for(int a = 0; a < n; a++)
for(int b = 0; b < 16; b++)
for(int c = 0; c < 5; c++)
jump[a][b][c] = INF;
for(int i = 0; i < m; i++) {
int u, v, w; cin >> u >> v >> w;
jump[u][0][v%k] = w;
}
for(int p = 1; p < 16; p++) {
int blocks = 1 << p;
for(int a = 0; a < n; a++) {
if(a+k*blocks-a%k >= n) continue;
for(int b = 0; b < 5; b++)
for(int c = 0; c < 5; c++) {
jump[a][p][c] = min(jump[a][p][c], jump[a][p-1][b]+jump[a+k*(blocks/2)-a%k+b][p-1][c]);
}
}
}
for(int _ = 0; _ < o; _++) {
int a, b; cin >> a >> b;
vi dist(5, INF);
dist[a%k] = 0;
for(int p = 15; p >= 0; p--) {
int blocks = 1 << p;
if(a+k*blocks-a%k >= n) continue;
while(a+k*blocks-a%k <= b) {
vi ndist(5, INF);
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
ndist[j] = min(ndist[j], dist[i]+jump[a-a%k+i][p][j]);
}
}
a = a+k*blocks;
dist = ndist;
}
}
int d = dist[b%k];
if(d == INF) cout << "-1\n";
else cout << d << "\n";
}
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
15960 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
648 KB |
Output is correct |
8 |
Correct |
85 ms |
15956 KB |
Output is correct |
9 |
Correct |
76 ms |
15996 KB |
Output is correct |
10 |
Correct |
59 ms |
15960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
15964 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
7 ms |
596 KB |
Output is correct |
8 |
Correct |
7 ms |
596 KB |
Output is correct |
9 |
Correct |
64 ms |
15980 KB |
Output is correct |
10 |
Correct |
78 ms |
15988 KB |
Output is correct |
11 |
Correct |
73 ms |
15972 KB |
Output is correct |
12 |
Correct |
69 ms |
15968 KB |
Output is correct |
13 |
Correct |
65 ms |
9700 KB |
Output is correct |
14 |
Correct |
57 ms |
9700 KB |
Output is correct |
15 |
Correct |
36 ms |
9684 KB |
Output is correct |
16 |
Correct |
37 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
68 ms |
15972 KB |
Output is correct |
11 |
Correct |
63 ms |
15956 KB |
Output is correct |
12 |
Correct |
68 ms |
15964 KB |
Output is correct |
13 |
Correct |
73 ms |
16052 KB |
Output is correct |
14 |
Correct |
64 ms |
15956 KB |
Output is correct |
15 |
Correct |
36 ms |
9704 KB |
Output is correct |
16 |
Correct |
35 ms |
9644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
68 ms |
15972 KB |
Output is correct |
11 |
Correct |
63 ms |
15956 KB |
Output is correct |
12 |
Correct |
68 ms |
15964 KB |
Output is correct |
13 |
Correct |
73 ms |
16052 KB |
Output is correct |
14 |
Correct |
64 ms |
15956 KB |
Output is correct |
15 |
Correct |
36 ms |
9704 KB |
Output is correct |
16 |
Correct |
35 ms |
9644 KB |
Output is correct |
17 |
Correct |
69 ms |
15964 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
724 KB |
Output is correct |
25 |
Correct |
4 ms |
724 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
61 ms |
16724 KB |
Output is correct |
28 |
Correct |
84 ms |
17700 KB |
Output is correct |
29 |
Correct |
87 ms |
17616 KB |
Output is correct |
30 |
Correct |
70 ms |
17628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
15960 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
648 KB |
Output is correct |
8 |
Correct |
85 ms |
15956 KB |
Output is correct |
9 |
Correct |
76 ms |
15996 KB |
Output is correct |
10 |
Correct |
59 ms |
15960 KB |
Output is correct |
11 |
Correct |
73 ms |
15964 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
7 ms |
596 KB |
Output is correct |
18 |
Correct |
7 ms |
596 KB |
Output is correct |
19 |
Correct |
64 ms |
15980 KB |
Output is correct |
20 |
Correct |
78 ms |
15988 KB |
Output is correct |
21 |
Correct |
73 ms |
15972 KB |
Output is correct |
22 |
Correct |
69 ms |
15968 KB |
Output is correct |
23 |
Correct |
65 ms |
9700 KB |
Output is correct |
24 |
Correct |
57 ms |
9700 KB |
Output is correct |
25 |
Correct |
36 ms |
9684 KB |
Output is correct |
26 |
Correct |
37 ms |
9684 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
68 ms |
15972 KB |
Output is correct |
37 |
Correct |
63 ms |
15956 KB |
Output is correct |
38 |
Correct |
68 ms |
15964 KB |
Output is correct |
39 |
Correct |
73 ms |
16052 KB |
Output is correct |
40 |
Correct |
64 ms |
15956 KB |
Output is correct |
41 |
Correct |
36 ms |
9704 KB |
Output is correct |
42 |
Correct |
35 ms |
9644 KB |
Output is correct |
43 |
Correct |
69 ms |
15964 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
3 ms |
596 KB |
Output is correct |
50 |
Correct |
3 ms |
724 KB |
Output is correct |
51 |
Correct |
4 ms |
724 KB |
Output is correct |
52 |
Correct |
3 ms |
724 KB |
Output is correct |
53 |
Correct |
61 ms |
16724 KB |
Output is correct |
54 |
Correct |
84 ms |
17700 KB |
Output is correct |
55 |
Correct |
87 ms |
17616 KB |
Output is correct |
56 |
Correct |
70 ms |
17628 KB |
Output is correct |
57 |
Correct |
116 ms |
17796 KB |
Output is correct |
58 |
Correct |
83 ms |
16972 KB |
Output is correct |
59 |
Correct |
85 ms |
17680 KB |
Output is correct |