let n = 5

let edges = [|
  [(1, 4); (2, 1)];
  [(3, 1)];
  [(1, 2); (3, 5)];
  [(4, 3)];
  []
|]

let dijkstra src =
  let dist = Array.make n 1000000 in
  dist.(src) <- 0;
  let visited = Array.make n false in
  for _ = 0 to n - 1 do
    let u = ref (-1) in
    let best = ref 1000000 in
    for v = 0 to n - 1 do
      if (not visited.(v)) && dist.(v) < !best then begin
        best := dist.(v);
        u := v
      end
    done;
    if !u >= 0 then begin
      visited.(!u) <- true;
      List.iter (fun (v, w) ->
        if dist.(!u) + w < dist.(v) then
          dist.(v) <- dist.(!u) + w
      ) edges.(!u)
    end
  done;
  dist

;;

let d = dijkstra 0 in
d.(4)
