最短路径问题,djkstra算法

本文介绍了一个基于C++实现的最短路径算法,通过构建图结构并应用Dijkstra算法来解决两个城市之间的最短路径问题。文章提供了一个完整的示例程序,包括图的构建、边的插入以及最短路径的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem C: Shortest Path

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 1007   Solved: 348
[ Submit][ Status][ Web Board]

Description

Abrao is a beautiful country. It has N cities (1 ≤ N≤ 2,500) and C (1 ≤ C ≤ 10,000) highways to connect adjacent cities. Each city is assigned with a unique number from 1 to N. You are required to calculate the length of the shortest path from one city to another.

Input

The first line contains four integers N, C, S and T. N and C has been defined above. S and T (1 ≤ S,T≤ 2,500) are the numbers of two cities. The information about the highways is defined in the following C lines, each one of which contains three integers vi,vj and w  indicating a high way between vand vwith the distance equal to w (there may be multiple highways between two cities). You are required to calculate the length of the shortest path from the city S to T. 

Output

The output is the length of the shortest path from the city S to T.

Sample Input

7 11 5 42 4 21 4 37 2 23 4 35 7 57 3 36 1 16 3 42 4 35 6 37 2 1

Sample Output

7
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
#define UNVISITED false
#define VISITED true

class Edge
{
private:
	int destination;
	int weight;
public:
	Edge(int a, int b)
	{
		destination = a;
		weight = b;
	}
	Edge()
	{
		destination = (int)INT_MAX;
		weight = (int)INT_MAX;
	}
	int Getdestination()
	{
		return destination;
	}
	int Getweight()
	{
		return weight;
	}
};

class graph
{
private:
	int numofvertices;
	int numofhighways;
	vector<Edge>* graphvertices;
	bool* mask;
public:
	graph(int numver, int numhigh)
	{
		numofvertices = numver;
		numofhighways = numhigh;
		graphvertices = new vector<Edge>[numver];
		mask = new bool[numver];
		for (int i = 0; i < numver; i++)
		{
			mask[i] = UNVISITED;
		}
	}
	void Insert(int start, int destination, int weight)
	{
		Edge tmp(destination, weight);
		graphvertices[start - 1].push_back(tmp);
	}
	vector<Edge> Getverticelist(int i)
	{
		return graphvertices[i];
	}
	int Getnumofvertices()
	{
		return numofvertices;
	}
	int Getnumofhighways()
	{
		return numofhighways;
	}
	bool Getmask(int i)
	{
		return mask[i];
	}
	void Setmask(int i, bool change)
	{
		mask[i] = change;
	}

};
int Minvertice(graph* G, int* D)
{
	int min = INT_MAX;
	int v = 0;
	for (int i = 0; i < G->Getnumofvertices(); i++)
	{
		if (G->Getmask(i) == UNVISITED&&D[i] < min)
		{
			v = i;
			min = D[i];
		}
	}
	return v;
}
void djkstra(graph* G, int* D, int s)
{
	for (int i = 0; i < G->Getnumofvertices(); i++)
	{
		D[i] = INT_MAX;
	}
	D[s - 1] = 0;
	for (int i = 0; i < G->Getnumofvertices(); i++)
	{
		int v = Minvertice(G, D);
		G->Setmask(v, VISITED);

		for (int i = 0; i < G->Getverticelist(v).size(); i++)
		{

			int w = G->Getverticelist(v).at(i).Getdestination() - 1;
			if (D[w] >(D[v] + G->Getverticelist(v).at(i).Getweight()))
			{
				D[w] = (D[v] + G->Getverticelist(v).at(i).Getweight());

			}
		}

	}
}

int main()
{
	int numcities, numhighway, Start, Terminal;
	cin >> numcities >> numhighway >> Start >> Terminal;

	graph* path = new graph(numcities, numhighway);
	int* D = new int[numcities];
	while (numhighway--)
	{
		int start, destination, weight;
		cin >> start >> destination >> weight;

		path->Insert(start, destination, weight);
		path->Insert(destination, start, weight);
	}
	djkstra(path, D, Start);
	cout << D[Terminal - 1] << endl;
	return 0;
}

经验:尽量使用c++库内包含的数据结构,自己写链表还有队列之类的会很崩溃的,而且浪费时间又做不好。
INT_MAX要引入<limits.h>头文件
以下是C语言实现图的邻接矩阵存储和Djkstra算法的示例代码: ```c #include <stdio.h> #include <limits.h> // 用于定义INT_MAX #define MAX_VERTICES 100 // 最大顶点数 // 图的邻接矩阵存储结构 typedef struct { int n; // 顶点数 int weight[MAX_VERTICES][MAX_VERTICES]; // 邻接矩阵 } Graph; // Djkstra算法,求图G从顶点v出发到其他顶点的最短路径 void Djkstra(Graph G, int v, int *dist, int *prev) { int i, j, k, min; int visited[MAX_VERTICES]; // 标记顶点是否已经访问 // 初始化 for (i = 0; i < G.n; i++) { visited[i] = 0; dist[i] = G.weight[v][i]; if (dist[i] < INT_MAX) { prev[i] = v; } else { prev[i] = -1; } } visited[v] = 1; dist[v] = 0; // 主循环 for (i = 1; i < G.n; i++) { min = INT_MAX; // 找未访问顶点中距离最近的 for (j = 0; j < G.n; j++) { if (!visited[j] && dist[j] < min) { min = dist[j]; k = j; } } visited[k] = 1; // 更新距离和前驱顶点 for (j = 0; j < G.n; j++) { if (!visited[j] && G.weight[k][j] < INT_MAX && dist[k] + G.weight[k][j] < dist[j]) { dist[j] = dist[k] + G.weight[k][j]; prev[j] = k; } } } } int main() { Graph G; int i, j, v; int dist[MAX_VERTICES]; // 存放最短路径长度 int prev[MAX_VERTICES]; // 存放最短路径的前驱顶点 // 输入顶点数和邻接矩阵 printf("请输入顶点数:"); scanf("%d", &G.n); printf("请输入邻接矩阵:\n"); for (i = 0; i < G.n; i++) { for (j = 0; j < G.n; j++) { scanf("%d", &G.weight[i][j]); if (G.weight[i][j] == 0) { G.weight[i][j] = INT_MAX; } } } // 输入起始顶点 printf("请输入起始顶点:"); scanf("%d", &v); // 求最短路径 Djkstra(G, v, dist, prev); // 输出结果 for (i = 0; i < G.n; i++) { printf("从顶点%d到顶点%d的最短路径长度为%d,路径为:", v, i, dist[i]); j = i; while (j != v && prev[j] != -1) { printf("%d <- ", j); j = prev[j]; } if (j == v) { printf("%d\n", j); } else { printf("无\n"); } } return 0; } ``` 程序首先定义了一个Graph结构体,用于存储图的邻接矩阵;然后定义了Djkstra算法,求出从指定顶点出发到其他顶点的最短路径;最后在main函数中输入邻接矩阵和起始顶点,调用Djkstra函数求解最短路径,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值