using
System;
using
System.Linq;
class
Program
{
static
int
N, M, K;
static
int
[][] edges, dist;
static
int
[] selected;
static
void
BFS(
int
x,
int
s)
{
int
[] q =
new
int
[200000];
int
qh = 0, qt = 0;
q[qh] = s;
qh++;
dist[x][s] = 0;
while
(qt < qh)
{
int
xx = q[qt];
qt++;
for
(
int
i = 0; i < edges[xx].Length; i++)
{
int
y = edges[xx][i];
if
(dist[x][y] ==
int
.MaxValue)
{
dist[x][y] = dist[x][xx] + 1;
q[qh] = y;
qh++;
}
}
}
}
static
void
ShortestPathCost(
int
[] selected,
int
K)
{
BFS(0, 0);
BFS(1, N - 1);
Tuple<
int
,
int
>[] data =
new
Tuple<
int
,
int
>[K];
for
(
int
i = 0; i < K; i++)
{
data[i] = Tuple.Create(dist[0][selected[i]] - dist[1][selected[i]], selected[i]);
}
Array.Sort(data, (a, b) => a.Item1.CompareTo(b.Item1));
int
best = 0, MAX =
int
.MinValue;
foreach
(Tuple<
int
,
int
> it
in
data)
{
int
a = it.Item2;
best = Math.Max(best, MAX + dist[1][a]);
MAX = Math.Max(MAX, dist[0][a]);
}
Console.WriteLine(Math.Min(dist[0][N - 1], best + 1));
}
static
void
Main(
string
[] args)
{
N = 5;
M = 4;
K = 2;
selected =
new
int
[] { 1, 3 };
Array.Sort(selected);
edges =
new
int
[5][];
edges[0] =
new
int
[] { 1 };
edges[1] =
new
int
[] { 0, 2 };
edges[2] =
new
int
[] { 1, 3 };
edges[3] =
new
int
[] { 2, 4 };
edges[4] =
new
int
[] { 3 };
dist =
new
int
[2][];
dist[0] = Enumerable.Repeat(
int
.MaxValue, 1000005).ToArray();
dist[1] = Enumerable.Repeat(
int
.MaxValue, 1000005).ToArray();
ShortestPathCost(selected, K);
}
}