Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
31 lines (30 sloc)
1.05 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let bellman_ford (n : int) (edges : (int*int*int) list) (source : int) : int array option = | |
let adj = Array.make n [] in | |
edges |> List.iter (fun (u, v, w) -> | |
adj.(u) <- (w, v) :: adj.(u) (* Directed edge *) | |
); | |
let dist = Array.make n max_int in | |
dist.(source) <- 0; | |
for _ = 1 to n - 1 do | |
for u = 0 to n - 1 do | |
if dist.(u) <> max_int then | |
adj.(u) |> List.iter (fun (w, v) -> | |
dist.(v) <- min dist.(v) (dist.(u) + w) | |
) | |
done | |
done; | |
let has_neg_cycle = List.init n (fun x -> x) |> List.exists (fun u -> | |
dist.(u) <> max_int && adj.(u) |> List.exists (fun (w, v) -> | |
dist.(v) > dist.(u) + w | |
) | |
) in | |
if has_neg_cycle then None | |
else Some dist | |
let () = | |
let (n, m, s) = Scanf.scanf "%d %d %d\n" (fun x y z -> (x, y, z)) in | |
let edges = List.init m (fun _ -> Scanf.scanf "%d %d %d\n" (fun u v w -> (u, v, w))) in | |
match bellman_ford n edges s with | |
| Some dist -> dist |> Array.iteri (fun u d -> | |
Printf.printf "SSSP(%d, %d) = %d\n" s u d | |
) | |
| None -> Printf.printf "The graph has a negative cycle" |