#include "bits/stdc++.h"
using namespace std;
const int Log = 17;
const long long INF = 1e15;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int k, n, m, q;
cin >> k >> n >> m >> q;
vector<vector<vector<long long>>> jump(Log, vector<vector<long long>> (n, vector<long long> (k, INF)));
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
jump[0][u][v % k] = w;
}
map<int, int> mp;
for (int i = 0; i < Log; ++i) {
mp[1 << i] = i;
}
for (int bit = 1; bit < Log; ++bit) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
int x = i - (i % k) + (1 << (bit - 1)) * k + j;
if (x >= n) {
break;
}
for (int l = 0; l < k; ++l) {
jump[bit][i][l] = min(jump[bit][i][l], jump[bit - 1][i][j] + jump[bit - 1][x][l]);
}
}
}
}
while (q--) {
int u, v;
cin >> u >> v;
int diff = v / k - u / k;
if (diff <= 0) {
cout << -1 << '\n';
continue;
}
vector<pair<int, long long>> vec = {{u, 0}};
while (diff) {
int lsb = diff & -diff;
int ind = mp[lsb];
diff -= lsb;
vector<pair<int, long long>> nvec;
int org = vec.back().first;
org -= org % k;
for (int i = 0; i < k; ++i) {
int nnode = org + lsb * k + i;
if (nnode >= n) {
break;
}
nvec.push_back({org + lsb * k + i, INF});
}
for (auto [x, y] : vec) {
for (int j = 0; j < k; ++j) {
nvec[j].second = min(nvec[j].second, y + jump[ind][x][j]);
}
}
vec = nvec;
}
bool f = false;
for (auto [i, j] : vec) {
if (i == v && j < INF) {
cout << j << '\n';
f = true;
break;
}
}
if (!f) {
cout << -1 << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
49756 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
1368 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
71 ms |
49744 KB |
Output is correct |
9 |
Correct |
72 ms |
49752 KB |
Output is correct |
10 |
Correct |
68 ms |
49788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
50244 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
412 KB |
Output is correct |
7 |
Correct |
5 ms |
1368 KB |
Output is correct |
8 |
Correct |
8 ms |
1368 KB |
Output is correct |
9 |
Correct |
59 ms |
50000 KB |
Output is correct |
10 |
Correct |
88 ms |
50876 KB |
Output is correct |
11 |
Correct |
76 ms |
50236 KB |
Output is correct |
12 |
Correct |
72 ms |
49908 KB |
Output is correct |
13 |
Correct |
68 ms |
40196 KB |
Output is correct |
14 |
Correct |
49 ms |
30916 KB |
Output is correct |
15 |
Correct |
53 ms |
38796 KB |
Output is correct |
16 |
Correct |
53 ms |
38804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
2 ms |
1880 KB |
Output is correct |
9 |
Correct |
2 ms |
1624 KB |
Output is correct |
10 |
Correct |
54 ms |
49744 KB |
Output is correct |
11 |
Correct |
65 ms |
49984 KB |
Output is correct |
12 |
Correct |
75 ms |
50748 KB |
Output is correct |
13 |
Correct |
83 ms |
50872 KB |
Output is correct |
14 |
Correct |
73 ms |
50360 KB |
Output is correct |
15 |
Correct |
51 ms |
38984 KB |
Output is correct |
16 |
Correct |
53 ms |
38820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
2 ms |
1880 KB |
Output is correct |
9 |
Correct |
2 ms |
1624 KB |
Output is correct |
10 |
Correct |
54 ms |
49744 KB |
Output is correct |
11 |
Correct |
65 ms |
49984 KB |
Output is correct |
12 |
Correct |
75 ms |
50748 KB |
Output is correct |
13 |
Correct |
83 ms |
50872 KB |
Output is correct |
14 |
Correct |
73 ms |
50360 KB |
Output is correct |
15 |
Correct |
51 ms |
38984 KB |
Output is correct |
16 |
Correct |
53 ms |
38820 KB |
Output is correct |
17 |
Correct |
77 ms |
50040 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
3 ms |
1372 KB |
Output is correct |
24 |
Correct |
3 ms |
1368 KB |
Output is correct |
25 |
Correct |
4 ms |
1624 KB |
Output is correct |
26 |
Correct |
4 ms |
1624 KB |
Output is correct |
27 |
Correct |
59 ms |
49936 KB |
Output is correct |
28 |
Correct |
81 ms |
50800 KB |
Output is correct |
29 |
Correct |
83 ms |
50872 KB |
Output is correct |
30 |
Correct |
84 ms |
50416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
49756 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
1368 KB |
Output is correct |
6 |
Correct |
2 ms |
1372 KB |
Output is correct |
7 |
Correct |
2 ms |
1368 KB |
Output is correct |
8 |
Correct |
71 ms |
49744 KB |
Output is correct |
9 |
Correct |
72 ms |
49752 KB |
Output is correct |
10 |
Correct |
68 ms |
49788 KB |
Output is correct |
11 |
Correct |
71 ms |
50244 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
412 KB |
Output is correct |
17 |
Correct |
5 ms |
1368 KB |
Output is correct |
18 |
Correct |
8 ms |
1368 KB |
Output is correct |
19 |
Correct |
59 ms |
50000 KB |
Output is correct |
20 |
Correct |
88 ms |
50876 KB |
Output is correct |
21 |
Correct |
76 ms |
50236 KB |
Output is correct |
22 |
Correct |
72 ms |
49908 KB |
Output is correct |
23 |
Correct |
68 ms |
40196 KB |
Output is correct |
24 |
Correct |
49 ms |
30916 KB |
Output is correct |
25 |
Correct |
53 ms |
38796 KB |
Output is correct |
26 |
Correct |
53 ms |
38804 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
2 ms |
1372 KB |
Output is correct |
33 |
Correct |
2 ms |
1368 KB |
Output is correct |
34 |
Correct |
2 ms |
1880 KB |
Output is correct |
35 |
Correct |
2 ms |
1624 KB |
Output is correct |
36 |
Correct |
54 ms |
49744 KB |
Output is correct |
37 |
Correct |
65 ms |
49984 KB |
Output is correct |
38 |
Correct |
75 ms |
50748 KB |
Output is correct |
39 |
Correct |
83 ms |
50872 KB |
Output is correct |
40 |
Correct |
73 ms |
50360 KB |
Output is correct |
41 |
Correct |
51 ms |
38984 KB |
Output is correct |
42 |
Correct |
53 ms |
38820 KB |
Output is correct |
43 |
Correct |
77 ms |
50040 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
344 KB |
Output is correct |
46 |
Correct |
0 ms |
600 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
3 ms |
1372 KB |
Output is correct |
50 |
Correct |
3 ms |
1368 KB |
Output is correct |
51 |
Correct |
4 ms |
1624 KB |
Output is correct |
52 |
Correct |
4 ms |
1624 KB |
Output is correct |
53 |
Correct |
59 ms |
49936 KB |
Output is correct |
54 |
Correct |
81 ms |
50800 KB |
Output is correct |
55 |
Correct |
83 ms |
50872 KB |
Output is correct |
56 |
Correct |
84 ms |
50416 KB |
Output is correct |
57 |
Correct |
132 ms |
65896 KB |
Output is correct |
58 |
Correct |
79 ms |
49636 KB |
Output is correct |
59 |
Correct |
84 ms |
50176 KB |
Output is correct |