您好,我已经在 C Dijkstra 的算法中实现了查找最短路径,但我需要返回 n 条最短路径,任何人都知道我该怎么做。
我的dijkstra函数:
int * Dijkstra(graph **g, int totalVertex, int vStart) {
int i;
int *distance = (int*) malloc(totalVertex * sizeof (int));
int *last = (int*) malloc(totalVertex * sizeof (int));
int *visited = (int*) calloc(totalVertex, sizeof (int));
int maxDistance, m;
graph *vertex;
for (i = 0; i < totalVertex; i++) {
distance[i] = MAXINT;
last[i] = -1;
}
distance[vOrigem] = 0;
while (sum(visited, totalVertex) < totalVertex) {
maxDistance = MAXINT;
for (i = 0; i < totalVertex; i++) {
if ((distance[i] < maxDistance) && (visited[i] == 0)) {
maxDistance = distance[i];
m = i;
}
}
vertex = g[m];
while (vertex != NULL) {
if ((vertex->distance + distance[m]) < (distance[vertex-> destination])) {
distance[vertex->destination] = vertex->distance + distance[m];
last[vertex->destination] = m;
}
vertex = vertice->next;
}
visited[m] = 1;
}
free(distance);
free(visited);
return last;
}
例如,我需要调用此函数 2 次,它会返回图中的两条最短路径。
谢谢你。