0

I have been asked about a shortest-path problem and was asked to solve it with BFS algorithm.

Given a mountain with v pavilions and e mountain tracks. Each track has a walking time te(time for both directions are the same), and each pavilion is recognized as v1, v2...,vn. One can walk at most w hours and need to take a rest in a pavilion. The resting time is two hours and one can go on climbing after the rest. Find out the shortest climb with shortest time by designing an algorithm of O((v+e)*w2).

I know that we can transform a weighted graph by splitting the nodes in order to apply BFS on it. However, on how to applying the constraint of at most w hours is still a mystery to me. I was wondering if it could solve with BFS. Can someone explain to me? Thank you very much in advance!

  • Your stop condition should be : target riched OR time exceeds W. – c0der Apr 29 '22 at 14:17
  • @c0der One starts from v1 and the algorithm will stop when reaching vn, you can keep walking and the w hours is only the constraint that tells you to take a rest at one vertex, but don't stop until you reach vn. – james2335 May 01 '22 at 11:50
  • It might be easier to explain using DFS, although it is applicable to BFS as well: Say you explore a path p. When the length of p becomes (or about to exceed) w hours you have 2 options: If p contains a pavilion, add 2 hours (rest time) to its length. If it doesn't contain a pavilion, it is a dead end. – c0der May 03 '22 at 10:20

0 Answers0