#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 >= 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 >= 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 |
72 ms |
15956 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
660 KB |
Output is correct |
6 |
Correct |
2 ms |
656 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
71 ms |
16848 KB |
Output is correct |
9 |
Correct |
83 ms |
16848 KB |
Output is correct |
10 |
Correct |
66 ms |
16008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 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 |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
7 ms |
724 KB |
Output is correct |
8 |
Correct |
7 ms |
740 KB |
Output is correct |
9 |
Correct |
67 ms |
16844 KB |
Output is correct |
10 |
Correct |
81 ms |
17784 KB |
Output is correct |
11 |
Correct |
81 ms |
17736 KB |
Output is correct |
12 |
Correct |
69 ms |
17240 KB |
Output is correct |
13 |
Correct |
61 ms |
11476 KB |
Output is correct |
14 |
Correct |
43 ms |
11144 KB |
Output is correct |
15 |
Correct |
38 ms |
10828 KB |
Output is correct |
16 |
Correct |
53 ms |
10908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
58 ms |
16724 KB |
Output is correct |
11 |
Correct |
71 ms |
17480 KB |
Output is correct |
12 |
Correct |
95 ms |
17744 KB |
Output is correct |
13 |
Correct |
91 ms |
17632 KB |
Output is correct |
14 |
Correct |
68 ms |
17720 KB |
Output is correct |
15 |
Correct |
38 ms |
10860 KB |
Output is correct |
16 |
Correct |
36 ms |
10888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
58 ms |
16724 KB |
Output is correct |
11 |
Correct |
71 ms |
17480 KB |
Output is correct |
12 |
Correct |
95 ms |
17744 KB |
Output is correct |
13 |
Correct |
91 ms |
17632 KB |
Output is correct |
14 |
Correct |
68 ms |
17720 KB |
Output is correct |
15 |
Correct |
38 ms |
10860 KB |
Output is correct |
16 |
Correct |
36 ms |
10888 KB |
Output is correct |
17 |
Correct |
91 ms |
17520 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
328 KB |
Output is correct |
21 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
15956 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
660 KB |
Output is correct |
6 |
Correct |
2 ms |
656 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
71 ms |
16848 KB |
Output is correct |
9 |
Correct |
83 ms |
16848 KB |
Output is correct |
10 |
Correct |
66 ms |
16008 KB |
Output is correct |
11 |
Correct |
75 ms |
15960 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
7 ms |
724 KB |
Output is correct |
18 |
Correct |
7 ms |
740 KB |
Output is correct |
19 |
Correct |
67 ms |
16844 KB |
Output is correct |
20 |
Correct |
81 ms |
17784 KB |
Output is correct |
21 |
Correct |
81 ms |
17736 KB |
Output is correct |
22 |
Correct |
69 ms |
17240 KB |
Output is correct |
23 |
Correct |
61 ms |
11476 KB |
Output is correct |
24 |
Correct |
43 ms |
11144 KB |
Output is correct |
25 |
Correct |
38 ms |
10828 KB |
Output is correct |
26 |
Correct |
53 ms |
10908 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
1 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
596 KB |
Output is correct |
36 |
Correct |
58 ms |
16724 KB |
Output is correct |
37 |
Correct |
71 ms |
17480 KB |
Output is correct |
38 |
Correct |
95 ms |
17744 KB |
Output is correct |
39 |
Correct |
91 ms |
17632 KB |
Output is correct |
40 |
Correct |
68 ms |
17720 KB |
Output is correct |
41 |
Correct |
38 ms |
10860 KB |
Output is correct |
42 |
Correct |
36 ms |
10888 KB |
Output is correct |
43 |
Correct |
91 ms |
17520 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
328 KB |
Output is correct |
47 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
48 |
Halted |
0 ms |
0 KB |
- |