#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
size_t n, m, o, k;
cin >> k >> n >> m >> o;
vector<vector<pair<unsigned, unsigned>>> g(n);
for (size_t i = 0; i < m; i++)
{
unsigned u, v, t;
cin >> u >> v >> t;
g[u].emplace_back(v, t);
}
vector<vector<vector<unsigned>>> dp(n, vector<vector<unsigned>>(1, vector<unsigned>(k, UINT_MAX)));
for (size_t u = 0; u < n; u++)
for (auto const &[v, w] : g[u])
dp[u][0][v % k] = w;
for (size_t j = 1; j <= n / k; j <<= 1)
for (size_t u = 0; u < n; u++)
{
if ((u / k + j) * k >= n)
continue;
dp[u].emplace_back(k, UINT_MAX);
for (size_t h = 0; h < k; h++)
for (size_t i = 0; i < k && (u / k + j) * k + i < n; i++)
{
if (dp[u][dp[u].size() - 2][i] != UINT_MAX &&
dp[u].size() - 2 < dp[(u / k + j) * k + i].size() &&
dp[(u / k + j) * k + i][dp[u].size() - 2][h] != UINT_MAX)
dp[u].back()[h] =
min(dp[u].back()[h],
dp[u][dp[u].size() - 2][i] + dp[(u / k + j) * k + i][dp[u].size() - 2][h]);
}
}
for (size_t i = 0; i < o; i++)
{
unsigned u, v;
cin >> u >> v;
if (u / k >= v / k)
{
cout << -1 << '\n';
continue;
}
size_t z = 0, h = v / k - u / k;
vector<unsigned> d(k, UINT_MAX);
d[u % k] = 0;
while (h)
{
if (h & 1)
{
vector<unsigned> e(k, UINT_MAX);
for (size_t j = 0; j < k; j++)
if (d[j] != UINT_MAX)
for (size_t l = 0; l < k; l++)
if (z < dp[(u / k) * k + j].size() &&
dp[(u / k) * k + j][z][l] != UINT_MAX)
e[l] = min(e[l], d[j] + dp[(u / k) * k + j][z][l]);
u = (u / k + (1 << z)) * k + u % k;
swap(d, e);
}
z++;
h >>= 1;
}
if (d[v % k] == UINT_MAX)
cout << -1 << '\n';
else
cout << d[v % k] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
58456 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
156 ms |
58324 KB |
Output is correct |
9 |
Correct |
142 ms |
58408 KB |
Output is correct |
10 |
Correct |
108 ms |
56732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
47668 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 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 |
7 ms |
1204 KB |
Output is correct |
8 |
Correct |
6 ms |
1104 KB |
Output is correct |
9 |
Correct |
127 ms |
59256 KB |
Output is correct |
10 |
Correct |
202 ms |
48968 KB |
Output is correct |
11 |
Correct |
155 ms |
49316 KB |
Output is correct |
12 |
Correct |
142 ms |
47704 KB |
Output is correct |
13 |
Correct |
154 ms |
29876 KB |
Output is correct |
14 |
Correct |
98 ms |
29388 KB |
Output is correct |
15 |
Correct |
121 ms |
28156 KB |
Output is correct |
16 |
Correct |
138 ms |
28100 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
1108 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
4 ms |
1112 KB |
Output is correct |
9 |
Correct |
3 ms |
980 KB |
Output is correct |
10 |
Correct |
124 ms |
59224 KB |
Output is correct |
11 |
Correct |
139 ms |
49164 KB |
Output is correct |
12 |
Correct |
173 ms |
48864 KB |
Output is correct |
13 |
Correct |
180 ms |
49320 KB |
Output is correct |
14 |
Correct |
155 ms |
48488 KB |
Output is correct |
15 |
Correct |
122 ms |
28000 KB |
Output is correct |
16 |
Correct |
114 ms |
28112 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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
1108 KB |
Output is correct |
7 |
Correct |
2 ms |
980 KB |
Output is correct |
8 |
Correct |
4 ms |
1112 KB |
Output is correct |
9 |
Correct |
3 ms |
980 KB |
Output is correct |
10 |
Correct |
124 ms |
59224 KB |
Output is correct |
11 |
Correct |
139 ms |
49164 KB |
Output is correct |
12 |
Correct |
173 ms |
48864 KB |
Output is correct |
13 |
Correct |
180 ms |
49320 KB |
Output is correct |
14 |
Correct |
155 ms |
48488 KB |
Output is correct |
15 |
Correct |
122 ms |
28000 KB |
Output is correct |
16 |
Correct |
114 ms |
28112 KB |
Output is correct |
17 |
Correct |
141 ms |
49132 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
320 KB |
Output is correct |
23 |
Correct |
4 ms |
1108 KB |
Output is correct |
24 |
Correct |
4 ms |
1096 KB |
Output is correct |
25 |
Correct |
5 ms |
1072 KB |
Output is correct |
26 |
Correct |
5 ms |
1108 KB |
Output is correct |
27 |
Correct |
146 ms |
59160 KB |
Output is correct |
28 |
Correct |
180 ms |
48904 KB |
Output is correct |
29 |
Correct |
190 ms |
49208 KB |
Output is correct |
30 |
Correct |
172 ms |
48572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
58456 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
2 ms |
1108 KB |
Output is correct |
8 |
Correct |
156 ms |
58324 KB |
Output is correct |
9 |
Correct |
142 ms |
58408 KB |
Output is correct |
10 |
Correct |
108 ms |
56732 KB |
Output is correct |
11 |
Correct |
165 ms |
47668 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 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 |
7 ms |
1204 KB |
Output is correct |
18 |
Correct |
6 ms |
1104 KB |
Output is correct |
19 |
Correct |
127 ms |
59256 KB |
Output is correct |
20 |
Correct |
202 ms |
48968 KB |
Output is correct |
21 |
Correct |
155 ms |
49316 KB |
Output is correct |
22 |
Correct |
142 ms |
47704 KB |
Output is correct |
23 |
Correct |
154 ms |
29876 KB |
Output is correct |
24 |
Correct |
98 ms |
29388 KB |
Output is correct |
25 |
Correct |
121 ms |
28156 KB |
Output is correct |
26 |
Correct |
138 ms |
28100 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
2 ms |
980 KB |
Output is correct |
34 |
Correct |
4 ms |
1112 KB |
Output is correct |
35 |
Correct |
3 ms |
980 KB |
Output is correct |
36 |
Correct |
124 ms |
59224 KB |
Output is correct |
37 |
Correct |
139 ms |
49164 KB |
Output is correct |
38 |
Correct |
173 ms |
48864 KB |
Output is correct |
39 |
Correct |
180 ms |
49320 KB |
Output is correct |
40 |
Correct |
155 ms |
48488 KB |
Output is correct |
41 |
Correct |
122 ms |
28000 KB |
Output is correct |
42 |
Correct |
114 ms |
28112 KB |
Output is correct |
43 |
Correct |
141 ms |
49132 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
320 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
320 KB |
Output is correct |
49 |
Correct |
4 ms |
1108 KB |
Output is correct |
50 |
Correct |
4 ms |
1096 KB |
Output is correct |
51 |
Correct |
5 ms |
1072 KB |
Output is correct |
52 |
Correct |
5 ms |
1108 KB |
Output is correct |
53 |
Correct |
146 ms |
59160 KB |
Output is correct |
54 |
Correct |
180 ms |
48904 KB |
Output is correct |
55 |
Correct |
190 ms |
49208 KB |
Output is correct |
56 |
Correct |
172 ms |
48572 KB |
Output is correct |
57 |
Correct |
222 ms |
48636 KB |
Output is correct |
58 |
Correct |
142 ms |
59308 KB |
Output is correct |
59 |
Correct |
184 ms |
49376 KB |
Output is correct |