The trick here is to use a priority queue , where each and every distance would be pushed.

Next we will eject elements one by one , note that if the distance is already ejected and copied in the minimum distance vector , it will be ejected but not copied into the min distance vector , here is an implementation of it , I made for Free Tickets (INOI 2012) problem,

```
int dijkstra(std::vector<std::vector<int> >graph,int vertex,int size){
std::vector<int>map(size,INT_MAX);
std::priority_queue<distances>heap;
std::vector<int>visited(size,false);
map[vertex] = 0;
for(int i=0;i<size;i++){
if(i == vertex){
continue;
}
if(graph[vertex][i] != NIL){
distances a;
a.index = i;
a.dist = graph[vertex][i];
heap.push(a);
}else{
distances a;
a.index = i;
a.dist = INT_MAX;
heap.push(a);
}
}
visited[vertex] = true;
while(!heap.empty()){
distances top = heap.top();
heap.pop();
int ind = top.index;
int distance = top.dist;
if(visited[ind]){
continue;
}
//std::cout << ind << ' ';
map[ind] = distance;
visited[ind] = true;
for(int i=0;i<size;i++){
if(graph[ind][i] != NIL && !visited[i]){
distances a;
a.index = i;
//std::cout << ind << ' ' << distance << ' ';
a.dist = graph[ind][i] + distance;
if( a.dist < map[i]){
heap.push(a);
}
}
}
}
//std::cout << std::endl;
int ret = *std::max_element(map.begin(),map.end());
return ret;
}
```

Hope it helps , cheers!