|
| 1 | +// https://atcoder.jp/contests/abc073/tasks/abc073_d |
| 2 | + |
| 3 | +use itertools::Itertools as _; |
| 4 | +use petgraph::graph::{NodeIndex, UnGraph}; |
| 5 | + |
| 6 | +use std::collections::HashMap; |
| 7 | +use std::io::{self, Read}; |
| 8 | + |
| 9 | +fn main() { |
| 10 | + let mut input = read_to_static(io::stdin()).split_whitespace(); |
| 11 | + macro_rules! read { |
| 12 | + ([$tt:tt]) => (read!([$tt; read!(usize)])); |
| 13 | + ([$tt:tt; $n:expr]) => ((0..$n).map(|_| read!($tt)).collect::<Vec<_>>()); |
| 14 | + (($($tt:tt),+)) => (($(read!($tt)),*)); |
| 15 | + ($ty:ty) => (input.next().unwrap().parse::<$ty>().unwrap()); |
| 16 | + ({ NodeIndex1 }) => { |
| 17 | + NodeIndex::from(read!(u32) - 1) |
| 18 | + }; |
| 19 | + } |
| 20 | + |
| 21 | + let (_, m, r) = read!((usize, usize, usize)); |
| 22 | + let rs = read!([{ NodeIndex1 }; r]); |
| 23 | + let abcs = read!([({ NodeIndex1 }, { NodeIndex1 }, u32); m]); |
| 24 | + |
| 25 | + let graph = UnGraph::<(), u32>::from_edges(abcs); |
| 26 | + |
| 27 | + let dijkstra = rs |
| 28 | + .iter() |
| 29 | + .map(|&r| { |
| 30 | + let dijkstra = petgraph::algo::dijkstra(&graph, r, None, |e| *e.weight()); |
| 31 | + (r, dijkstra) |
| 32 | + }) |
| 33 | + .collect::<HashMap<_, _>>(); |
| 34 | + |
| 35 | + let ans = rs |
| 36 | + .into_iter() |
| 37 | + .permutations(r) |
| 38 | + .map(|rs| rs.windows(2).map(|w| dijkstra[&w[0]][&w[1]]).sum::<u32>()) |
| 39 | + .min() |
| 40 | + .unwrap(); |
| 41 | + println!("{}", ans); |
| 42 | +} |
| 43 | + |
| 44 | +fn read_to_static(mut source: impl Read) -> &'static str { |
| 45 | + let mut input = "".to_owned(); |
| 46 | + source.read_to_string(&mut input).unwrap(); |
| 47 | + Box::leak(input.into_boxed_str()) |
| 48 | +} |
0 commit comments