static int Pathmatirx[MAX_VERTEX_NUM]; //存储最短路径下标
static int ShortPathTable[MAX_VERTEX_NUM]; // 存储到各点最短路径权重和
void Dijkstra(MGraph *graph){
int v,w,k,min;
int final[MAX_VERTEX_NUM]; // 标记数组,记录 v0 到某个顶点的最短路径是否找到
for(v = 0; v < MAX_VERTEX_NUM; v++){ // 初始化数据
final[v] = 0;
ShortPathTable[v] = graph->arcs[0][v].adj;
Pathmatirx[v] = 0;
}
// V0 不需要进一步计算了
ShortPathTable[0] = 0;
final[0] = 1;
for( v = 1; v < graph->vexnum; v ++){
min = INFINITY;
for(w = 0; w < graph->vexnum; w++){
if(!final[w] && ShortPathTable[w] < min){
k = w;
min = ShortPathTable[w];
}
}
final[k] = 1;
for(w = 0; w < graph->vexnum; w ++){
if(!final[w] && (min + graph->arcs[k][w].adj < ShortPathTable[w])){
ShortPathTable[w] = min + graph->arcs[k][w].adj;
Pathmatirx[w] = k;
}
}
}
printf("shortPathTable:");
for(w = 0; w < graph->vexnum; w ++){
printf(" %d ",ShortPathTable[w]);
}
printf("\nPathmatirx:");
for(w = 0; w < graph->vexnum; w ++){
printf(" %d ",Pathmatirx[w]);
}
Dijkstra
最新推荐文章于 2023-02-22 23:33:21 发布