-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.fold
45 lines (41 loc) · 1.01 KB
/
Dijkstra.fold
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
let dijkstra g s e =
let n = Array.length g in
let dist = Array.make n max_int in
let q = ref PairSet.(empty |> add (0,s)) in
dist.(s) <- 0;
while not (PairSet.is_empty !q) do
let (d, u) = PairSet.min_elt !q in
q := PairSet.remove (d, u) !q;
List.iter
(fun (v, w) ->
let newdist = dist.(u) + w in
if newdist < dist.(v) then
begin
q := PairSet.add (newdist, v) !q;
dist.(v) <- newdist
end
)
g.(u)
done;
dist.(e)
type lexer =
{ line_count :: Int,
lexbuf :: LexBuf }
fun dijkstra g s e =>
let n = Array.length g
let dist = Array.make n max_int
let q = ref PairSet.(empty |> add (0, s))
dist[s] := 0;
while not (PairSet.is_empty !q) do
let (d, u) = PairSet.min_elt !q;
q := PairSet.remove (d, u) !q;
List.iter
((v, w) ->
let newdist = dist[u] + w;
if newdist < dist[v] then
q := PairSet.add (newdist, v) !q;
dist[v] := newdist
end)
g[u]
end;
dist[e]