- 1
- 0
- 约6.63千字
- 约 6页
- 2018-03-08 发布于河南
- 举报
floyd算法C实现
#include iostream#include iomanipusing namespace std;#define? MAXV? 100#define? INF? 32767typedef 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
您可能关注的文档
最近下载
- 市场部门市场营销专员工作手册(标准版).doc VIP
- 2025新课标义务教育数学(2022年版)课程标准试题库(含答案).docx VIP
- 电力工程专业设计工日定额-送电工程750kV部分.doc VIP
- 基于K-means和主成分分析的京张体育文化旅游带冰雪运动旅游市场研究.pdf VIP
- 电力工程设计收工日定额2012-3-28.doc VIP
- 人教版七年级下册英语全册教学设计(配2025年春新版教材).docx
- 电力工程专业设计工日定额-送电工程±500kV(直流)部分.doc VIP
- 2021春人教版九年级数学下册 第29章 全章教案.doc VIP
- 2025年湖北专升本武汉文理学院《动画概论》考试及答案.docx VIP
- 医院门诊服务优化与创新体系构建ppt课件.pptx
原创力文档

文档评论(0)