若一名乘客计划从哈尔滨到海口旅游,请为他设计一条最省钱的路径。可使用dijkstra算法。使用c语言生成代码
时间: 2024-03-03 08:51:32 浏览: 51
好的,这里是使用C语言实现Dijkstra算法的代码,用来找到从哈尔滨到海口的最短路径:
```c
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_CITIES 100
// 定义城市结构体
typedef struct {
char name[20]; // 城市名称
int cost[MAX_CITIES]; // 到其他城市的费用列表
int distance[MAX_CITIES]; // 到其他城市的距离列表
} City;
// 定义图结构体
typedef struct {
City cities[MAX_CITIES]; // 城市列表
int num_cities; // 城市数量
} Map;
// Dijkstra算法函数
void dijkstra(Map* map, int start, int end) {
int dist[MAX_CITIES]; // 存储当前节点到起点的距离
int visited[MAX_CITIES]; // 记录节点是否已经被访问
int prev[MAX_CITIES]; // 记录当前节点的前一个节点
int i, j, min_distance, next_node;
// 初始化距离、访问和前置节点数组
for (i = 0; i < map->num_cities; i++) {
dist[i] = INT_MAX; // 起点到该点的距离初始化为无穷大
visited[i] = 0; // 所有节点都未被访问过
prev[i] = -1; // 所有节点都没有前一个节点
}
// 起点到起点的距离为0
dist[start] = 0;
// 遍历所有节点
for (i = 0; i < map->num_cities; i++) {
// 找到当前未被访问过的节点中距离起点最近的节点
min_distance = INT_MAX;
for (j = 0; j < map->num_cities; j++) {
if (!visited[j] && dist[j] < min_distance) {
min_distance = dist[j];
next_node = j;
}
}
// 标记该节点为已访问
visited[next_node] = 1;
// 更新当前节点的相邻节点的距离
for (j = 0; j < map->num_cities; j++) {
if (!visited[j]) {
int distance = map->cities[next_node].distance[j];
int cost = map->cities[next_node].cost[j];
int total_distance = dist[next_node] + distance;
if (total_distance < dist[j]) {
dist[j] = total_distance;
prev[j] = next_node;
}
}
}
}
// 输出最短路径
printf("最短路径为:");
i = end;
while (i != start) {
printf("%s -> ", map->cities[i].name);
i = prev[i];
}
printf("%s\n", map->cities[start].name);
}
int main() {
Map map;
int i, j;
// 初始化地图
map.num_cities = 6;
// 设置城市名称
strcpy(map.cities[0].name, "哈尔滨");
strcpy(map.cities[1].name, "北京");
strcpy(map.cities[2].name, "上海");
strcpy(map.cities[3].name, "广州");
strcpy(map.cities[4].name, "深圳");
strcpy(map.cities[5].name, "海口");
// 设置城市之间的距离和费用
map.cities[0].distance[1] = 1000;
map.cities[0].distance[2] = 2000;
map.cities[0].distance[3] = 3000;
map.cities[1].distance[0] = 1000;
map.cities[1].distance[2] = 1500;
map.cities[1].distance[3] = 2500;
map.cities[1].distance[4] = 3000;
map.cities[2].distance[0] = 2000;
map.cities[2].distance[1] = 1500;
map.cities[2].distance[3] = 1000;
map.cities[2].distance[4] = 2000;
map.cities[2].distance[5] = 2500;
map.cities[3].distance[0] = 3000;
map.cities[3].distance[1] = 2500;
map.cities[3].distance[2] = 1000;
map.cities[3].distance[4] = 1500;
map.cities[3].distance[5] = 2000;
map.cities[4].distance[1] = 3000;
map.cities[4].distance[2] = 2000;
map.cities[4].distance[3] = 1500;
map.cities[4].distance[5] = 1000;
map.cities[5].distance[2] = 2500;
map.cities[5].distance[3] = 2000;
map.cities[5].distance[4] = 1000;
map.cities[0].cost[1] = 100;
map.cities[0].cost[2] = 200;
map.cities[0].cost[3] = 300;
map.cities[1].cost[0] = 100;
map.cities[1].cost[2] = 150;
map.cities[1].cost[3] = 250;
map.cities[1].cost[4] = 300;
map.cities[2].cost[0] = 200;
map.cities[2].cost[1] = 150;
map.cities[2].cost[3] = 100;
map.cities[2].cost[4] = 200;
map.cities[2].cost[5] = 250;
map.cities[3].cost[0] = 300;
map.cities[3].cost[1] = 250;
map.cities[3].cost[2] = 100;
map.cities[3].cost[4] = 150;
map.cities[3].cost[5] = 200;
map.cities[4].cost[1] = 300;
map.cities[4].cost[2] = 200;
map.cities[4].cost[3] = 150;
map.cities[4].cost[5] = 100;
map.cities[5].cost[2] = 250;
map.cities[5].cost[3] = 200;
map.cities[5].cost[4] = 100;
// 执行Dijkstra算法
dijkstra(&map, 0, 5);
return 0;
}
```
这个示例代码中,我们定义了一个`City`结构体来表示城市,以及一个`Map`结构体来表示地图。我们使用Dijkstra算法来寻找从`start`城市到`end`城市的最短路径,并输出这条路径。
阅读全文