Only calculate shortest paths when necessary