Dijkstra's Shortest Path Problem Statement
You are given an undirected graph with V
vertices (numbered from 0 to V-1) and E
edges. Each edge connects two nodes u
and v
and has an associated weight representing the distance between them. Your task is to find the shortest path distance from the source node, labeled as 0, to all vertices in the graph.
Input:
The first line of input contains an integer T
denoting the number of test cases.
The first line of each test case contains two integers, V
and E
, denoting the number of vertices and the number of edges, respectively.
The next E
lines each contain three integers u
, v
, and distance
, denoting an edge between nodes u
and v
, and the distance between them.
Output:
For each test case, print the shortest path distance from the source node (node 0) to all vertices. The distances should be printed in ascending order according to the node numbers, starting from node 0 up to node V-1.
For disconnected nodes, print the maximum positive integer value, 2147483647.
Example:
Input:
4 5
0 1 5
0 2 8
1 2 9
1 3 2
2 3 6
Output:
0 5 8 7
In the given example, the graph has 4 vertices and 5 edges. The shortest paths from node 0 are calculated using Dijkstra's algorithm, resulting in distances to all nodes being printed in sequence.
Constraints:
1 <= T <= 50
1 <= V <= 1000
1 <= E <= 3000
1 <= distance[u][v] <= 10^5
Note: You are not required to print the output; just implement the function to return the calculated distances.
The question is about finding the shortest path distance from a source node to all vertices in an undirected graph.
The graph is represented by the number of vertices and edges, followed by the edges a...read more
Top JUSPAY Front end Developer interview questions & answers
Popular interview questions of Front end Developer
Reviews
Interviews
Salaries
Users/Month