floyd算法C++实现.docVIP

  • 1
  • 0
  • 约6.63千字
  • 约 6页
  • 2018-03-08 发布于河南
  • 举报
floyd算法C实现

#include iostream #include iomanip using namespace std; #define? MAXV? 100 #define? INF? 32767 typedef int InfoType; typedef int Vertex; typedef struct {??? ? ?int no;????????????? ? ? ? ?InfoType info;??????????? ? } VertexType;???????????? ? //顶点类型 typedef struct? ????????????????????? ? {??? ? ?int edges[MAXV][MAXV]; ? ? ?int n,e; ? ????????? ? ? ? ?VertexType vexs[MAXV]; } MGraph;???????????????????????? ? //图类型 void Ppath(int path[][MAXV], int i, int j) {?? ? int k; ??? ? k=path[i][j]; ??? ? if (k==-1)? return;???????????? ? //递归出口 ??? ? Ppath(path,i,k);?????????????? ? ??? ? coutk ? ;???????? ? ?//输出k????????? ? ??? ? Ppath(path,k,j);?????????????? ? } void Dispath(int A[][MAXV],int path[][MAXV],int n) { ? int i,j; ? ? for (i=0;in;i++) ?? for (j=0;jn;j++) ?? ?? if (A[i][j]==INF) ? ? { ? if(i!=j) cout 从 i 到 ?????????????????? ? j 不存在路径\n; ? ? } ? ? else ? ? {??? ? cout 从 i 到 j ????????????????????? ? ? 的路径为: i ; ????????? ? Ppath(path, i, j);? ? ????????????? ? ? coutj; ???????????????????????????? ? cout \t\t 路径长度为:A[i][j] ??????????????????????????????????? ? endl; ? ? } } void Floyd(MGraph g) {?? ? int A[MAXV][MAXV],path[MAXV][MAXV]; ??? ? int i,j,k; ??? ? for (i=0;ig.n;i++) ? for (j=0;jg.n;j++) ????? ? ? {? ?? A[i][j]=g.edges[i][j]; ?? path[i][j]=-1; ? } ??? ? for (k=0;kg.n;k++) ? for (i=0;ig.n;i++) ????? ? ? for (j=0;jg.n;j++) ??? if (A[i][j](A[i][k]+A[k][j])) ????????????? ? ?? { ???? A[i][j]=A[i][k]+A[k][j]; ???? path[i][j]=k;? ??? } ?Dispath(A,path,g.n);???????????????????????? ? //输出最短路径 ? } void DispMat(MGraph g) { ?int i,j; ?for(i=0;ig.n;i++) ?{ ? for(j=0;jg.n;j++) ?? if(g.edges[i][j]==INF) ??? cout setw(3) ∞; ?? else coutsetw(3)g.edges[i][j]; ? coutendl; ?} } void Floyd(MGraph g); void Ppath(int path[][MAXV], int i, int j) ; void Dispath(int A[][MAXV],int path[][MAXV],int n); void DispMat(MGraph g); void main() { ?int i,j; ?MGraph g; ?cout输入邻接矩阵g总顶点数g.n和总边数g.e:; ?cing.ng.e; ?cout输入邻接矩阵g的元素值:\n; ?for(i=0;ig.n;i++) ?{ ? cou

文档评论(0)

1亿VIP精品文档

相关文档