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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::{collections::BinaryHeap, ops::Add};

pub struct Cert<V>(Vec<Option<V>>);
pub struct NoCert;

pub struct DijkstraSssp<V, W, I, C> {
    cost: Vec<Option<W>>,
    prev: C,
    index: I,
    src: V,
}

#[derive(Eq, PartialEq)]
struct RevFst<F, S>(F, S);

impl<F: Ord, S: Eq> Ord for RevFst<F, S> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.cmp(&other.0).reverse()
    }
}

impl<F: Ord, S: Eq> PartialOrd for RevFst<F, S> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<V, W, I> DijkstraSssp<V, W, I, Cert<V>>
where
    V: Eq + Clone,
    W: Add<Output = W> + Ord + Clone,
    I: Fn(&V) -> usize,
{
    pub fn new_cert<D, J>(
        src: V,
        len: usize,
        zero: W,
        index: I,
        delta: D,
    ) -> Self
    where
        D: Fn(&V) -> J,
        J: Iterator<Item = (V, W)>,
    {
        let mut cost = vec![None; len];
        let mut prev = vec![None; len];
        let mut heap = BinaryHeap::new();
        cost[index(&src)] = Some(zero.clone());
        heap.push(RevFst(zero, src.clone()));
        while let Some(RevFst(w, v)) = heap.pop() {
            if let Some(cur_w) = &cost[index(&v)] {
                if cur_w > &w {
                    continue;
                }
            }
            for (nv, dw) in delta(&v) {
                let nw = w.clone() + dw;
                let ni = index(&nv);
                match &cost[ni] {
                    Some(cur_w) if cur_w <= &nw => {}
                    _ => {
                        cost[ni] = Some(nw.clone());
                        prev[ni] = Some(v.clone());
                        heap.push(RevFst(nw, nv));
                    }
                }
            }
        }

        Self { src, cost, prev: Cert(prev), index }
    }
    pub fn path(&self, dst: &V) -> Option<std::vec::IntoIter<V>> {
        let mut i = (self.index)(dst);
        if self.prev.0[i].is_none() {
            return (&self.src == dst).then(|| vec![dst.clone()].into_iter());
        }

        let mut res = vec![dst.clone()];
        while let Some(v) = &self.prev.0[i] {
            i = (self.index)(v);
            res.push(v.clone());
        }
        res.reverse();
        Some(res.into_iter())
    }
}

impl<V, W, I> DijkstraSssp<V, W, I, NoCert>
where
    V: Eq + Clone,
    W: Add<Output = W> + Ord + Clone,
    I: Fn(&V) -> usize,
{
    pub fn new<D, J>(src: V, len: usize, zero: W, index: I, delta: D) -> Self
    where
        D: Fn(&V) -> J,
        J: Iterator<Item = (V, W)>,
    {
        let mut cost = vec![None; len];
        let mut heap = BinaryHeap::new();
        cost[index(&src)] = Some(zero.clone());
        heap.push(RevFst(zero, src.clone()));
        while let Some(RevFst(w, v)) = heap.pop() {
            if let Some(cur_w) = &cost[index(&v)] {
                if cur_w > &w {
                    continue;
                }
            }
            for (nv, dw) in delta(&v) {
                let nw = w.clone() + dw;
                let ni = index(&nv);
                match &cost[ni] {
                    Some(cur_w) if cur_w <= &nw => {}
                    _ => {
                        cost[ni] = Some(nw.clone());
                        heap.push(RevFst(nw, nv));
                    }
                }
            }
        }

        Self { src, cost, prev: NoCert, index }
    }
}

impl<V, W, I, C> DijkstraSssp<V, W, I, C>
where
    V: Eq + Clone,
    W: Add<Output = W> + Ord + Clone,
    I: Fn(&V) -> usize,
{
    pub fn cost(&self, dst: &V) -> Option<W> {
        self.cost[(self.index)(dst)].clone()
    }
}