Dijkstra算法在C相邻矩阵 [英] Dijkstra on adjacency matrix in C
问题描述
我需要一些帮助,Dijkstra算法在C中。
I need some help with Dijkstra's algorithm in C.
我已经生成了我的邻接矩阵,这看起来是这样的:
I've generated my adjacency matrix, that looks something like:
int mat[NB][NB] = {{0, 171, MAX, 132, [...]}, {171, 0, 30, 39, [...]}, , [...]};
我发现这个实现: http://www.answers.com /主题/迪杰斯特拉 - 均算法-1 的但该路径是一个1维阵列和我的矩阵是一个2维阵列。
I've found this implementation: http://www.answers.com/topic/dijkstra-s-algorithm-1 but the path is an 1-dimensional array and my matrix is a 2-dimensional array.
有没有一种方法来转换一个到另一个? 或者,也许有人有一个方法来对付这种矩阵。
Is there a way to transform one to another? Or maybe someone has a method to deal with this kind of matrix.
在此先感谢您的帮助
推荐答案
如果您通过垫[0]
来一个函数期待一个 INT *
(和尺寸),该功能可以很容易地把2维矩阵作为1维矩阵
If you pass mat[0]
to a function expecting an int *
(and a size), that function can easily treat the 2-dimensional matrix as a 1-dimensional matrix.
#include <stdio.h>
int foobar(int *arr, int siz) {
int sum = 0;
for (int i = 0; i < siz; i++) sum += arr[i];
return sum;
}
int main(void) {
int mat[10][10] = {{4, -3, 7}, {5}};
printf("%d\n", foobar(mat[0], 10*10));
return 0;
}
编辑: ideone( http://ideone.com/2mLi7 )运行上面的程序无投诉: - )
ideone ( http://ideone.com/2mLi7 ) runs the program above without complaints :-)
这篇关于Dijkstra算法在C相邻矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!