Time complexity of the following algorithm is O(M log N), where M is number of edges and N is number of vertices. Dijkstra can also be implemented as O(n * log (n) + m) using a fibonacci heap, which can be faster, especially with dense graphs.

UM[x] is the total distance between the first vertex and x.
S shows which vertices are selected before.
A is our matrix that shows the distances. For example, A[i][j] is the distance between i and j.