-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdial.rs
28 lines (28 loc) · 828 Bytes
/
dial.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
// 未Verify
pub fn shortest_path_dial(graph: &Vec<Vec<(usize, usize)>>, start: usize) -> Vec<Option<usize>> {
let cost_limit = graph.iter().filter_map(|edges| edges.iter().map(|edge| edge.1).max()).max().unwrap() + 1;
let mut dist = vec![None; graph.len()];
dist[start] = Some(0);
let mut stacks = vec![vec![]; cost_limit];
stacks[0].push((start, 0));
let mut i = 0;
let mut count = 1;
while count != 0 {
while let Some((u, d)) = stacks[i].pop() {
count -= 1;
if dist[u].unwrap() != d {
continue;
}
for &e in &graph[u] {
if dist[e.0].map(|d2| d2 <= d + e.1).unwrap_or(false) {
continue;
}
dist[e.0] = Some(d + e.1);
stacks[(i + e.1) % cost_limit].push(e);
count += 1;
}
}
i = (i + 1) % cost_limit;
}
dist
}