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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! ダイクストラ
use std::{cmp::Ordering, collections::BinaryHeap};

#[derive(Debug, Clone, Eq)]
pub struct Node {
    position: usize,
    cost: i64,
    from: Option<usize>,
}
impl Node {
    #[inline]
    pub fn new(position: usize, cost: i64, from: Option<usize>) -> Self {
        Node {
            position,
            cost,
            from,
        }
    }
}
impl PartialEq for Node {
    fn eq(&self, other: &Node) -> bool {
        self.cost.eq(&other.cost)
    }
}
impl PartialOrd for Node {
    fn partial_cmp(&self, other: &Node) -> Option<Ordering> {
        Some(other.cost.cmp(&(self.cost)))
    }
}
impl Ord for Node {
    fn cmp(&self, other: &Self) -> Ordering {
        self.cost.cmp(&(other.cost))
    }
}

pub fn dijkstra(
    edge: &[Vec<(usize, i64)>],
    start: usize,
    end: usize,
    vertex: usize,
) -> Option<(i64, Vec<usize>)> {
    let mut costs = vec![None; edge.len()];
    let mut nodes = BinaryHeap::new();
    let mut previous = vec![None; vertex];
    nodes.push(Node::new(start, 0, None));

    while let Some(Node {
        position,
        cost,
        from,
    }) = nodes.pop()
    {
        if costs[position].is_some() {
            continue;
        }

        previous[position] = from;
        costs[position] = Some(cost);
        if position == end {
            return Some((cost, restore_path(end, &previous)));
        }

        edge[position]
            .iter()
            .filter(|(to, c)| costs[*to].filter(|&d| d <= cost + c).is_none())
            .for_each(|&(to, c)| {
                nodes.push(Node::new(to, cost + c, Some(position)));
            });
    }
    None
}

fn restore_path(end: usize, previous: &[Option<usize>]) -> Vec<usize> {
    let mut buff = end;
    let mut v = vec![buff];

    while let Some(i) = previous[buff] {
        buff = i;
        v.push(buff);
    }
    v.reverse();
    v
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_dijkstra() {
        let graph = vec![
            vec![(2, 10), (1, 1)],
            vec![(3, 2)],
            vec![(1, 1), (3, 3), (4, 1)],
            vec![(0, 7), (4, 2)],
            vec![],
        ];
        let l = graph.len();
        for (start, end, ans) in &[
            (0, 1, Some((1, vec![0, 1]))),
            (0, 3, Some((3, vec![0, 1, 3]))),
            (3, 0, Some((7, vec![3, 0]))),
            (0, 4, Some((5, vec![0, 1, 3, 4]))),
            (4, 0, None),
        ] {
            match dijkstra(&graph, *start, *end, l) {
                Some((a, b)) => {
                    assert_eq!(a, ans.as_ref().unwrap().0);
                    assert_eq!(b, ans.as_ref().unwrap().1);
                }
                None => assert!(ans.is_none()),
            }
        }
    }
}