问题
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, [...]}, , [...]};
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.
Thanks in advance for any help
回答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;
}
Edit: ideone ( http://ideone.com/2mLi7 ) runs the program above without complaints :-)
回答2:
In the link you provided, path
is an array where the output of the algorithm is written. The adjacency matrix in that example is apparently the dist
2D array.
来源:https://stackoverflow.com/questions/3719641/dijkstra-on-adjacency-matrix-in-c