Given a matrix mat[][] of size N * N, where mat[i][j] represents the time taken to reach from ithcity to jthcity. Also, given M queries in the form of three arrays S[], I[], and D[] representing source, intermediate, and destination respectively. The task is to find the time taken to go from S[i] to D[i] using I[i] city and the time that can be saved if we go using the shortest path from S[i] to D[i].
Examples:
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
Approach: Implement the idea below to solve the problem:
The problem is based on shortest path technique and can be solved by using all pair shortest path approach for all the cities.
Steps were taken to solve the problem:
- Run three nested loops for i = 0, j = 0, k = 0 to i < n, j < n, k < n respectively, and follow below mentioned steps under the scope of loop:
- Calculate time[i][j] as the minimum of mat[i][j] and (mat[i][k] + mat[k][j] )
- Run a loop from i=0 to M and do the following:
- Create three variables S, IN and D and initialize them equal to S[i], I[i] and D[i] respectively.
- Output the value of mat[S][IN] + mat[IN][D] and mat[S][IN] + mat[IN][D] – mat[S][D].
Below is the code to implement the approach:
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std;
int main() {
// Number of cities
int n = 4;
// Matrix holding time such that time[i][j]
// denotes time taken to reach from i
// to j city
int time [4][4] = { { 0, 2, 1, 4 },
{ 1, 0, 4, 1 },
{ 3, 1, 0, 3 },
{ 1, 1, 1, 0 } };
// Number of queries
int m = 2;
// Arrays containing Number of city as
// source, destination and intermediate
int source[] = { 1, 0 };
int intermediate[] = { 2, 2 };
int destination[] = { 3, 1 };
// Nested loop for all pair shortest paths
// from i city to j city having shortest
// intermediate city k
for ( int k = 0; k < n; k++) {
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
time [i][j] = min( time [i][j], time [i][k] + time [k][j]);
}
}
}
// Loop for number of queries
for ( int i = 0; i < m; i++) {
// Variables containing source,
// intermediate an destination
// cities for per query
int s = source[i];
int in = intermediate[i];
int d = destination[i];
// Time taken if source to destination
// reached with intermediate city
int withIntermediate = time [s][in] + time [in][d];
// Time taken if source to destination
// reached without intermediate city
int withoutIntermediate = withIntermediate - time [s][d];
// Printing the output
cout << withIntermediate << " " << withoutIntermediate << endl;
}
} // This code is contributed by Rahul Sharma |
// Java code to implement the approach import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Driver Code
public static void main(String[] args)
throws java.lang.Exception
{
// Number of cities
int n = 4 ;
// Matrix holding time such that time[i][j]
// denotes time taken to reach from i
// to j city
int [][] time = { { 0 , 2 , 1 , 4 },
{ 1 , 0 , 4 , 1 },
{ 3 , 1 , 0 , 3 },
{ 1 , 1 , 1 , 0 } };
// Number of queries
int m = 2 ;
// Arrays containing Number of city as
// source, destination and intermediate
int [] source = { 1 , 0 };
int [] intermediate = { 2 , 2 };
int [] destination = { 3 , 1 };
// Nested loop for all pair shortest paths
// from i city to j city having shortest
// intermediate city k
for ( int k = 0 ; k < n; k++) {
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
time[i][j] = Integer.min(
time[i][j],
time[i][k] + time[k][j]);
}
}
}
// Loop for number of queries
for ( int i = 0 ; i < m; i++) {
// Variables containing source,
// intermediate and destination
// cities for per query
int s = source[i];
int in = intermediate[i];
int d = destination[i];
// Time taken if source to destination
// reached with intermediate city
int withIntermediate
= time[s][in] + time[in][d];
// Time taken if source to destination
// reached without intermediate city
int withoutIntermediate
= withIntermediate - time[s][d];
// Printing the output
System.out.println(withIntermediate + " "
+ withoutIntermediate);
}
}
} |
import sys
import math
# Number of cities n = 4
# Matrix holding time such that time[i][j] # denotes time taken to reach from i to j city time = [[ 0 , 2 , 1 , 4 ],
[ 1 , 0 , 4 , 1 ],
[ 3 , 1 , 0 , 3 ],
[ 1 , 1 , 1 , 0 ]]
# Number of queries m = 2
# Arrays containing Number of city as # source, destination, and intermediate source = [ 1 , 0 ]
intermediate = [ 2 , 2 ]
destination = [ 3 , 1 ]
# Nested loop for all pair shortest paths # from i city to j city having shortest # intermediate city k for k in range (n):
for i in range (n):
for j in range (n):
time[i][j] = min (time[i][j],
time[i][k] + time[k][j])
# Loop for number of queries for i in range (m):
# Variables containing source,
# intermediate and destination
# cities for per query
s = source[i]
in_ = intermediate[i]
d = destination[i]
# Time taken if source to destination
# reached with intermediate city
with_intermediate = time[s][in_] + time[in_][d]
# Time taken if source to destination
# reached without intermediate city
without_intermediate = with_intermediate - time[s][d]
# Printing the output
print (with_intermediate, without_intermediate)
|
using System;
class GFG
{ // Driver Code
static void Main( string [] args)
{
// Number of cities
int n = 4;
// Matrix holding time such that time[i][j]
// denotes time taken to reach from i
// to j city
int [,] time = {{0, 2, 1, 4},
{1, 0, 4, 1},
{3, 1, 0, 3},
{1, 1, 1, 0}};
// Number of queries
int m = 2;
// Arrays containing Number of city as
// source, destination and intermediate
int [] source = {1, 0};
int [] intermediate = {2, 2};
int [] destination = {3, 1};
// Nested loop for all pair shortest paths
// from i city to j city having shortest
// intermediate city k
for ( int k = 0; k < n; k++)
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
time[i, j] = Math.Min(
time[i, j],
time[i, k] + time[k, j]);
}
}
}
// Loop for number of queries
for ( int i = 0; i < m; i++)
{
// Variables containing source,
// intermediate and destination
// cities for per query
int s = source[i];
int in = intermediate[i];
int d = destination[i];
// Time taken if source to destination
// reached with intermediate city
int withIntermediate
= time[s, in ] + time[ in , d];
// Time taken if source to destination
// reached without intermediate city
int withoutIntermediate
= withIntermediate - time[s, d];
// Printing the output
Console.WriteLine(withIntermediate + " " +
withoutIntermediate);
}
}
} |
function GFG() {
// Number of cities
var n = 4;
// Matrix holding time such that time[i][j]
// denotes time taken to reach from i
// to j city
var time = [ [0, 2, 1, 4],
[1, 0, 4, 1],
[3, 1, 0, 3],
[1, 1, 1, 0] ];
// Number of queries
var m = 2;
// Arrays containing Number of city as
// source, destination and intermediate
var source = [1, 0];
var intermediate = [2, 2];
var destination = [3, 1];
// Nested loop for all pair shortest paths
// from i city to j city having shortest
// intermediate city k
for ( var k = 0; k < n; k++) {
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < n; j++) {
time[i][j] = Math.min(time[i][j], time[i][k] + time[k][j]);
}
}
}
// Loop for number of queries
for ( var i = 0; i < m; i++) {
// Variables containing source,
// intermediate and destination
// cities for per query
var s = source[i];
var in = intermediate[i];
var d = destination[i];
// Time taken if source to destination
// reached with intermediate city
var withIntermediate = time[s][ in ] + time[ in ][d];
// Time taken if source to destination
// reached without intermediate city
var withoutIntermediate = withIntermediate - time[s][d];
// Printing the output
console.log(withIntermediate + " " + withoutIntermediate);
}
} // Call the GFG function GFG(); |
4 3 2 0
Time Complexity: O(N3)
Auxiliary Space: O(N^2), where n is the number of cities. This is because the program uses a 2D array of size n x n to store the time taken to travel between each pair of cities.
Related Articles:
- Introduction to Graph – Data Structure and Algorithm Tutorials
- Introduction to Matrix – Data Structure and Algorithm Tutorials