Dijkstra's Algorithm Problem Statement
Given an undirected graph with 'V' vertices (labeled 0, 1, ..., V-1) and 'E' edges, where each edge connects two nodes ('X', 'Y') and has a weight that denotes the distance between nodes 'X' and 'Y'.
Your task is to calculate the shortest path distance from the source node (labeled as 0) to all vertices in the graph.
Input:
The first line contains an integer 'T' indicating the number of test cases to run. Each test case starts with a line containing two integers 'V' and 'E', representing the number of vertices and edges, respectively.
Following this, each of the next 'E' lines contains three space-separated integers 'X', 'Y', and 'DISTANCE', representing a node 'X', a node 'Y', and the distance between them.
Output:
For each test case, output a single line with 'V' space-separated integers that denote the shortest distance for each node from node 0 to node 'V'-1. If a node is disconnected, output 2147483647 for that node. Separate outputs with a new line for each test case.
Example:
Input:
4 5
0 1 5
0 2 8
1 2 9
1 3 2
2 3 6
Output:
0 5 8 7
Constraints:
- 1 <= T <= 50
- 1 <= V <= 1000
- 1 <= E <= 3000
- 0 <= X, Y < V
- 1 <= DISTANCE[X][Y] <= 10^5
Note:
Do not print the output in the function; it is handled automatically. Implement the function as required.

AnswerBot
4mo
Dijkstra's algorithm is used to find the shortest path distance from a source node to all other nodes in a graph.
Implement Dijkstra's algorithm to find the shortest path distances from node 0 to all o...read more
Help your peers!
Add answer anonymously...
Directi Software Developer interview questions & answers
A Software Developer was asked Q. Stock Buy and Sell Problem Statement Given an array/list 'prices' where the elem...read more
A Software Developer was asked Q. Box Stacking Problem Statement Consider you are provided with 'n' different type...read more
A Software Developer was asked Q. Find Triplets with Given Sum Problem Statement Given an array ARR consisting of ...read more
Popular interview questions of Software Developer
A Software Developer was asked Q1. Stock Buy and Sell Problem Statement Given an array/list 'prices' where the elem...read more
A Software Developer was asked Q2. Box Stacking Problem Statement Consider you are provided with 'n' different type...read more
A Software Developer was asked Q3. Find Triplets with Given Sum Problem Statement Given an array ARR consisting of ...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

