// Program 2-4 #include <the iostream> #include <WINDOWS.H> #include <Stack> the using namespace STD; const int N = 100 ; // number of cities may be modified const int INF = 1E7; // infinity 10000000 int Map [N] [N], dist [N], P [N], n-, m; // n-city number, m is the number of routes between cities strip BOOL in Flag [N]; // if s [i] is equal to true, vertex i description has been added to the set S; otherwise, i belongs to the set of vertices VS void the Dijkstra ( int U) { for ( int i = . 1 ; i <= n-; i ++ ) { Dist [I] = Map [u] [I]; // initialize u source shortest path length to each other vertex In Flag [I] = to false ; IF (dist [I] == INF) P [I] = - . 1 ; // source vertex u to the path length is infinite, indicating that the source vertex i and u is not adjacent to the else [i] P = u; // described vertex u i and adjacent to the source point, provided vertex i precursor P [I] = U } dist [U] = 0 ; in Flag [U] = to true ; // initially, only one element in the set S: source U for ( int I = 0 ; I <n-; + + I) { intTEMP = INF, t = u; for ( int J = . 1 ; J <= n-; J ++) // seek distance source u nearest vertex t is set in VS IF (In Flag [J] && dist [J] <! TEMP ) { t = J; TEMP = dist [J]; } IF (t == U) return ; // can not find t, out of the loop In Flag [t] = to true ; // otherwise, t is added to set for ( int = J . 1 ; J <= n-; J ++) // updated to t vertices adjacent to the source point u distance IF (in Flag [J] && Map [t] [J] <! INF) IF(dist[j]>(dist[t]+map[t][j])) { dist[j]=dist[t]+map[t][j] ; p[j]=t ; } } } void findpath(int u) { int x; stack<int>s; cout<<"源点为:"<<u<<endl; for(int i=1;i<=n;i++) { x=p[i]; while(! X = - . 1 ) { s.push (X); X = P [X]; } COUT << " source shortest path to the other as each vertex: " ; the while (! s.empty ()) { COUT s.top << () << " - " ; s.pop (); } COUT << << I " ; the shortest distance is: " << dist [I] << endl; } } int main () { int U, V, W, ST; System ( " Color 0D "); COUT << Please enter the number of cities: " << endl; CIN >> n-; COUT << " Please enter the number of routes between cities: " << endl; CIN >> m; COUT << " Please an input line and a distance between the cities: " << endl; for ( int I = . 1 ; I <= n-; I ++ ) for ( int J = . 1 ; J <= n-; J ++ ) { Map [I] [J] = INF;// initialize the adjacency matrix infinity } the while (M-- ) {" cin >>u >> v >>W; Map [U] [V] = min (Map [U] [V], W); // adjacency matrix storage, to retain a minimum distance } COUT << " Please enter a location where Bob: " << endl; ; CIN >> ST; the Dijkstra (ST); COUT << " position where Bob: " << ST << endl; for ( int I = . 1 ; I <= n-; I ++ ) { COUT << " Bob: " ST << << " - " <<" "To the new location: << I; IF (dist [I] == INF) COUT << " Sorry, no way up " << endl; the else COUT << " shortest distance is: " << dist [I] << endl; } FindPath (ST); return 0 ; }