# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1228382 | Jer | Railway Trip 2 (JOI22_ho_t4) | C++20 | 30 ms | 840 KiB |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 305;
int dist[MAXN][MAXN];
int n, k, m, q;
int main()
{
scanf("%d%d%d", &n, &k, &m);
int a, b;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dist[i][j] = (i == j) ? 0 : INT_MAX;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &a, &b);
a--, b--;
if (a < b)
{
for (int s = a; s <= min(a + k - 1, b - 1); s++)
for (int j = s + 1; j <= b; j++)
dist[s][j] = 1;
}
else
{
for (int s = a; s >= max(a - k + 1, b + 1); s--)
for (int j = s - 1; j >= b; j--)
dist[s][j] = 1;
}
}
for (int x = 0; x < n; x++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (dist[i][x] != INT_MAX and dist[x][j] != INT_MAX and dist[i][x] + dist[x][j] < dist[i][j])
dist[i][j] = dist[i][x] + dist[x][j];
scanf("%d", &q);
while (q--)
{
int s, t;
scanf("%d%d", &s, &t);
s--, t--;
printf("%d\n", (dist[s][t] == INT_MAX) ? -1 : dist[s][t]);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |