标签:typedef jks isp == void ndis 最短路 i++ 测试
从一个顶点到其余各顶点的最短路径
typedef struct { int edges[MAXV][MAXV]; InfoType vex[MAXV]; int n, e; }MatGraph;
void Dispath(MatGraph g, int dist[], int path[], int S[], int v) { int i, j, k; int apath[MAXV], d; for (i = 0; i < g.n; i++) { if (S[i] == 1 && i != v) { printf("顶点%d到顶点%d的路径长度为:%d\t路径为:",v, i, dist[i]); d = 0; apath[d] = i; k = path[i]; if (k == -1) printf("无路径\n"); else { while (k != v) { apath[++d] = k; k = path[k]; } apath[++d] = v; printf("%d", apath[d]); for (j = d - 1; j >= 0; j--) printf(",%d", apath[j]); printf("\n"); } } } }
void Dijkstral(MatGraph g, int v) { int i, j, u; int mindist; int path[MAXV], dist[MAXV], S[MAXV]; for (i = 0; i < g.n; i++) { S[i] = 0; dist[i] = g.edges[v][i]; if (g.edges[v][i] < INF) path[i] = v; else path[i] = -1; } S[v] = 1; path[v] = 0; for (i = 1; i < g.n; i++) { mindist = INF; for (j = 0; j < g.n; j++) { if (S[j] == 0 && dist[j] < mindist) { u = j; mindist = dist[j]; } } S[u] = 1; for (j = 0; j < g.n; j++) { if (S[j] == 0) { if (g.edges[u][j] < INF && dist[u] + g.edges[u][j] < dist[j]) { dist[j] = dist[u] + g.edges[u][j]; path[j] = u; } } } } Dispath(g, dist, path, S, v); }
测试用例
7 11
100 4 6 6 100 100 100
100 100 1 100 7 100 100
100 100 100 100 6 4 100
100 100 2 100 100 5 100
100 100 100 100 100 100 6
100 100 100 100 1 100 8
100 100 100 100 100 100 100
标签:typedef jks isp == void ndis 最短路 i++ 测试
原文地址:https://www.cnblogs.com/KIROsola/p/11916907.html