You are given a list of edges in a graph containing ‘N’ nodes. An edge is made up of two parts, a normal link, and a special link. Both links have their respective lengths. You can use either of the links to travel between two edges. Your task is to determine the shortest distance between the two given nodes such that at most one(possibly, zero) special link is included in this path.
Note:
1. All the edges are directed.
2. Multiple edges can be present between two nodes.
3. The given graph is a single connected component.
4. It does not have any self-loop.
5. At least one path always exists between the two given nodes.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the 'T' test cases follow.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’, denoting the number of nodes present in the graph and the number of edges present in the graph respectively. Then, ‘M’ lines follow.
Each line contains four space-separated integers, ‘A’, ‘B’, ‘W1’, ‘W2’, denoting that there is a normal link between ‘A’ and ‘B’ having a length ‘D1’ and a special link having a length ‘D2’.
The last line of each test case contains two space-separated integers, ‘X’ and ‘Y’, denoting the nodes between which the shortest distance has to be calculated.
Output Format:
For each test case, return an integer denoting the shortest between the two given nodes.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N < 10^3
N-1 <= M <= 10^3
1 <= A,B,X,Y <= N
1 <= D1,D2 <= 10^6
Time Limit: 1sec
1
3 2
1 2 2 3
2 3 4 2
1 3
4

In the diagram, we can observe that there is no direct edge between node 1 and node 3. But we can go from node 1 to node 2 using the normal link of length 2 and then from node 2 to node 3 using the special link of length 2. So the total length will be 4 and we can clearly see that no other path can be smaller than this.
1
3 3
1 2 3 2
2 3 4 2
1 3 7 8
1 3
5

In the diagram, we can observe that there is a direct edge between node 1 and node 3. But we can observe that the path that directly connects node 1 to node 3 is longer than the path that goes from node 1 to node 2 and then from node 2 to node 3. Although lengths of both the special links are smaller than their corresponding normal links, we can choose at most 1 special link. So we go from node 1 to node 2 using a normal link having a length of 3 and then from node 2 to node 3 using a special link having length 2. So, the total length of this path becomes 5 which is the least among all other possible paths.
Think about the Djikstra's Algorithm
Approach:
The approach is to apply Dijkstra’s Algorithm after making some modifications according to our requirements. We can make a struct that has three values, the node to which the current node is directed, and the respective distances of both types of edges. We also need a VISITED array to keep track of nodes that have already been VISITED.
Steps:
O(N + M * log N), where ‘N’ is the number of nodes present in the graph and ‘M’ is the number of edges present in the graph.
Dijkstra's algorithm visits every node once (O(N)). Therefore it iterates over each edge in (O(M)), each time accessing the priority queue up to two times in O(logN).
So, the overall complexity becomes O(N + M * log N).
O(N), where ‘N’ is the number of nodes present in the graph.
We need extra linear space to maintain a boolean array to keep track of nodes that have already been visited. We also need extra space to maintain the priority queue.