#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 += 1 << z;
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 |
148 ms |
58444 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 |
3 ms |
1108 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
145 ms |
58440 KB |
Output is correct |
9 |
Correct |
132 ms |
58436 KB |
Output is correct |
10 |
Correct |
106 ms |
56676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
171 ms |
47692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
58444 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 |
3 ms |
1108 KB |
Output is correct |
6 |
Correct |
2 ms |
980 KB |
Output is correct |
7 |
Correct |
3 ms |
1108 KB |
Output is correct |
8 |
Correct |
145 ms |
58440 KB |
Output is correct |
9 |
Correct |
132 ms |
58436 KB |
Output is correct |
10 |
Correct |
106 ms |
56676 KB |
Output is correct |
11 |
Incorrect |
171 ms |
47692 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |