Given a graph of **V** nodes represented in the form of adjacency matrix. The task is to find the shortest distance of all the vertex's from the source vertex.

**Input:**

The first line of input contains an integer T denoting the number of test cases . Then T test cases follow .The first line of each test case contains an integer V denoting the size of the adjacency matrix and in the next line are 2*V space separated values, which denotes the weight of an edge of the matrix (graph). The third line of each test case contains an integer denoting the source vertex s.

**Output:**

For each test case output will be V space separated integers where the ith integer denote the shortest distance of ith vertex from source vertex.

**You task:**

You are required to complete the function** dijkstra()** which takes 3 arguments. The first argument is the adjacency matrix (graph), the second argument is the source vertex s and the third argument is V denoting the size of the matrix. The function prints V space separated integers where i'th integer denotes the shortest distance of the i'th vertex from source vertex.

**Constraints:**

1 <= T <= 20

1 <= V <= 20

0 <= graph[i][j] <= 1000

0 <= s

**Example:**

**Input**:

2

2

0 25 25 0

0

3

0 1 43 1 0 6 43 6 0

2

**Output**:

0 25

7 6 0

**Explanation:
Testcase 1: **Shortest distance of source node 0 to 1 is 25, and shortest distance of source to source is 0.

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

the_coder95 | 465 |

thanuvinu94 | 388 |

adityasuman2025 | 348 |

Senbonzakura_Kageyoshi | 321 |

PrakharBansal1 | 294 |

the_coder95 | 1691 |

RishabhTanwar1 | 1110 |

thanuvinu94 | 1064 |

samay_kam_hai | 726 |

adityasuman2025 | 646 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4897 |

Quandray | 4547 |

Login to report an issue on this page.