問題一覧 > 通常問題

No.3263 Illegal Walking Path

レベル : / AI推定難易度 : 1025 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (解説公開後に AC するまで非表示) / 解いたユーザー数 179
作問者 : bolero / テスター : のらら ゆにたりー卿 elphe DeltaStruct Rice_tawara459 Andrew8128 kazuppa よには yimiya(いみや)
お気に入りにしたユーザー ProblemId : 12599 / 自分の提出
問題文最終更新日: 2025-09-06 11:58:38
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
第2回 岩井星人アンソロジープログラミングコンテスト Div.2 (順位表) の他の問題:

Story

Takahashi, an Earthling, had a sudden idea:
"I wanna gather 5 Iwai aliens and fuse them to create the strongest Iwai alien, the Giant Boulder alien!"
So he came to Planet Iwai as a tourist.
However, on Planet Iwai, fusing Iwai aliens is prohibited as an illegal act.
Disappointed, Takahashi was taking a walk in a park on Planet Iwai, but if he encounters Iwai aliens 5 times in a row, he will be unable to restrain his desire and will turn the Iwai aliens he met into a Giant Boulder alien.
Let's tell Takahashi whether there exists a walking path on which he will not commit the illegal act!

Problem Statement

Takahashi, an Earthling, is planning to take a walk in a park on Planet Iwai.
The park is represented by a simple connected undirected graph with $N$ vertices and $M$ edges.
The vertices are numbered vertex $1$, vertex $2$, $\dotsc$ , vertex $N$, and the $i\ (1 ≤ i ≤ M)$-th edge connects vertex $U_i$ and vertex $V_i$.
Takahashi can move in both directions between vertex $U_i$ and vertex $V_i$ through the $i$-th edge.
There are Iwai aliens at the $K$ vertices $A_1,\ A_2,\ \dotsc ,\ A_K$, and the Iwai aliens do not move between vertices.

Takahashi moves from vertex $1$ to vertex $N$.
If Takahashi encounters Iwai aliens $5$ times in a row while moving, he becomes unable to restrain his desire, so he cannot take such a route.
Specifically, when a route from vertex $1$ to vertex $N$ is represented as a vertex sequence $(v_0,\ v_1,\ \dotsc ,v_t)$, if there exists a contiguous subsequence $(v_i,\ v_{i+1},\ v_{i+2},\ v_{i+3},\ v_{i+4})$ of length $5$ consisting only of vertices with Iwai aliens, he cannot take that route.

Determine whether there exists a route on which Takahashi can move from vertex $1$ to vertex $N$.
If it exists, output the shortest distance of such a route; otherwise, output $-1$.

Constraints

  • $3 \leq N \leq 10^5$
  • $N - 1 \leq M \leq \min\left(\frac{N(N-1)}{2},\ 10^5 \right)$
  • $1 \leq U_i < V_i \leq N $
  • The given graph is simple and connected.
  • $0 \leq K \leq N - 2$
  • $2 \leq A_1 < A_2 < \dotsc < A_K \leq N - 1$
  • All values given in the input are integers.

Input

The input is given from Standard Input in the following format.

$
N\ M \\
U_{1}\ V_{1} \\
U_{2}\ V_{2} \\
\vdots \\
U_{M} \ V_{M} \\
K \\
A_1 \ A_2 \  \dotsc \  A_K \\
$

Output

If there exists a route on which Takahashi can move from vertex $1$ to vertex $N$, output the shortest distance of such a route; otherwise, output $-1$.

Sample

Sample 1
Input
13 13
1 2
2 3
3 4
4 5
5 6
6 13
1 7
7 8
8 9
9 10
9 12
10 11
11 13
10
2 3 4 5 6 7 8 9 10 11
Output
8

A route such as $(1, 2, 3, 4, 5, 6, 13)$ cannot be taken, because he would encounter Iwai aliens $5$ times in a row.
A route such as $(1, 7, 8, 9, 12, 9, 10, 11, 13)$ does not make him encounter Iwai aliens $5$ times in a row, so he can reach vertex $N$ by it.
This route is the shortest among the routes on which he can move from vertex $1$ to vertex $N$, so the answer is $8$.

Sample 2
Input
7 6
1 2
2 3
3 4
4 5
5 6
6 7
5
2 3 4 5 6
Output
-1

No matter how he moves, Takahashi cannot restrain his desire.

Sample 3
Input
3 2
1 2
2 3
0
Output
2

There may be no Iwai aliens in the park at all.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。