Reputation: 41
Given a graph in which each node represents a city. Some cities are connected to each other through bidirectional roads. The length of each road is also given. Some of the cities have hotels. Given a start city and a destination city and a value K which represents the maximum distance that can be travelled by a person in one day, find the minimum number of days in which the person can reach his destination (or tell if it is impossible for the given K). (Note : If the distance travelled in one day is exceeding K, the person can rest in the city which has a hotel in it, if there is no hotel in that city this implies you have to choose another path. Next day, the person can start from that city and the distance travelled get reset to 0).
Upvotes: 2
Views: 660
Reputation: 2777
This can be done with Dijkstra:
{daysPassed, timeTravelled, vertexId}
{0,0,startCity}
we add it to priority queue (sorted as tuples, min first)timeTravelled + edgeWeigth <= maxTravelTimePerDay
), state changes to {daysPassed, timeTravelled + edgeWeight, neighbourId}
{daysPassed + 1, 0, vertexId}
daysPassed
from state is the answerUpvotes: 1