Dijkstra's Algorithm

4

1 votes
Easy
Problem

Here You need to implement Dijkstra's Algorithm(Single Source Shortest Path Algorithm).

You will be given an adjacency matrix of an undirected graph and some q queries.

Each query contains two integers(0-indexed) denoting the source and destination vertices. 

For every query you need to print the shortest path between the given two vertices.

Input:

First line contains integer denoting the number of vertices in the graph.

Next lines contains integers denoting nxn adjacency matrix a,the value a[ i ] [ j ] denotes the distance between ith and jth vertex.

Next line contains integer denoting number of queries.

Next lines contains two integers denoting source and destination vertices

Output:

Output the lines denoting the shortest path between two vertices given in each respective query.

Constraints:

1 <=  n <= 9

1 <= q <= n

1<= a[i][j] <= 100000.

Sample Input
5
0 5 10 2 0
5 0 3 7 2
10 3 0 0 0
2 7 0 0 3
0 2 0 3 0
3
0 4
2 3
1 3
Sample Output
5
8
5
Time Limit: 5
Memory Limit: 256
Source Limit:
Contributers:
Editor Image

?