-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathabc073-d.rs
50 lines (41 loc) · 1.27 KB
/
abc073-d.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// https://atcoder.jp/contests/abc073/tasks/abc073_d
use itertools::Itertools as _;
use num::traits::One;
use petgraph::graph::{IndexType, NodeIndex, UnGraph};
use proconio::input;
use proconio::source::{Readable, Source};
use std::collections::HashMap;
use std::io::BufRead;
use std::marker::PhantomData;
use std::ops::Sub;
fn main() {
input! {
_: usize,
m: usize,
r: usize,
rs: [NodeIndex1<u32>; r],
abcs: [(NodeIndex1<u32>, NodeIndex1<u32>, u32); m],
}
let graph = UnGraph::<(), u32>::from_edges(abcs);
let dijkstra = rs
.iter()
.map(|&r| {
let dijkstra = petgraph::algo::dijkstra(&graph, r, None, |e| *e.weight());
(r, dijkstra)
})
.collect::<HashMap<_, _>>();
let ans = rs
.into_iter()
.permutations(r)
.map(|rs| rs.windows(2).map(|w| dijkstra[&w[0]][&w[1]]).sum::<u32>())
.min()
.unwrap();
println!("{}", ans);
}
struct NodeIndex1<Ix>(PhantomData<fn() -> Ix>);
impl<Ix: IndexType + Readable<Output = Ix> + One + Sub<Output = Ix>> Readable for NodeIndex1<Ix> {
type Output = NodeIndex<Ix>;
fn read<R: BufRead, S: Source<R>>(source: &mut S) -> NodeIndex<Ix> {
NodeIndex::from(Ix::read(source) - Ix::one())
}
}