You are suggested to use the teaching servers burrow.soic.indiana.edu or hulk.soic.indiana.edu or tank.soic.indiana.edu for practicing C programs.
DIJKSTRA(G, s) DISTANCE[0...n-1] = INFINITY DISTANCE[s] = 0 sourceNode.vertex = s sourceNode.dist = 0 PQ.INSERT(sourceNode) WHILE PQ is not empty u = PQ.EXTRACT_MIN_DIST() for each vertex v adjacent to u IF DISTANCE[v] > DISTANCE[u] + edgeCost(u,v) DISTANCE[v] = DISTANCE[u] + edgeCost(u,v) node.vertex = v node.dist = DISTANCE[v] PQ.INSERT(node)At the end DISTANCE[0...n-1] holds the shortest path from s to each vertices.
#include <stdio.h> #include <stdlib.h> #include <iostream> #include <queue> #include <vector> using namespace std; typedef struct node_struct { int vertex; int dist; } Node; typedef struct edge_struct { int u1; int u2; int cost; } Edge; vector<Node> graph[50]; int main() { const int INF = 9999999; int n = 5; int u, v, d; int u1, u2, dist, cost; Edge edgeList[10] = { {0, 1, 10}, {0, 3, 5}, {1, 2, 1}, {1, 3, 2}, {2, 4, 4}, {3, 1, 3}, {3, 2, 9}, {3, 4, 2}, {4, 0, 7}, {4, 2, 6} }; for(int i = 0; i < 10; ++i) { u1 = edgeList[i].u1; u2 = edgeList[i].u2; cost = edgeList[i].cost; Node t; t.vertex = u2; t.dist = cost; graph[u1].push_back(t); } for(int i = 0; i < n; ++i) { u = i; cout << u << ": "; for(vector<Node>::iterator vi = graph[u].begin(); vi != graph[u].end(); ++vi) { v = vi->vertex; cost = vi->dist; cout << "[" << v << "," << cost << "] "; } cout << endl; } return 0; }We will need a priority queue to hold the nodes. We can initialize the Distance[] array and priority queue the following way:
std::priority_queue<Node, vector<Node>, NodeCompare> PQ; for(int i = 0; i < n; ++i) distance[i] = INF; distance[0] = 0; // Assuming 0 is the source node Node node; node.vertex = 0; // Assuming 0 is the source node node.dist = 0; PQ.push(node);