custom shortest path implementation with upper limit on distance