标签:
艾兹格·W·迪科斯彻 (Edsger Wybe Dijkstra,1930年5月11日~2002年8月6日)荷兰人。 计算机科学家,毕业就职于荷兰Leiden大学,早年钻研物理及数学,而后转为计算学。曾在1972年获得过素有计算机科学界的诺贝尔奖之称的图灵奖,之 后,他还获得过1974年 AFIPS Harry Goode Memorial Award、1989年ACM SIGCSE计算机科学教育教学杰出贡献奖、以及2002年ACM PODC最具影响力论文奖。
艾兹格·W·迪科斯彻(Edsger Wybe Dijkstra)
Dijkstra算法C代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXINT 32767 #define MAXNUM 6 char *str = "ABCDEF"; void dijkstra(int *dist, int *prev, int (*A)[MAXNUM], int v0); void findPath(int *dist, int *prev, int start, int end); int main() { int A[MAXNUM][MAXNUM] = {{0, 6, 3, MAXINT, MAXINT, MAXINT}, {6, 0, 2, 5, MAXINT, MAXINT}, {3, 2, 0, 3, 4, MAXINT}, {MAXINT, 5, 3, 0, 2, 3}, {MAXINT, MAXINT, 4, 2, 0, 5}, {MAXINT, MAXINT, MAXINT, 3, 5, 0}, }; int dist[MAXNUM] = {0}; int prev[MAXNUM] = {0}; int v0 = 2, i = 0; dijkstra(dist, prev, A, v0); findPath(dist, prev, v0, 5); //for(i = 0; i < MAXNUM; i++) { // printf("%d ", prev[i]); } system("pause"); return 0; } void findPath(int *dist, int *prev, int start, int end) { int tmp = prev[end]; int *rst = (int*)calloc(MAXNUM, sizeof(int)); int cnt = 0, i = 0; rst[cnt] = end; cnt++; if(tmp == start) { rst[cnt] = tmp; } else { while(tmp != start) { rst[cnt] = tmp; tmp = prev[tmp]; cnt++; } rst[cnt] = tmp; } //printf("%d\n", cnt); for(i = cnt; i >= 0; i--) { printf("%c", str[rst[i]]); if(i != 0) { printf("->"); } } printf(" %d\n", dist[end]); free(rst); rst = NULL; } void dijkstra(int *dist, int *prev, int (*A)[MAXNUM], int v0) { int S[MAXNUM]; int n = MAXNUM, i = 0, j = 0; for (i = 0; i < n; i++) { dist[i] = A[v0][i]; S[i] = 0; if(dist[i] == MAXINT) { prev[i] = -1; } else { prev[i] = v0; // printf("%c前驱%c\n", str[i], str[v0]); } } dist[v0] = 0; S[v0] = 1; for(i = 1; i < n; i++) { int mindist = MAXINT; int u = v0; for(j = 0; j < n; j++) { if((!S[j]) && dist[j] < mindist) { u = j; mindist = dist[j]; } } S[u] = 1; for(j = 0; j < n; j++) { if((!S[j]) && A[u][j] < MAXINT) { if(dist[u] + A[u][j] < dist[j]) { dist[j] = dist[u] + A[u][j]; prev[j] = u; // printf("%c前驱%c\n", str[j], str[u]); } } } } }
代码仅供参考
参考资料
标签:
原文地址:http://www.cnblogs.com/hdu-2010/p/4672360.html