#abc277e. E - Crystal Switches

E - Crystal Switches

Score : 500500 points

问题描述

你被给予一个由 NN 个顶点和 MM 条边构成的无向图。
对于 i=1,2,,Mi = 1, 2, \ldots, M,第 ii 条边是一个无向边,连接顶点 uiu_iviv_i,如果 ai=1a_i = 1 则初始时该边是可通行的,如果 ai=0a_i = 0 则初始时不可通行。另外,在 KK 个顶点上存在开关:顶点 s1s_1、顶点 s2s_2\ldots、顶点 sKs_K

高桥初始时位于顶点 11,他将重复执行以下两种动作之一——移动击打开关,每次可以选择任一动作,并可以重复任意次数。

  • 移动:选择当前所在顶点通过一条边相邻的一个顶点,并移动到那个顶点。
  • 击打开关:如果当前所在顶点上有开关,则击打它。这将使图中每条边的通行性翻转。也就是说,原本可通行的边会变为不可通行,反之亦然。

确定高桥是否能够到达顶点 NN,如果可以,请输出他在到达顶点 NN 前至少需要执行 移动 动作的次数。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given an undirected graph consisting of NN vertices and MM edges.
For i=1,2,,Mi = 1, 2, \ldots, M, the ii-th edge is an undirected edge connecting vertex uiu_i and viv_i that is initially passable if ai=1a_i = 1 and initially impassable if ai=0a_i = 0. Additionally, there are switches on KK of the vertices: vertex s1s_1, vertex s2s_2, \ldots, vertex sKs_K.

Takahashi is initially on vertex 11, and will repeat performing one of the two actions below, Move or Hit Switch, which he may choose each time, as many times as he wants.

  • Move: Choose a vertex adjacent to the vertex he is currently on via an edge, and move to that vertex.
  • Hit Switch: If there is a switch on the vertex he is currently on, hit it. This will invert the passability of every edge in the graph. That is, a passable edge will become impassable, and vice versa.

Determine whether Takahashi can reach vertex NN, and if he can, print the minimum possible number of times he performs Move before reaching vertex NN.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1M2×1051 \leq M \leq 2 \times 10^5
  • 0KN0 \leq K \leq N
  • 1ui,viN1 \leq u_i, v_i \leq N
  • uiviu_i \neq v_i
  • ai{0,1}a_i \in \lbrace 0, 1\rbrace
  • 1s1<s2<<sKN1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N
  • All values in the input are integers.

Input

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

NN MM KK

u1u_1 v1v_1 a1a_1

u2u_2 v2v_2 a2a_2

\vdots

uMu_M vMv_M aMa_M

s1s_1 s2s_2 \ldots sKs_K

Output

If Takahashi cannot reach vertex NN, print 1-1; if he can, print the minimum possible number of times he performs Move before reaching vertex NN.

Sample Input 1

5 5 2
1 3 0
2 3 1
5 4 1
2 1 1
1 4 0
3 4

Sample Output 1

5

Takahashi can reach vertex NN as follows.

  • Move from vertex 11 to vertex 22.
  • Move from vertex 22 to vertex 33.
  • Hit the switch on vertex 33. This inverts the passability of every edge in the graph.
  • Move from vertex 33 to vertex 11.
  • Move from vertex 11 to vertex 44.
  • Hit the switch on vertex 44. This again inverts the passability of every edge in the graph.
  • Move from vertex 44 to vertex 55.

Here, Move is performed five times, which is the minimum possible number.

Sample Input 2

4 4 2
4 3 0
1 2 1
1 2 0
2 1 1
2 4

Sample Output 2

-1

The given graph may be disconnected or contain multi-edges. In this sample input, there is no way for Takahashi to reach vertex NN, so you should print 1-1.

update @ 2024/3/10 11:40:01