Dijkstra's Algorithm solves the single source shortest path problem in O((E + V)logV) time, which can be improved to O(E + VlogV) when using a Fibonacci heap. This note requires that you understand basic graph theory terminology and concepts.
The sssp is to find the shortest distance from the source vertex to all other vertices in the graph. We can store this in a simple array.
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 1<<29;
int N, M, adj[1002][1002], dist[1002]; bool flag[1002];
void dijkstra(int s){
fill(dist, dist+1002, INF);
dist[s] = 0;
for(int i=1; i<=N; i++){
int d=INF, u=0;
for(int j=1; j<=N; j++)
if(!flag[j] && dist[j]< d){
d=dist[j]; u=j;
}
flag[u] = 1;
for(int j=1; j<=N; j++)
if(!flag[j])
dist[j]=min(dist[j], dist[u]+adj[u][j]);
}
}
int main(){
cin >> N >> M;
fill_n(&adj[0][0], 1002*1002, INF);
for(int i=0, u, v, w; i<M; i++){
cin >> u >> v >> w;
adj[u][v] = adj[v][u] = min(adj[u][v], w);
}
dijkstra(1);
for(int i=1; i<=N; i++)
if(flag[i]) cout << dist[i] << endl;
else cout << -1 << endl;
}
CLRS explains it best, but the Wikipedia page is good as well.
Dijkstra's algorithm can be easily sped up using a priority queue, pushing in all unvisited vertices during step 4 and popping the top in step 5 to yield the new current vertex.
Visit VisuAlgo.