编写一个程序实现北京地铁最短乘坐(站)线路查询,输入为起始站名和目的站名,输出为从起始站到目的站的最短乘坐站换乘线路。 注:1. 要求采用Dijkstra算法实现;2)如果两站间存在多条最短路径,找出其中的一条即可 【输入形式】 文件bgstations.txt为数据文件,包含了北京地铁的线路及车站信息。其格式如下: <地铁线路总条数> <线路1> <线路1站数> <站名1> <换乘状态> <站名2> <换乘状态> ... <线路2> <线路2站数> <站名1> <换乘状态> <站名2> <换乘状态> 从标准输入读入起始站和目的站 输出从起始站到目的站的乘坐信息,要求乘坐站数最少。换乘信息格式如下: SSN-n1(m1)-S1-n2(m2)-...-ESN 其中:SSN和ESN分别为起始站名和目的站名;n为乘坐的地铁线路号,m为乘坐站数;其它字符都是英文字符。 用C语言实现
时间: 2023-11-11 13:02:19 浏览: 177
基于Python实现北京地铁最短路径规划仿真(源码).rar
5星 · 资源好评率100%
好的,这是一个比较复杂的问题,需要用到Dijkstra算法进行最短路径的计算。这里提供一份C语言代码供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
#define MAX_STATION 100
typedef struct {
int line_num;
char line_name[MAX_LINE];
int station_count;
char station_name[MAX_STATION][MAX_LINE];
int station_transfer[MAX_STATION];
} subway_line;
typedef struct {
int station_num;
int line_num;
int transfer_count;
} vertex;
typedef struct {
int weight;
int next_vertex;
} edge;
void dijkstra(int start, int end, vertex *vertices, edge *edges, int vertex_count, char station_names[MAX_STATION][MAX_LINE], subway_line *lines, int line_count) {
int i, j, k, min, min_vertex;
int *prev_vertex = (int *)malloc(vertex_count * sizeof(int));
int *dist = (int *)malloc(vertex_count * sizeof(int));
int *visited = (int *)malloc(vertex_count * sizeof(int));
for (i = 0; i < vertex_count; ++i) {
prev_vertex[i] = -1;
dist[i] = -1;
visited[i] = 0;
}
dist[start] = 0;
visited[start] = 1;
int current_vertex = start;
while (current_vertex != end) {
for (i = 0; i < vertex_count; ++i) {
if (visited[i] || !edges[current_vertex * vertex_count + i].weight) {
continue;
}
if (dist[i] == -1 || dist[current_vertex] + edges[current_vertex * vertex_count + i].weight < dist[i]) {
dist[i] = dist[current_vertex] + edges[current_vertex * vertex_count + i].weight;
prev_vertex[i] = current_vertex;
}
}
min = -1;
min_vertex = -1;
for (i = 0; i < vertex_count; ++i) {
if (visited[i] || dist[i] == -1) {
continue;
}
if (min == -1 || dist[i] < min) {
min = dist[i];
min_vertex = i;
}
}
if (min_vertex == -1) {
break;
}
visited[min_vertex] = 1;
current_vertex = min_vertex;
}
if (dist[end] == -1) {
printf("No route found.\n");
} else {
int route[MAX_STATION];
int route_length = 0;
current_vertex = end;
while (current_vertex != start) {
route[route_length++] = current_vertex;
current_vertex = prev_vertex[current_vertex];
}
route[route_length++] = start;
printf("%s-%d(%d)", station_names[vertices[start].station_num], vertices[route[route_length - 1]].line_num, vertices[route[route_length - 1]].transfer_count);
for (i = route_length - 2; i >= 0; --i) {
int line_num1 = vertices[route[i + 1]].line_num;
int line_num2 = vertices[route[i]].line_num;
if (line_num1 != line_num2) {
char line_name[MAX_LINE];
int j;
for (j = 0; j < line_count; ++j) {
if (lines[j].line_num == line_num1) {
strcpy(line_name, lines[j].line_name);
break;
}
}
printf("-%s-%d(%d)", line_name, line_num2, vertices[route[i]].transfer_count);
}
printf("-%s", station_names[vertices[route[i]].station_num]);
}
printf("\n");
}
free(prev_vertex);
free(dist);
free(visited);
}
int main() {
FILE *fp;
char buf[MAX_LINE];
int line_count, station_count;
subway_line *lines;
char station_names[MAX_STATION][MAX_LINE];
vertex vertices[MAX_STATION * MAX_STATION];
edge edges[MAX_STATION * MAX_STATION];
int vertex_count = 0;
fp = fopen("bgstations.txt", "r");
if (fp == NULL) {
printf("Failed to open file.\n");
return 1;
}
fgets(buf, MAX_LINE, fp);
sscanf(buf, "%d", &line_count);
lines = (subway_line *)malloc(line_count * sizeof(subway_line));
for (int i = 0; i < line_count; ++i) {
fgets(buf, MAX_LINE, fp);
sscanf(buf, "%d %s %d", &lines[i].line_num, lines[i].line_name, &lines[i].station_count);
for (int j = 0; j < lines[i].station_count; ++j) {
fgets(buf, MAX_LINE, fp);
sscanf(buf, "%s %d", lines[i].station_name[j], &lines[i].station_transfer[j]);
int found = -1;
for (int k = 0; k < vertex_count; ++k) {
if (strcmp(station_names[vertices[k].station_num], lines[i].station_name[j]) == 0) {
found = k;
break;
}
}
if (found == -1) {
int station_num = vertex_count++;
vertices[station_num].station_num = station_num;
vertices[station_num].line_num = i;
vertices[station_num].transfer_count = lines[i].station_transfer[j];
strcpy(station_names[station_num], lines[i].station_name[j]);
found = station_num;
} else {
if (vertices[found].line_num != i) {
vertices[found].transfer_count = 1;
}
}
if (j > 0) {
int weight = vertices[found].transfer_count == 1 ? 1 : j - 1;
edges[(found - 1) * vertex_count + found].weight = weight;
edges[found * vertex_count + found - 1].weight = weight;
}
if (j < lines[i].station_count - 1) {
int found2 = -1;
for (int k = 0; k < vertex_count; ++k) {
if (strcmp(station_names[vertices[k].station_num], lines[i].station_name[j + 1]) == 0) {
found2 = k;
break;
}
}
if (found2 == -1) {
int station_num = vertex_count++;
vertices[station_num].station_num = station_num;
vertices[station_num].line_num = i;
vertices[station_num].transfer_count = lines[i].station_transfer[j + 1];
strcpy(station_names[station_num], lines[i].station_name[j + 1]);
found2 = station_num;
} else {
if (vertices[found2].line_num != i) {
vertices[found2].transfer_count = 1;
}
}
int weight = vertices[found].line_num == vertices[found2].line_num ? 1 : vertices[found].transfer_count + vertices[found2].transfer_count;
edges[found * vertex_count + found2].weight = weight;
edges[found2 * vertex_count + found].weight = weight;
}
}
}
fclose(fp);
char start_name[MAX_LINE], end_name[MAX_LINE];
int start = -1, end = -1;
printf("Please enter the start station: ");
scanf("%s", start_name);
printf("Please enter the end station: ");
scanf("%s", end_name);
for (int i = 0; i < vertex_count; ++i) {
if (strcmp(station_names[vertices[i].station_num], start_name) == 0) {
start = i;
}
if (strcmp(station_names[vertices[i].station_num], end_name) == 0) {
end = i;
}
}
if (start == -1 || end == -1) {
printf("Invalid station name.\n");
} else {
dijkstra(start, end, vertices, edges, vertex_count, station_names, lines, line_count);
}
free(lines);
return 0;
}
```
这个程序的输入为起始站名和目的站名,输出为从起始站到目的站的最短路径,其中包括经过的站点和换乘信息。程序首先从文件中读取北京地铁的线路及车站信息,并通过Dijkstra算法计算最短路径,最后输出路径信息。
阅读全文