#include #include #include #include #include #include #include #include using namespace std; #define INFINITY numeric_limits::max( ) struct Graph { int num_connections; int shortestDistance; int prevPath; struct { int weight; int dest; } edges[5]; }; typedef struct Graph graph; typedef graph *graphPtr; int main() { int nodecount = 30, i; graphPtr myGraph; myGraph = (graphPtr)calloc(nodecount, sizeof(graph)); if(myGraph != NULL) { for(i=0; i, greater > q; q.push( 0 ); int top = -1, sum; while( !q.empty( ) ) { if( top == q.top( ) ) { q.pop( ); } else { top = q.top( ); for(i = 0; i sum) { myGraph[myGraph[top].edges[i].dest].shortestDistance = sum; myGraph[myGraph[top].edges[i].dest].prevPath = top; if( top == q.top() ) q.pop(); q.push(myGraph[top].edges[i].dest); } } } } cout << endl; for(i=0; i "; int preRoute = myGraph[i].prevPath; while(preRoute != -1) { cout << preRoute << " --> "; preRoute = myGraph[preRoute].prevPath; } cout << "End\n"; } cout << "Shortest distance is " << myGraph[i].shortestDistance << "\n\n"; } cout << endl; return 0; }