Given an undirected connected graph of N nodes and M edges. Each node has a light but at a time it can be either green or red. Initially, all the node is of green color. After every T seconds, the color of light changes from green to red and vice-versa. It is possible to travel from node U to node V only if the color of node U is green. The time taken required to travel through any edges is C seconds. Find the minimum amount of time required to travel from node 1 to node N.Examples:Input: N = 5, M = 5, T = 3, C = 5 Edges[] = {{1, 2}, {1, 3}, {1, 4}, {2, 4}, {2, 5}}Output: 11Explanation: At 0sec, the color of node 1 is green, so travel from node 1 to node 2 in 5sec.Now at 5sec, the color of node 2 is red so wait 1sec to change into green.Now at 6sec, the color of node 2 is green, so travel from node 2 to node 5 in 5sec.Total time = 5(from node 1 to node 2) + 1(wait at node 2) + 5(from node 2 to node 5) = 11 sec.Approach: The given problem can be solved by using Breadth-First Search and Dijkstra Algorithm because the problem is similar to the shortest path problem. Follow the steps below to solve the problem:Initialize a queue, say Q that stores the nodes that are to be traversed and the time required to reach that node.Create a boolean array, say V that stores whether the present node is visited or not.Push node 1 and time 0 as a pair in the queue Q.Consider that there is always green light and traveling through edges requires 1 second then simply run the BFS and find the shortest time required to reach from node 1 to node N and store it in a variable, say time.Create a function findTime which finds the time if traveling through edges requires C seconds and the light color changes after T seconds by performing the following steps:Initialize a variable ans as 0 that will store the minimum time required to reach from node 1 to node N.Iterate in the range [0, time] using the variable i and perform the following steps:If (ans/T) %2 = 1, then modify the value of ans as (ans/T + 1)* T + C.Otherwise, add C to the variable ans.Print ans as the answer.Below is the implementation of the above approach: Python3 from queue import Queue def minEdge(): Q = Queue() Q.put([1, 0]) V = [False for _ in range(N + 1)] while 1: crnt, c = Q.get() if crnt == N: break for _ in X[crnt]: if not V[_]: Q.put([_, c + 1]) V[_] = True return c def findTime(c): ans = 0 for _ in range(c): if (ans//T) % 2: ans = (ans//T + 1)*T + C else: ans += C return ans if __name__ == “__main__”: N = 5 M = 5 T = 3 C = 5 X = [[], [2, 3, 4], [4, 5], [1], [1, 2], [2]] c = minEdge() ans = findTime(c) print(ans)Time Complexity: O(N + M)Space Complexity: O(N + M)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.