c语言解决邻接矩阵为{0,4,0,0,0,0,0,8,0}, {4,0,8,0,0,0,0,11,0}, {0,8,0,7,0,4,0 ,0 ,2}, {0,0,7,0,9 ,14 ,0 ,0 ,0}, {0,0,0,9,0 ,10, 0 ,0 ,0}, {0,0,4,14,10 ,0 ,2 ,0 ,0}, {0,0,0,0,0 ,2 ,0 ,1 ,6}, 8,11,0,0,0 ,0 ,1 ,0 ,7}, {0,0,2,0,0 ,0 ,6 ,7, 0}}的无向连通有权图的单源最短路径问题,源点为0点,在控制台窗口输出每个点到0点的最短距离和对应的路径,要求使用迪杰斯特拉算法
时间: 2023-08-22 15:09:30 浏览: 59
好的,这是一个经典的最短路径问题,使用 Dijkstra 算法可以解决。以下是解决该问题的 C 语言代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_VERTEX_NUM 9
#define INFINITY 0x7fffffff
typedef struct {
int vexs[MAX_VERTEX_NUM];
int arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int vexnum, arcnum;
} Graph;
void CreateGraph(Graph* G) {
G->vexnum = MAX_VERTEX_NUM;
G->arcnum = 14;
for (int i = 0; i < G->vexnum; ++i) {
G->vexs[i] = i;
for (int j = 0; j < G->vexnum; ++j) {
G->arcs[i][j] = INFINITY;
}
}
G->arcs[0][1] = G->arcs[1][0] = 4;
G->arcs[0][7] = G->arcs[7][0] = 8;
G->arcs[1][2] = G->arcs[2][1] = 8;
G->arcs[1][7] = G->arcs[7][1] = 11;
G->arcs[2][3] = G->arcs[3][2] = 7;
G->arcs[2][5] = G->arcs[5][2] = 4;
G->arcs[2][8] = G->arcs[8][2] = 2;
G->arcs[3][4] = G->arcs[4][3] = 9;
G->arcs[3][5] = G->arcs[5][3] = 14;
G->arcs[4][5] = G->arcs[5][4] = 10;
G->arcs[5][6] = G->arcs[6][5] = 2;
G->arcs[6][7] = G->arcs[7][6] = 1;
G->arcs[6][8] = G->arcs[8][6] = 6;
G->arcs[7][8] = G->arcs[8][7] = 7;
}
void Dijkstra(Graph* G, int start, int* dist, int* path) {
bool visited[G->vexnum];
for (int i = 0; i < G->vexnum; ++i) {
visited[i] = false;
dist[i] = INFINITY;
path[i] = -1;
}
visited[start] = true;
dist[start] = 0;
for (int i = 0; i < G->vexnum; ++i) {
if (G->arcs[start][i] != INFINITY) {
dist[i] = G->arcs[start][i];
path[i] = start;
}
}
for (int i = 1; i < G->vexnum; ++i) {
int minDist = INFINITY;
int minIndex = -1;
for (int j = 0; j < G->vexnum; ++j) {
if (!visited[j] && dist[j] < minDist) {
minDist = dist[j];
minIndex = j;
}
}
if (minIndex == -1) {
break;
}
visited[minIndex] = true;
for (int j = 0; j < G->vexnum; ++j) {
if (!visited[j] && G->arcs[minIndex][j] != INFINITY) {
int newDist = dist[minIndex] + G->arcs[minIndex][j];
if (newDist < dist[j]) {
dist[j] = newDist;
path[j] = minIndex;
}
}
}
}
}
void PrintPath(int* path, int end) {
if (path[end] == -1) {
printf("%d", end);
return;
}
PrintPath(path, path[end]);
printf(" -> %d", end);
}
int main() {
Graph G;
CreateGraph(&G);
int dist[MAX_VERTEX_NUM];
int path[MAX_VERTEX_NUM];
Dijkstra(&G, 0, dist, path);
for (int i = 1; i < G.vexnum; ++i) {
printf("0 -> %d: distance = %d, path = ", i, dist[i]);
PrintPath(path, i);
printf("\n");
}
return 0;
}
```
该程序使用了结构体来表示图,包含了顶点数、边数、顶点数组和邻接矩阵。在 CreateGraph 函数中初始化了这个图,然后在 Dijkstra 函数中实现了 Dijkstra 算法,计算了每个点到起点 0 的最短距离和对应的路径。PrintPath 函数用于输出路径。最后在 main 函数中调用 Dijkstra 函数,并输出结果。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)