最短路径(Dijikstra和Floyed)
2014-04-01 16:07
363 查看
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> typedef unsigned int ssize_t; const ssize_t MAXSIZE = 20; static ssize_t graph [MAXSIZE][MAXSIZE]; static ssize_t node_num; static char node [MAXSIZE]; #define FLOYED #ifdef DIJIKSTRA static ssize_t dist [MAXSIZE]; static char path [MAXSIZE]; static bool visited[MAXSIZE]; void find_path_from(char node) { ssize_t i = 0, j = 0; ssize_t start_index = node - 'A'; ssize_t cur_min_path = 0; char cur_start_index = 0; visited[start_index] = true; for(i = 0; i < node_num - 1; i++) { if(graph[start_index][i] < UINT_MAX) dist[i] = graph[start_index][i]; else dist[i] = UINT_MAX; } path[start_index] = 'A' + start_index; dist[start_index] = 0; for(j = 0; j < node_num; j++) { cur_min_path = UINT_MAX; for(i = 0; i < node_num; i++) { if((visited[i] == false) && (dist[i] < cur_min_path)) { cur_min_path = dist[i]; cur_start_index = i; } } visited[cur_start_index] = true; for(i = 0; i < node_num; i++) { if((visited[i] == false) && (dist[i] > dist[cur_start_index] + graph[cur_start_index][i])) { dist[i] = dist[cur_start_index] + graph[cur_start_index][i]; path[i] = cur_start_index + 'A'; } } } } #endif #ifdef FLOYED void floyed() { int k = 0, i = 0, j = 0; for(k = 0; k < node_num; k++) { for(i = 0; i < node_num; i++) { for(j = 0; j < node_num; j++) { if(graph[i][j] > graph[i][k] + graph[k][j]) graph[i][j] = graph[i][k] + graph[k][j]; } } } } #endif int main(ssize_t argc, char * argv [ ]) { ssize_t i = 0, j = 0; ssize_t edge_num = 0; ssize_t distance = 0; char start_node, end_node; #ifdef DIJIKSTRA memset(graph, 0xFF, MAXSIZE * MAXSIZE * sizeof(ssize_t)); memset(dist, 0xFF, MAXSIZE * sizeof(ssize_t)); #endif printf("请输入节点数量(小于%d):", MAXSIZE); scanf("%u", &node_num); for(i = 0; i < node_num; i++) node[i] = 'A' + i; printf("节点编号已自动分配为:%c-%c。\n", 'A', node[node_num - 1]); printf("请输入边的数量:"); scanf("%u", &edge_num); while(getchar()!='\n'); for(i = 0; i < edge_num; i++) { printf("请输入第%d条边(如'A-B:20'):", i + 1 ); scanf("%c-%c:%u",&start_node, &end_node, &distance); graph[start_node - 'A'][end_node - 'A'] = distance; graph[end_node - 'A' ][start_node - 'A'] = distance; while(getchar()!='\n'); } #ifdef DIJIKSTRA find_path_from('A'); for(i = 0; i < node_num; i++) { printf("%c->%c:%u\n", 'A', node[i], dist[i]); } #endif #ifdef FLOYED floyed(); for(i = 0; i < node_num; i++) { printf("%c->%c:%u\n", 'A', node[i], graph[0][i]); } #endif return 0; }
相关文章推荐
- POJ 2253 Frogger 最短路径 Floyed-Warshall算法
- 数据结构之最短路径(Dijkstra 和 Floyed)
- floyed任意两点间最短路径
- 数据结构_图_最短路径_弗洛伊德(Floyed)算法
- poj 1125 (floyed 最短路径)
- 【模板】最短路径(Floyed&SPFA )
- 例题:信使 最优乘车 最短路径 图论算法之Floyed 学习笔记
- 最短路径算法(上)——迪杰斯特拉(Dijikstra)算法
- 最短路径问题Floyed-Warshall(图…
- POJ 2240 Arbitrage 最短路径 Floyed-Warshall
- Floyed 算法求最短路径
- 弗洛伊德算法Floyed(求各顶点间最短路径):可打印最短路径
- 最短路径之Floyed-Warshall算法
- 从零开始学算法(五)最短路径之Floyed-Warshall算法
- Floyed算法求两点之间的最短路径
- poj 2457 Part Acquisition最短路径dijikstra迪杰斯特拉
- 多源最短路径+源码<floyed>
- 模板 图的遍历 bfs+dfs 图的最短路径 Floyed+Dijkstra
- [图论] 最短路径(Bellman-Ford , SPFA , Floyed , Dijkstra)
- Floyed算法求多源最短路径