Dijsktra

Given a weighted undirected graph having A nodes.
A matrix B of size M x 3 is given which represents the edges such that there is an edge between
B[i][0] and B[i][1] having weight B[i][2].
Given the source node C.

You have to find an integer array D of size A such that

D[i] : Shortest distance form the C node to node i. if node i is not reachable from C then -1.

Note:

  1. There are no self-loops in the graph.

  2. No multiple edges between two pair of vertices.

  3. The graph may or may not be connected.

  4. Nodes are Numbered from 0 to A-1.

  5. Your solution will run on multiple testcases. If you are using global variables make sure to clear them.



Input Format

The first argument given is an integer A.
The second argument given is the matrix B.
The third argument given is an integer C.

Output Format

Return the integer array D.

Constraints

1 <= A <= 100000
0<= B[i][0],B[i][1] < A
0 <= B[i][2] <= 1000;
0 <= C < A

For Example

Input 1:
    A = 6
    B = [   [0, 4, 9]
            [3, 4, 6] 
            [1, 2, 1] 
            [2, 5, 1] 
            [2, 4, 5] 
            [0, 3, 7] 
            [0, 1, 1] 
            [4, 5, 7] 
            [0, 5, 1] ] 
    C = 4
Output 1:
    D = [7, 6, 5, 6, 0, 6]

Input 2:
    A = 5
    B = [   [0, 3, 4]
            [2, 3, 3] 
            [0, 1, 9] 
            [3, 4, 10] 
            [1, 3, 8]  ] 
    C = 4
Output 2:
    D = [14, 18, 13, 10, 0]
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Dijsktra on Interview Code Editor
Sign Up
to access hints and editorial solutions for Dijsktra

Discussion


Loading...
Click here to start solving coding interview questions