标签:ges ++ typedef rap lse struct max div lang
各队顶点之间的最短路径及其路径长度
typedef int InfoType; typedef struct { int edges[MAXV][MAXV]; InfoType vex[MAXV]; int n, e; }MatGraph;
void Dispath(MatGraph g, int A[][MAXV], int path[][MAXV]) { int apath[MAXV], d; int i, j, k; for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) { if (A[i][j] < INF && i != j) { printf("从%d到%d的路径为:", i, j); d = 0; apath[d] = j; k = path[i][j]; while (k != -1 && k != i) { apath[++d]=k; k = path[i][k]; } apath[++d] = i; printf("%d", apath[d]); for (k = d - 1; k >= 0; k--) printf(",%d", apath[k]); printf("\t路径长度为:%d\n", A[i][j]); } } } }
void Floyd(MatGraph g) { int A[MAXV][MAXV]; int path[MAXV][MAXV]; int i, j, k; for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) { A[i][j] = g.edges[i][j]; if (i != j && g.edges[i][j] < INF) path[i][j] = i; else path[i][j] = -1; } } for (k = 0; k < g.n; k++) { for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) { if (A[i][j] > A[i][k] + A[k][j]) { A[i][j] = A[i][k] + A[k][j]; path[i][j] = path[k][j]; } } } } Dispath(g,A,path); }
测试数据
3 5
0 4 11
6 0 2
3 100 0
标签:ges ++ typedef rap lse struct max div lang
原文地址:https://www.cnblogs.com/KIROsola/p/11919360.html