Data structures 在dijkstra'中找到最短路径;C语言中的s算法
此代码返回从选定节点到图形其余节点的距离。但是它没有正确地输出从源节点到其他节点的路径 #定义最大值10 #定义inf 999 void dijk(int G[max][max],int n,int start){ 整数成本[max][max],距离[max],访问量[max],预测量[max]; int i,j,count,Minist,nextnodeData structures 在dijkstra'中找到最短路径;C语言中的s算法,data-structures,dijkstra,Data Structures,Dijkstra,此代码返回从选定节点到图形其余节点的距离。但是它没有正确地输出从源节点到其他节点的路径 #定义最大值10 #定义inf 999 void dijk(int G[max][max],int n,int start){ 整数成本[max][max],距离[max],访问量[max],预测量[max]; int i,j,count,Minist,nextnode for(i=0; i<n; i++) { for(j=0; j<n; j++) { if(G[i
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(G[i][j] == 0)
{
cost[i][j] = inf;
}
else
{
cost[i][j] = G[i][j];
}
}
}
for(i=0; i<n; i++)
{
dist[i] = cost[start][i];
pred[i] = start;
visited[i] = 0;
}
dist[start] = 0;
visited[start] = 1;
count = 1;
while(count < n-1)
{
mindist = inf;
for(i=0; i<n; i++)
{
if(dist[i] < mindist && !visited[i])
{
mindist = dist[i];
nextnode = i;
}
}
visited[nextnode] = 1;
for(i=0; i<n; i++)
{
if(!visited[i])
{
if((mindist + cost[nextnode][i]) < dist[i])
{
dist[i] = mindist + cost[nextnode][i];
pred[i] = nextnode;
}
}
}
count++;
}
for(i=0; i<n; i++)
{
if(i != start)enter code here
{
printf("The distance of node %d is %d", i, dist[i]);
printf("\nThe path is : %d", i);
j=1;
do
{
j = pred[j];
printf("<- %d", j);
}while(j != start);
}
printf("\n");
} }
(i=0;i)的
printf("No. of vertices : \n");
scanf("%d", &n);
printf("Enter the graph : \n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
scanf("%d", &G[i][j]);
}
}
printf("Enter the starting node : \n");
scanf("%d", &start);
dijk(G, n, start);
return 0;