【数据结构与算法】利用Dijkstra算法求图的最短路径(头歌)

任务描述

本关任务:用邻接矩阵存储有向图,实现最短路径Dijkstra算法,图中边的权值为整型,顶点个数少于 10 个。

编程要求

根据提示,在右侧编辑器补充代码。

测试说明

输入描述

首先输入图中顶点个数和边的条数; 再输入顶点的信息(字符型); 再输入各边及其权值。

输出描述

依次输出从编号为0的顶点开始的从小到大的所有最短路径,每条路径及其长度占一行。

输入样例:

5 7

A B C D E

0 1 6

0 2 2

0 3 1

1 2 4

1 3 3

2 4 6

3 4 7

输出样例:

AD 1

AC 2

AB 6

ADE 8

#include <stdio.h>
#include <string.h>
#define MaxSize 10
#define INF 32767

typedef struct MGraph {
    char vertex[MaxSize];// 顶点数组
    int arc[MaxSize][MaxSize];// 边数组
    int vertexNum, arcNum;// 顶点数和边数
} MGraph;
// 初始化图
void MGraph_init(MGraph *G, char a[], int n, int e) {
	//write your code.
	//========begin=======
    G->vertexNum = n; G->arcNum = e;
    
    for (int i=0; i<n; i++)
    {
    	G->vertex[i] = a[i];
	}

	for (int i=0; i<n; i++)
	{
		for (int j=0; j<n; j++)
		{
			if (i == j)
			{
				G->arc[i][j] = 0;
			}
			else
			{
				G->arc[i][j] = INF;
			}
		}
	}
	
	int i, j, x;
	
	for (int k=0; k<e; k++)
	{
		scanf("%d %d %d", &i, &j, &x);
		
		G->arc[i][j] = x; 
	}
	//=========end========
}
// 找到dist数组中最小值对应的顶点下标
int Min(int dist[], int vertexNum) {
	//========begin=======
	int index = 0, min = INF;

	for (int i=0; i<vertexNum; i++)
	{
		if ((dist[i] != 0) && (dist[i] < min))
		{
			min = dist[i];
			index = i;
		}
	}
	
	return index;
	//=========end========
}
// 将字符ch添加到字符串str的末尾
void strch(char str[], char ch) {
    int l = strlen(str);
    str[l] = ch;
    str[l + 1] = '\0';
}
// Dijkstra算法求最短路径
void MGraph_Dijkstra(MGraph *G, int v) {
	//========begin=======
    int dist[MaxSize], num;
    char path[20][20] = {'\0'};
    
    for (int i=0; i<G->vertexNum; i++)
    {
    	dist[i] = G->arc[v][i];
    	
		if (dist[i] != INF)
    	{
    		strch(path[i], G->vertex[v]);
    		strch(path[i], G->vertex[i]);
		}
	}
	
	dist[v] = 0; num = 1;
	
	while (num < G->vertexNum)
	{
		int k = Min(dist, G->vertexNum);
		
		printf("%s %d\n", path[k], dist[k]);
		
		for (int i=0; i<G->vertexNum; i++)
		{
			if (dist[i] > dist[k] + G->arc[k][i])
			{
				dist[i] = dist[k] + G->arc[k][i];
    			
				for (int j=0; path[k][j]; j++)
				{
					strch(path[i], path[k][j]);
				}
    			
				strch(path[i], G->vertex[i]);
			}
		}
		
		dist[k] = 0;
		num++;
	}
	//=========end========
}
int main() {
    int n, e;
    scanf("%d %d", &n, &e);
    char p[MaxSize];
    int i;
    for (i = 0; i < n; i++) {
        scanf(" %c", &p[i]);
    }
    MGraph MG;
    MGraph_init(&MG, p, n, e);
    MGraph_Dijkstra(&MG, 0);
    return 0;
}

 

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
利用Dijkstra算法实现地铁网络最短路径数据结构设计的关键代码如下: ```python # 定义地铁网络的节点类 class Node: def __init__(self, name): self.name = name self.neighbors = {} # 存储相邻节点及对应的距离 def add_neighbor(self, neighbor, distance): self.neighbors[neighbor] = distance def get_neighbors(self): return self.neighbors.keys() def get_distance(self, neighbor): return self.neighbors[neighbor] # 定义地铁网络的类 class SubwayNetwork: def __init__(self): self.nodes = {} # 存储所有节点 def add_node(self, node): self.nodes[node.name] = node def get_node(self, name): return self.nodes[name] def dijkstra(self, start_node, end_node): # 初始化距离字典,存储起点到各节点的最短距离 distances = {node: float('inf') for node in self.nodes} distances[start_node] = 0 # 初始化已访问节点集合和未访问节点集合 visited = set() unvisited = set(self.nodes) while unvisited: # 选择当前距离最小的节点 current_node = min(unvisited, key=lambda node: distances[node]) # 更新当前节点的邻居节点的最短距离 for neighbor in current_node.get_neighbors(): new_distance = distances[current_node] + current_node.get_distance(neighbor) if new_distance < distances[neighbor]: distances[neighbor] = new_distance # 将当前节点标记为已访问,并从未访问集合中移除 visited.add(current_node) unvisited.remove(current_node) # 如果已经到达终点节点,则停止搜索 if current_node == end_node: break return distances[end_node] # 创建地铁网络 subway = SubwayNetwork() # 添加地铁站节点 station_a = Node("A") station_b = Node("B") station_c = Node("C") station_d = Node("D") station_e = Node("E") # 添加地铁站之间的连接关系及距离 station_a.add_neighbor(station_b, 5) station_a.add_neighbor(station_c, 3) station_b.add_neighbor(station_d, 2) station_c.add_neighbor(station_b, 1) station_c.add_neighbor(station_d, 6) station_d.add_neighbor(station_e, 4) # 将节点添加到地铁网络中 subway.add_node(station_a) subway.add_node(station_b) subway.add_node(station_c) subway.add_node(station_d) subway.add_node(station_e) # 计算最短路径 start_station = station_a end_station = station_e shortest_distance = subway.dijkstra(start_station, end_station) print(f"The shortest distance from {start_station.name} to {end_station.name} is {shortest_distance}") ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

布凯彻-劳斯基

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值