1 //20142880 Tang Binghui Shijiazhuang Railway University2#include <iostream>3#include <string>4 using namespacestd;5 #defineMaxnum 327676 #defineN 1007 8 9 Tentypedefstruct One { A stringDianarr[n]; - stringInfo[n]; - intArcs[n][n]; the intDiannum,biannum; - }floydstruct; - - intLocatevex (Floydstruct G,stringa) + { - inti; + BOOLFlag =0; A for(i =0; I < g.diannum;i++) at if(A = =G.dianarr[i]) - { -Flag =1; - returni; - } - if(Flag = =0) in { - return-1; to } + } - voidCreateudn (Floydstruct &G) the { *cout <<"Please enter the number of vertices and the number of sides:"<<Endl; $CIN >> G.diannum >>G.biannum;Panax Notoginsengcout <<"Please enter the vertex information:"<<Endl; - for(inti =0; i<g.diannum;i++) theCIN >>G.dianarr[i]; + for(inti =0; i<g.diannum;i++) A for(intj =0; j<g.diannum;j++) theG.ARCS[I][J] =Maxnum; + stringV1,v2; - intw,i,j; $cout <<"Please enter the two vertices and path lengths that exist for the path:"<<Endl; $ for(intK =0; k<g.biannum;k++) - { -CIN >> v1 >> v2 >>W; thei = Locatevex (G,V1);//gets the array subscript of the point -j =Locatevex (G,V2);WuyiG.ARCS[I][J] = W;//weight value assigned to two points the } - } Wu - About intPath[n][n]; $ intD[n][n]; - voidFloyd (floydstruct G)//Freud Core Algorithm - { - for(inti =0; I < g.diannum;i++) A for(intj =0; J < g.diannum;j++) + { theD[I][J] =G.arcs[i][j]; - if(d[i][j]<maxnum) $PATH[I][J] =i; the Else thePATH[I][J] =-1; the } the for(intK =0; k < g.diannum;k++)/** Core **/ - for(inti =0; I < g.diannum;i++) in for(intj =0; J < g.diannum;j++) the if(D[i][k] + d[k][j] <D[i][j]) the { AboutD[I][J] = D[i][k] +D[k][j]; thePATH[I][J] =Path[k][j]; the } the } + - voidPRINTFL (floydstruct G)//Output the {Bayi /*string A, B; the int i,j,font = 1; the cout << "Please enter your current location and where you want to go:" << Endl; - cin >> a >> b; - i = Locatevex (g,a); the j = Locatevex (g,b); the if (d[i][j]==maxnum) the cout << "The shortest distance is:" << "not Up!" "<< Endl; the Else - cout << "minimum Distance:" << d[i][j]<< Endl;*/ the for(intI=0; i<g.diannum;i++) the { the for(intj=i+1; j<g.diannum;j++)94 { the if(d[i][j]!=maxnum) the { thecout<<g.dianarr[i]<<"------>"<<G.Dianarr[j]<<"the shortest path is"<< d[i][j]<<Endl;98 } About } - 101 }102 103 }104 the voidMain ()106 {107 floydstruct G;108CREATEUDN (G);//generating adjacency matrices109 Floyd (G); the PRINTFL (G);111}
Freud C + + Shortest path algorithm