Input: N = 4, M = 2
mat[][]: { {0, 2, 1, 4}, {1, 0, 4, 1}, {3, 1, 0, 3}, {1, 1, 1, 0} }
S[] = {1, 0}, I[] = {2, 2}, D[] = {3, 1}
Output:
4 3
2 0
Explanation:
Query 1:
- Source, Intermediate and Destination city are S1 = 1, I1 = 2, D1 = 3 respectively.
- Travelling source to destination (1 ->3) city using intermediate (2) city:
- We can use cities as: 1 -> 0 -> 2 -> 1 -> 3, total time taken will be M[1][0] + M[0][2] + M[2][1] + M[1][3] = 1+1+1+1 = 4. It can be verified that it is the minimum possible time to use an intermediate city for travelling sources to the destination city.
- Travelling source to destination (1 ->3) city directly:
- The most optimal path will be: 1 -> 3, Total time taken will be = M[1][3] = 1.
- So that the time taken from source to destination city using intermediate city is: 4, while the time that can be saved if travelled directly from source to destination is: 4-1=3 Therefore, the output is 4 3.
The time that will take with the intermediate and time that can be saved if not travelled from the intermediate city are 4, and 3 respectively. Therefore, the output is 4 3.
Query 2:
- Source, Intermediate and Destination city are S2=0, I2=2, D2=1 respectively
- Travelling source to destination (0 ->1) city using intermediate (2) city:
- We can use cities as: 0 -> 2 -> 1, Total time taken will be M[0][2] + M[2][1] = 1+1 = 2. It can be verified that it is the minimum possible time using intermediate city for travelling source to destination city.
- Travelling source to destination (0 -> 1) city directly:
- The most optimal path will be: 0 -> 1, Total time taken will be = M[0][1] = 2.
- So that the time taken from source to destination city using intermediate city is: 2, while the time that can be saved if travelled directly from source to destination is: 2-2 = 0. Therefore, the output is 2 0.
Input: N = 4, M = 3
mat[][] = {0, 2, 4}, {1, 0, 7}, {3, 2, 0} }
S[] = {0, 2, 1}, I[] = {1, 1, 2}, D[] = {3, 2, 0}
Output:
7 3
3 0
7 7