Code
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e7;
void solve() {
int n, m, k;
cin >> n >> m >> k;
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
u--;
v--;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
vector<bool> has_switch(n, false);
for (int i = 0; i < k; i++) {
int index;
cin >> index;
index--;
has_switch[index] = true;
}
// Each vertex in state graph is identified by (vertex, flip_cnt%2)
vector<vector<int>> level(n, vector<int>(2, inf));
queue<pair<int, int>> bfs;
level[0][0] = 0;
bfs.push(make_pair(0, 0));
if (has_switch[0]) {
level[0][1] = 0;
bfs.push(make_pair(0, 1));
}
while (!bfs.empty()) {
int vertex_now = bfs.front().first;
int flip_cnt = bfs.front().second;
int dist_now = level[vertex_now][flip_cnt];
bfs.pop();
for (auto child : adj[vertex_now]) {
// Don't use the switch. In that case, an edge would only be
// iterable if the the xor of flip_cnt and edge weight is 1.
if (child.second ^ flip_cnt == 1) {
int nxt = child.first;
// Don't use the switch.
if (level[nxt][flip_cnt] == inf) {
level[nxt][flip_cnt] = dist_now + 1;
bfs.push(make_pair(nxt, flip_cnt));
}
}
// Now, use the switch at this vertex if possible.
if (child.second ^ flip_cnt == 0) {
int nxt = child.first;
// Use the switch.
if (has_switch[vertex_now] && level[nxt][flip_cnt ^ 1] == inf) {
level[nxt][flip_cnt ^ 1] = dist_now + 1;
bfs.push(make_pair(nxt, flip_cnt ^ 1));
}
}
}
}
int res = min(level[n - 1][0], level[n - 1][1]);
if (res >= inf) {
cout << -1 << endl;
} else {
cout << res << endl;
}
}
int main() {
solve();
return 0;
}