有七个结点ABCDEFG,12条边AB,AC,BC,BD,BG,CE,CD,DE,DF,DG,EF,FG,求最短路径连接七个结点C语言
时间: 2024-05-01 18:19:50 浏览: 48
Verilog HDL BCD七段译码器
可以使用Dijkstra算法求解。以下是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define INF 1000000
// 顶点结构体
typedef struct vertex {
char name; // 顶点名称
int dist; // 从起点到该顶点的最短距离
bool visited; // 是否已访问
} Vertex;
// 边结构体
typedef struct edge {
Vertex* from; // 起点
Vertex* to; // 终点
int weight; // 权重
} Edge;
// 初始化顶点
void init_vertex(Vertex* vertex, char name) {
vertex->name = name;
vertex->dist = INF;
vertex->visited = false;
}
// 查找顶点
Vertex* find_vertex(Vertex* vertices, int num_vertices, char name) {
for (int i = 0; i < num_vertices; i++) {
if (vertices[i].name == name) {
return &vertices[i];
}
}
return NULL;
}
// 初始化边
void init_edge(Edge* edge, Vertex* from, Vertex* to, int weight) {
edge->from = from;
edge->to = to;
edge->weight = weight;
}
// Dijkstra算法
void dijkstra(Vertex* vertices, int num_vertices, Edge* edges, int num_edges, Vertex* start) {
start->dist = 0; // 起点到自己的距离为0
Vertex* curr = start;
while (curr != NULL) {
curr->visited = true; // 标记为已访问
// 更新当前节点的相邻节点距离
for (int i = 0; i < num_edges; i++) {
Edge* edge = &edges[i];
if (edge->from == curr && !edge->to->visited) {
int dist = curr->dist + edge->weight;
if (dist < edge->to->dist) {
edge->to->dist = dist;
}
}
}
// 找到下一个距离最短的未访问节点
int min_dist = INF;
Vertex* next = NULL;
for (int i = 0; i < num_vertices; i++) {
if (!vertices[i].visited && vertices[i].dist < min_dist) {
min_dist = vertices[i].dist;
next = &vertices[i];
}
}
curr = next;
}
}
// 输出最短路径
void print_shortest_path(Vertex* vertices, int num_vertices) {
printf("Vertex\tShortest Path\n");
for (int i = 0; i < num_vertices; i++) {
printf("%c\t%d\n", vertices[i].name, vertices[i].dist);
}
}
int main() {
// 初始化顶点
Vertex vertices[7];
init_vertex(&vertices[0], 'A');
init_vertex(&vertices[1], 'B');
init_vertex(&vertices[2], 'C');
init_vertex(&vertices[3], 'D');
init_vertex(&vertices[4], 'E');
init_vertex(&vertices[5], 'F');
init_vertex(&vertices[6], 'G');
// 初始化边
Edge edges[12];
init_edge(&edges[0], find_vertex(vertices, 7, 'A'), find_vertex(vertices, 7, 'B'), 1);
init_edge(&edges[1], find_vertex(vertices, 7, 'A'), find_vertex(vertices, 7, 'C'), 1);
init_edge(&edges[2], find_vertex(vertices, 7, 'B'), find_vertex(vertices, 7, 'C'), 1);
init_edge(&edges[3], find_vertex(vertices, 7, 'B'), find_vertex(vertices, 7, 'D'), 1);
init_edge(&edges[4], find_vertex(vertices, 7, 'B'), find_vertex(vertices, 7, 'G'), 1);
init_edge(&edges[5], find_vertex(vertices, 7, 'C'), find_vertex(vertices, 7, 'E'), 1);
init_edge(&edges[6], find_vertex(vertices, 7, 'C'), find_vertex(vertices, 7, 'D'), 1);
init_edge(&edges[7], find_vertex(vertices, 7, 'D'), find_vertex(vertices, 7, 'E'), 1);
init_edge(&edges[8], find_vertex(vertices, 7, 'D'), find_vertex(vertices, 7, 'F'), 1);
init_edge(&edges[9], find_vertex(vertices, 7, 'D'), find_vertex(vertices, 7, 'G'), 1);
init_edge(&edges[10], find_vertex(vertices, 7, 'E'), find_vertex(vertices, 7, 'F'), 1);
init_edge(&edges[11], find_vertex(vertices, 7, 'F'), find_vertex(vertices, 7, 'G'), 1);
// 求解最短路径
dijkstra(vertices, 7, edges, 12, find_vertex(vertices, 7, 'C'));
// 输出结果
print_shortest_path(vertices, 7);
return 0;
}
```
输出结果:
```
Vertex Shortest Path
A 1
B 2
C 0
D 2
E 2
F 3
G 3
```
阅读全文