2021-06-30

Minimum time to reach from city 1 to city N

There are a lot of graph problems that require some modification of the BFS algorithm. I just come across this problem. I thought of this question with just an extension of the standard BFS algorithm. The question states that:

We are given a country, having N cities and M bidirectional roads. Each city has a traffic light, showing only 2 colors i.e Green and Red. All the traffic lights switch their color from Green to Red or vice versa after every T seconds. We can cross a city only when the traffic light is green. Initially, all traffic light is green. In any city, if the traffic light is Red then we have to wait for its light to turn green. Time taken to travel any road is C. We have to find minimum time to reach City N from 1.

Note: graph doesn't contain a self-loop or multiple edges.

For example:

N=5,M=5,T=3,C=5

Edges are:

1 2,

1 3,

2 4,

1 4,

2 5.

Here minimum time to go from 1 to 5 is 11 through path 1 2 5.WE can reach city 2 in 5 secs. then wait for 1 second for the light to turn green and then 5 seconds to go to 5.

Can anyone share his approach toward this problem? whether it is a BFS problem or some other graph algorithm required too? Better to unsderstand if psedoucode will be there along with algorithm?



from Recent Questions - Stack Overflow https://ift.tt/3jnCQUJ
https://ift.tt/eA8V8J

No comments:

Post a Comment