Bellman Ford Algorithm
Given a directed weighted graph with n vertices numbered 0 to n - 1, a list of directed edges, and a source vertex, compute the shortest distance from source to every vertex.
Each edge is a triple [u, v, w] meaning a directed edge from u to v with weight w. Weights may be negative, and the same pair of vertices may appear in several edges with different weights. A self loop u == v is also a valid edge, so a single negative self loop is enough to create a negative cycle.
Return an array of length n where index i holds the minimum total weight of any path from source to i. Vertices that cannot be reached from source are reported as -1, so a reachable vertex whose shortest distance is exactly -1 reports the same value; the distance itself is what is graded.
If a cycle that is reachable from source has a negative total weight, then distances along that cycle can be lowered without bound. In that case the answer is not a finite array, so return an empty array. A negative cycle that is not reachable from source does not affect the answer. Reachability is purely directional: a cycle that is only reachable by ignoring edge direction is still unreachable.
Bellman Ford Algorithm
Given a directed weighted graph with `n` vertices numbered `0` to `n - 1`, a list of directed edges, and a `source` vertex, compute the shortest distance from `source` to every vertex.