Roads

Easy
0/40
Average time to solve is 15m
profile
Contributed by
5 upvotes
Asked in companies
IntuitFacebook

Problem statement

You live in the country with ‘V’ cities that have ‘E’ roads. You are in city ‘S’ with a car having ‘P’ amount of petrol in it. Roads are bidirectional and consume your petrol. Each road has a description ‘X’, ‘Y’ and ‘Z’, which means city ‘X’ and ‘Y’ have a road between them which consumes ‘Z’ amount of petrol. You want to visit the city ‘D’. Your task is to check if it's possible to visit ‘D’ from ‘S’ using ‘P’ amount of petrol.

For Example:
In the given graph, if we want to reach from node 1 to node 4 with ‘P’ = 10, then our answer is ‘Yes’ because there exists path 1 -> 3 -> 4, which takes 7 amount of petrol, which is less than the given value of ‘P’ so it is possible and hence, the answer is ‘Yes’.

undirected

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains a single integer, 'T,’ denoting the number of test cases.

The first line of each test case contains three space-separated integers, ‘V’, ‘E’, and ’P’, denoting the number of vertices, the number of edges in the graph, and the amount of petrol in the car.

The next ‘E’ lines of each test case contain three space-separated integers, ‘X’, ‘Y’ and ‘Z’, denoting the vertices which are connected with a bidirectional edge, and the weight of the edge.

The last line of each test case has two space-separated integers, ‘S’ and ’D’, representing the source and destination vertices.
Output Format:
For each test case, print “Yes” if there exists a path from vertex 'S’ to 'D’ which consumes less than or equal to the given amount of petrol ‘P’. Otherwise, print “No” (without quotes).

Print the output of each test case in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
2 <= V <= 2000
0 <= E <= (V*(V-1)) / 2
0 <= X,Y < V
1 <= Z <= 10 ^ 6
1 <= P <= 10 ^ 9
0 <= S,D < V

Time Limit: 1 sec 
Sample Input 1:
2
4 3 4
0 1 1
1 2 1
2 3 1
0 3
4 5 3
0 1 1
0 2 1
0 3 4
1 2 2
2 3 2
0 3 
Sample Output 1:
Yes
Yes
Explanation:
In test case 1:
there are 4 vertices and 3 edges, and there is a path between 0 and 3 which is 0 -> 1 -> 2 -> 3. The path consumes 3 amount of petrol, which is less than the given ‘P’. Hence, the answer is ‘Yes’.

In test case 2:
there are 4 vertices and 5 edges, and there is a path between 0 and 3 which is 0 -> 2 -> 3.  The path consumes 3 amount of petrol, which is equal to the given ‘P’. Hence, the answer is ‘Yes’.
Sample Input 2:
1
3 2 1
0 1 3
1 2 4
0 2
Sample Output 2:
No
Hint

Can you use any all-pairs Shortest Path algorithm?

Approaches (2)
Floyd Warshall Algorithm

In this approach, we will calculate the shortest path between all pairs of vertices using the values of already computed values. This algorithm is known as Floyd Warshall Algorithm.

Floyd Warshall Algorithm is a dynamic programming approach in which we try to find the shortest distance of a pair of vertices using an intermediate vertex whose value is already calculated as -:

distance[index1][index2] = min(distance[index1][index2], distance[index1][k] + distance[k][index2]) 

Where k is the intermediate vertex.

Algorithm:

  • Floyd Warshall Algorithm:
    • Make an array distance of size (V)*(V) to store the shortest distance between all pairs of vertices.
    • Initialize all values of the array by maximum integer value because, in the beginning, we assume that all vertices are unreachable.
    • For all vertices index from 0 to V - 1.
      • set distance[index][index] as 0 (distance between same vertices is 0).
    • Set the values of the edge weights into distance from the given edges.
    • Using each vertex from 0 to V - 1 as intermediate vertex k, do the following steps:
      • For a pair between index1 and index2, we are trying to minimize its minimum distance using intermediate vertex k. We will iterate k from 0 to V - 1.
        • Set distance[index1][index2]  as the minimum of distance[index1][index2] and distance[index1][k] + distance[k][index2].
  • Values of the Shortest distance between each pair are calculated.
  • Return True if the value of distance[S][D] (Shortest distance between S and D) is less than or equal to P. Otherwise, return False.
Time Complexity

O(V^3), where V is the number of vertices in the graph.

 

For each pair of vertices, we are trying each node as an intermediate node and the number of pairs is (V)*(V). So, the time complexity will be O(V^3). Hence, the overall time complexity is O(V^3).

Space Complexity

O(V^2), where V is the number of vertices in the graph.

 

The O(V^2) space is used to store the minimum distance between all pairs of vertices, and in total, there are V*V=V^2 pairs. Hence, the overall space complexity is O(V^2).

Code Solution
(100% EXP penalty)
Roads
Full screen
Console