Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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
试着写Dijkstra';C语言中的s算法及其程序崩溃_C_Algorithm_Dijkstra - Fatal编程技术网

试着写Dijkstra';C语言中的s算法及其程序崩溃

试着写Dijkstra';C语言中的s算法及其程序崩溃,c,algorithm,dijkstra,C,Algorithm,Dijkstra,因此,我刚刚遇到了Dijkstra的路径查找算法,并决定用C语言进行试验。我编写了代码,以获取Dijkstra图的数据,并找到最短路径。我使用的数据是: 节点数:5个 成本矩阵 0 4 0 8 0 4 0 3 0 0 0 3 0 4 0 8 0 4 0 7 0 0 0 7 0 要访问的节点:5 路径数:2条 路径矩阵 1 2 3 4 5 1 4 5 0 0 它应该输出: 最小距离为15英里 最小距离路径:->1->4->5 代码如下: #include <stdio.h> #incl

因此,我刚刚遇到了Dijkstra的路径查找算法,并决定用C语言进行试验。我编写了代码,以获取Dijkstra图的数据,并找到最短路径。我使用的数据是:

节点数:5个

成本矩阵 0 4 0 8 0 4 0 3 0 0 0 3 0 4 0 8 0 4 0 7 0 0 0 7 0 要访问的节点:5

路径数:2条

路径矩阵 1 2 3 4 5 1 4 5 0 0 它应该输出:

最小距离为15英里

最小距离路径:->1->4->5

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>

int main(){
    int cost[10][10], path[10][10], distance[10], column, index = 1, row, min, n, i, j, v, p;

    printf("Enter number of nodes: ");
    scanf(" %d", &n);

    printf("\n\nEnter cost matrix: ");

    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++){
            scanf(" %d", &cost[i][j]);
        }
        printf("\n");
    }

    printf("\n\nEnter the node to visit: ");
    scanf(" %d", &v);
    printf("\nEnter the number of paths for the node: ");
    scanf(" %d", &p);

    printf("\n\nEnter path matrix: ");

    for(i = 1; i <= p; i++){
        for(j = 1; j <= n; j++){
            scanf(" %d", &path[i][j]);
        }
        printf("\n");
    }                                  // program crashes here

    for(i = 1; i <= p; i++){
        distance[i] = 0;
        row = 1;
        for(j = 1; j <=n; j++){
            if(row != v){
                column = path[i][j + 1];
                distance[i] = distance[i] + cost[row][column];
            }
        }
        row = column;
    }

    min = distance[1];
    for(i = 1; i <= p; i++){
        if(distance[i] <= min   ){
            min = distance[i];
            index = i;
        }
    }

    printf("Minimum distance is %d\n\n", min);
    printf("Minimum distance path:");
    for(i = 1; i <= n; i++){
        if(path[index][i] != 0){
            printf(" ->%d", path[index][i]);
        }
    }

    return 0;
}
#包括
#包括
#包括
#包括
int main(){
int cost[10][10],path[10][10],distance[10],column,index=1,row,min,n,i,j,v,p;
printf(“输入节点数:”);
scanf(“%d”和“&n”);
printf(“\n\n输入成本矩阵:”);

对于(i=1;i错误消息是什么?您是否尝试过使用调试符号编译并在调试器中运行它?调试器告诉您什么?您没有调试器?获取一个。真的。您有一个调试器,但您不知道如何使用它?现在开始学习。是的,现在正是开始的最佳时机。我使用调试选项运行代码,然后r它返回了一个信号:程序接收到信号SIGSEGV,分段错误。为什么要从
1
运行循环(和数组索引)?这是打破数组边界的好方法。您要输入多少节点?如果是
10
,那么灾难就会发生。如果使用
[j+1]进行索引,情况可能会更糟;