102774: [AtCoder]ABC277 E - Crystal Switches

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:4 Solved:0

Description

Score : $500$ points

Problem Statement

You are given an undirected graph consisting of $N$ vertices and $M$ edges.
For $i = 1, 2, \ldots, M$, the $i$-th edge is an undirected edge connecting vertex $u_i$ and $v_i$ that is initially passable if $a_i = 1$ and initially impassable if $a_i = 0$. Additionally, there are switches on $K$ of the vertices: vertex $s_1$, vertex $s_2$, $\ldots$, vertex $s_K$.

Takahashi is initially on vertex $1$, 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 $N$, and if he can, print the minimum possible number of times he performs Move before reaching vertex $N$.

Constraints

  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq M \leq 2 \times 10^5$
  • $0 \leq K \leq N$
  • $1 \leq u_i, v_i \leq N$
  • $u_i \neq v_i$
  • $a_i \in \lbrace 0, 1\rbrace$
  • $1 \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:

$N$ $M$ $K$
$u_1$ $v_1$ $a_1$
$u_2$ $v_2$ $a_2$
$\vdots$
$u_M$ $v_M$ $a_M$
$s_1$ $s_2$ $\ldots$ $s_K$

Output

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


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 $N$ as follows.

  • Move from vertex $1$ to vertex $2$.
  • Move from vertex $2$ to vertex $3$.
  • Hit the switch on vertex $3$. This inverts the passability of every edge in the graph.
  • Move from vertex $3$ to vertex $1$.
  • Move from vertex $1$ to vertex $4$.
  • Hit the switch on vertex $4$. This again inverts the passability of every edge in the graph.
  • Move from vertex $4$ to vertex $5$.

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 $N$, so you should print $-1$.

Input

题意翻译

【题面翻译】 给定一张 $n$ 个点 $m$ 条边的无向图。每条边有一个权值 $w \in \{0, 1\}$。$w = 0$ 表示这条边无法通过,$w = 1$ 则可以通过。 有 $k$ 个点上面有按钮 $s_i$。 你现在位于 $1$ 号点。每次,你可以做两件事情中的一件: 1. 移动。移到相邻的一个点上,注意这条边一定是可以通行的。 2. 按开关。此时,全部路的边权取反。即:$w = 0$ 变成 $1$,$w = 1$ 变成 $0$。 请问你是否能够到达 $n$ 号点。如果可以,求出最少移动次数。 translated by @[liangbowen](https://www.luogu.com.cn/user/367488). 【输入格式】 第一行三个数 $n, m, k$。 接下来 $m$ 行,每行三个数 $u_i, v_i, w_i$表示一条连接 $u_i$ 与 $v_i$ 的边。 最后一行 $k$ 个数,表示按钮的位置。 【输出格式】 如果无法到达,输出 $-1$。否则输出最少移动次数。 【数据范围】 $2 \le n \le 2 \times 10^5$ $1 \le m \le 2 \times 10^5$ $1 \le k \le n$ 保证 $1 \le u_i, v_i \le n$,且 $u_i \ne v_i$。 保证 $1 \le s_1 < s_2 < \cdots < s_k \le n$。

Output

分数:$500$分

问题描述

你有一个包含$N$个顶点和$M$条边的无向图。
对于$i = 1, 2, \ldots, M$,第$i$条边是连接顶点$u_i$和$v_i$的边,如果$a_i = 1$则初始可通行,如果$a_i = 0$则初始不可通行。 此外,图中有$K$个顶点有开关:顶点$s_1$,顶点$s_2$,$\ldots$,顶点$s_K$。

最初,Takahashi在顶点$1$,他会重复执行以下两个动作之一,移动按开关,他可以在每次选择,多次执行。

  • 移动:选择一个与当前所在顶点相邻的顶点,移动到那个顶点。
  • 按开关:如果当前所在顶点有一个开关,按它。这将反转图中每条边的可通行性。即,可通行的边将变为不可通行,反之亦然。

确定Takahashi是否能够到达顶点$N$,如果可以,输出他到达顶点$N$前执行次数最少的次数。

约束

  • $2 \leq N \leq 2 \times 10^5$
  • $1 \leq M \leq 2 \times 10^5$
  • $0 \leq K \leq N$
  • $1 \leq u_i, v_i \leq N$
  • $u_i \neq v_i$
  • $a_i \in \lbrace 0, 1\rbrace$
  • $1 \leq s_1 \lt s_2 \lt \cdots \lt s_K \leq N$
  • 输入中的所有值都是整数。

输入

输入以标准输入给出以下格式:

$N$ $M$ $K$
$u_1$ $v_1$ $a_1$
$u_2$ $v_2$ $a_2$
$\vdots$
$u_M$ $v_M$ $a_M$
$s_1$ $s_2$ $\ldots$ $s_K$

输出

如果Takahashi不能到达顶点$N$,输出$-1$; 如果他可以到达,输出他到达顶点$N$前执行次数最少的次数。


样例输入 1

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

样例输出 1

5

Takahashi可以按照以下方式到达顶点$N$。

  • 从顶点$1$移动到顶点$2$。
  • 从顶点$2$移动到顶点$3$。
  • 在顶点$3$上按下开关。这将反转图中每条边的可通行性。
  • 从顶点$3$移动到顶点$1$。
  • 从顶点$1$移动到顶点$4$。
  • 在顶点$4$上按下开关。这再次反转图中每条边的可通行性。
  • 从顶点$4$移动到顶点$5$。

在这里,移动执行了五次,这是最少的次数。


样例输入 2

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

样例输出 2

-1

给定的图可能是不连通的或包含多条边。 在这个样例输入中,Takahashi没有到达顶点$N$的方法,所以你应该输出$-1$。

加入题单

算法标签: