#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 5e4 + 2;
const int inf = 2e9;
const int mod = 1e9 + 7;
int lift[N][16][5];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int k, n, m, q;
cin >> k >> n >> m >> q;
for (int i = 0; i < n; i++) {
for (int l = 0; l < 16; l++) {
for (int o = 0; o < k; o++) {
lift[i][l][o] = inf;
}
}
}
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
lift[a][0][b % k] = c;
}
for (int l = 1; l < 16; l++) {
for (int i = 0; i < n; i++) {
int nesto = k * (i / k + (1 << l) / 2);
int pom = k * (i / k + (1 << l));
for (int o = 0; o < k; o++) {
if (pom + o >= n) break;
for (int oo = 0; oo < k; oo++) {
if (lift[i][l - 1][oo] == inf || lift[nesto + oo][l - 1][o] == inf) continue;
smin(lift[i][l][o], lift[i][l - 1][oo] + lift[nesto + oo][l - 1][o]);
}
}
}
}
while (q--) {
int a, b;
cin >> a >> b;
int dn = b / k - a / k;
int cur = a / k;
vector<int> mn(k, inf);
mn[a % k] = 0;
for (int i = 0; i < 16; i++) {
if ((1 << i) & dn) {
vector<int> novi(k, inf);
for (int o = 0; o < k; o++) {
for (int oo = 0; oo < k; oo++) {
if (mn[oo] == inf || lift[cur * k + oo][i][o] == inf) continue;
smin(novi[o], mn[oo] + lift[cur * k + oo][i][o]);
}
}
cur += (1 << i);
swap(mn, novi);
}
}
if (mn[b % k] == inf) mn[b % k] = -1;
cout << mn[b % k] << en;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
16340 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 |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
47 ms |
16880 KB |
Output is correct |
9 |
Correct |
34 ms |
16852 KB |
Output is correct |
10 |
Correct |
18 ms |
16032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
16340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
736 KB |
Output is correct |
8 |
Correct |
6 ms |
728 KB |
Output is correct |
9 |
Correct |
45 ms |
16848 KB |
Output is correct |
10 |
Correct |
82 ms |
17164 KB |
Output is correct |
11 |
Correct |
58 ms |
17124 KB |
Output is correct |
12 |
Correct |
57 ms |
17116 KB |
Output is correct |
13 |
Correct |
78 ms |
10940 KB |
Output is correct |
14 |
Correct |
51 ms |
10856 KB |
Output is correct |
15 |
Correct |
58 ms |
10860 KB |
Output is correct |
16 |
Correct |
52 ms |
10856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
700 KB |
Output is correct |
10 |
Correct |
32 ms |
16768 KB |
Output is correct |
11 |
Correct |
68 ms |
17112 KB |
Output is correct |
12 |
Correct |
72 ms |
17116 KB |
Output is correct |
13 |
Correct |
74 ms |
17108 KB |
Output is correct |
14 |
Correct |
61 ms |
17072 KB |
Output is correct |
15 |
Correct |
53 ms |
10828 KB |
Output is correct |
16 |
Correct |
54 ms |
10860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
700 KB |
Output is correct |
10 |
Correct |
32 ms |
16768 KB |
Output is correct |
11 |
Correct |
68 ms |
17112 KB |
Output is correct |
12 |
Correct |
72 ms |
17116 KB |
Output is correct |
13 |
Correct |
74 ms |
17108 KB |
Output is correct |
14 |
Correct |
61 ms |
17072 KB |
Output is correct |
15 |
Correct |
53 ms |
10828 KB |
Output is correct |
16 |
Correct |
54 ms |
10860 KB |
Output is correct |
17 |
Correct |
48 ms |
17060 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 |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
2 ms |
600 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
25 |
Correct |
3 ms |
724 KB |
Output is correct |
26 |
Correct |
3 ms |
724 KB |
Output is correct |
27 |
Correct |
31 ms |
16724 KB |
Output is correct |
28 |
Correct |
69 ms |
17124 KB |
Output is correct |
29 |
Correct |
71 ms |
17048 KB |
Output is correct |
30 |
Correct |
85 ms |
17008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
16340 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 |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
47 ms |
16880 KB |
Output is correct |
9 |
Correct |
34 ms |
16852 KB |
Output is correct |
10 |
Correct |
18 ms |
16032 KB |
Output is correct |
11 |
Correct |
52 ms |
16340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
5 ms |
736 KB |
Output is correct |
18 |
Correct |
6 ms |
728 KB |
Output is correct |
19 |
Correct |
45 ms |
16848 KB |
Output is correct |
20 |
Correct |
82 ms |
17164 KB |
Output is correct |
21 |
Correct |
58 ms |
17124 KB |
Output is correct |
22 |
Correct |
57 ms |
17116 KB |
Output is correct |
23 |
Correct |
78 ms |
10940 KB |
Output is correct |
24 |
Correct |
51 ms |
10856 KB |
Output is correct |
25 |
Correct |
58 ms |
10860 KB |
Output is correct |
26 |
Correct |
52 ms |
10856 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
600 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
2 ms |
700 KB |
Output is correct |
36 |
Correct |
32 ms |
16768 KB |
Output is correct |
37 |
Correct |
68 ms |
17112 KB |
Output is correct |
38 |
Correct |
72 ms |
17116 KB |
Output is correct |
39 |
Correct |
74 ms |
17108 KB |
Output is correct |
40 |
Correct |
61 ms |
17072 KB |
Output is correct |
41 |
Correct |
53 ms |
10828 KB |
Output is correct |
42 |
Correct |
54 ms |
10860 KB |
Output is correct |
43 |
Correct |
48 ms |
17060 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 |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
2 ms |
600 KB |
Output is correct |
50 |
Correct |
2 ms |
600 KB |
Output is correct |
51 |
Correct |
3 ms |
724 KB |
Output is correct |
52 |
Correct |
3 ms |
724 KB |
Output is correct |
53 |
Correct |
31 ms |
16724 KB |
Output is correct |
54 |
Correct |
69 ms |
17124 KB |
Output is correct |
55 |
Correct |
71 ms |
17048 KB |
Output is correct |
56 |
Correct |
85 ms |
17008 KB |
Output is correct |
57 |
Correct |
109 ms |
17160 KB |
Output is correct |
58 |
Correct |
35 ms |
16852 KB |
Output is correct |
59 |
Correct |
56 ms |
17100 KB |
Output is correct |