Refactored shortest path method.
Lcs works for weighted graphs.