atcoder#ABC277E. [ABC277E] Crystal Switches

[ABC277E] Crystal Switches

Score : 500500 points

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.

5 5 2
1 3 0
2 3 1
5 4 1
2 1 1
1 4 0
3 4
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.

4 4 2
4 3 0
1 2 1
1 2 0
2 1 1
2 4
-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.