Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Data structures 在dijkstra'中找到最短路径;C语言中的s算法_Data Structures_Dijkstra - Fatal编程技术网

Data structures 在dijkstra'中找到最短路径;C语言中的s算法

Data 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

此代码返回从选定节点到图形其余节点的距离。但是它没有正确地输出从源节点到其他节点的路径

#定义最大值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][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;