fn main() { let stdin = std::io::read_to_string(std::io::stdin().lock()).unwrap(); let mut stdin = stdin.split_ascii_whitespace(); let n: usize = stdin.next().unwrap().parse().unwrap(); let m: usize = stdin.next().unwrap().parse().unwrap(); let k: usize = stdin.next().unwrap().parse().unwrap(); let roads: Vec<(u16, u16, u32)> = (0..m) .map(|_| { ( stdin.next().unwrap().parse().unwrap(), stdin.next().unwrap().parse().unwrap(), stdin.next().unwrap().parse().unwrap(), ) }) .collect(); let d: Vec = (0..k) .map(|_| stdin.next().unwrap().parse().unwrap()) .collect(); use std::io::Write; std::io::stdout() .lock() .write_all(output(solve(n, roads, d)).as_bytes()) .unwrap(); } fn prepare(n: usize, roads: Vec<(u16, u16, u32)>) -> Vec> { let mut next_of = vec![Vec::new(); n + 1]; roads.into_iter().for_each(|(a, b, c)| { next_of[a as usize].push((c, b as u32)); next_of[b as usize].push((c, a as u32)); }); next_of } fn solve(n: usize, roads: Vec<(u16, u16, u32)>, d: Vec) -> Vec { let next_of = prepare(n, roads); let mut dp_cur = vec![false; n + 1]; let mut dp_next = vec![true; n + 1]; d.into_iter().for_each(|d| { std::mem::swap(&mut dp_cur, &mut dp_next); dp_next.fill(false); dp_cur .iter() .enumerate() .filter(|&(_, &b)| b) .for_each(|(i, _)| { next_of[i] .iter() .filter(|&&(cost, _)| cost == d) .for_each(|&(_, next)| { dp_next[next as usize] = true; }) }) }); dp_next .into_iter() .enumerate() .filter(|&(_, b)| b) .map(|(i, _)| i as u8) .collect() } fn output(ans: Vec) -> String { ans.len().to_string() + "\n" + &ans .into_iter() .map(|x| x.to_string()) .collect::>() .join(" ") + "\n" }