I'm using an adjacency matrix to store the graph.
The code is as follows:
voidGraph:: Dijkstra () {structdijnode{intindex; intdistance; Vector<string>path; BOOLfounded;//determine if you have found ... }; Dijnode*shorestpaths=NewDijnode[mgraph.vexnum]; stringStartPoint; intStartIndex; cout<<"Please enter a single source point:"<<Endl; CIN>>StartPoint; Locate (Startpoint,startindex); //initializing an arrayshorestpaths[0]={startindex,0,vector<string>{startpoint},true}; for(intI=1; i<mgraph.vexnum;i++) {Shorestpaths[i]=dijnode{i,none,vector<string>{""},false}; } //Start Running for(intI=1; i<mgraph.vexnum;i++) {cout<<startIndex<<Endl; //If this point has not determined the shortest path and there is a path from the current point and is shorter than the current, then it is good to consider for(intI=0; i<mgraph.vexnum;i++){ if(I!=startindex&&mgraph.arcs[startindex][i].adj+shorestpaths[startindex].distance<shorestpaths[i]. Distance&&shorestpaths[i]. founded==false) {Shorestpaths[i].path=Shorestpaths[startindex].path; Shorestpaths[i].path.push_back (Mgraph.vexs[i]); Shorestpaths[i].distance=shorestpaths[startindex].distance+Mgraph.arcs[startindex][i].adj; } } //Select a fixed point that currently has the shortest distance and does not find the shortest path as StartPoint intmin=-1; for(intI=0; i<mgraph.vexnum;i++) {//Select the first min value that meets the requirements if(Shorestpaths[i]. founded==false) {MIN=i; Break; } } for(intI=0; i<mgraph.vexnum;i++){ if(Shorestpaths[i]. founded==false&&shorestPaths[i].distance<shorestpaths[min].distance) {MIN=i; }} Shorestpaths[min]. Founded=true; StartIndex=MIN; } //result Output for(intI=1; i<mgraph.vexnum;i++) {cout<<"from single source point"<<mgraph.vexs[shorestpaths[0].index]<<"to the"<<mgraph.vexs[shorestPaths[i].index]<<"the shortest path is:"; for(intj=0; J<shorestpaths[i].path.size ()-1; j + +) {cout<<shorestPaths[i].path[j]<<" -"; } cout<<shorestpaths[i].path[shorestpaths[i].path.size ()-1]<<"path Length:"<<shorestPaths[i].distance<<Endl; }}
Dijkstra (Dijkstra) algorithm to find the shortest path