dijkstra算法

对于图有两种,一种是矩阵式的方方正正的,另一种是链表式的没有固定方向的,对于第一种

cpp 复制代码
int n;
std::vector<std::vector<int>>a(n, std::vector<int>(n)),dis(n,std::vector<int>(n));
int dx[5] = { 0,1,0,-1,0 };
int dy[5] = { 0,0,1,0,-1 };
using  PII= std::pair<int, int>;
PII st,end;
struct node {
	int x, y, dis;
	bool operator>(const node& other)const {
		return dis > other.dis;
	}
};
int dijkstra() {
	std::priority_queue<node>q;
	q.push({ st.first, st.second, 0 });
	dis[st.first][st.second] = 0;
	while (q.empty()) {
		node t = q.top();
		q.pop();
		if (t.x == end.first && t.y == end.second)return t.dis;
		for (int i = 1; i <=4; i++) {
			int nx = t.x + dx[i];
			int ny = t.y + dy[i];
			if (nx >= 0 && nx < n && ny >= 0 && ny < n) {
				int ndis = t.dis + a[nx][ny];
				if (ndis < dis[nx][ny]) {
					dis[nx][ny] = ndis;
					q.push({ nx,ny,ndis });
				}
			}
		}
	}
	return -1;
}
int main() {
	//输入获取起点终点和地图
	dijkstra();
	//输出结果
}

本质上是个广搜,和第二种相同的方面有以下几点,我们需要一个dis数组来存每个节点的搜索值,用于储存当前节点的最优值,本质框架不变,使用优先队列作为主要结构,但是这种判到终点的就是返回,且需要判断是否出界.还需要定义一个辅助方向数组.

来看看第二种

我们先来看有向图的情况

【模板】单源最短路径(标准版) - 洛谷

cpp 复制代码
constexpr int MaxM = 500010;
constexpr int MaxN = 100010;
struct edge {
	int to, dis, next;
};
edge e[MaxM];
int head[MaxN], dis[MaxN], cnt;
bool vis[MaxN];
int n, m, s;
inline void add_edge(int u, int v, int d) {
	cnt++;
	e[cnt].dis = d;
	e[cnt].to = v;
	e[cnt].next = head[u];
	head[u] = cnt;
}
struct node {
	int dis;
	int pos;
	bool operator<(const node& x)const {
		return x.dis < dis;
	}
};
std::priority_queue<node>q;
inline void dijkstra() {
	dis[s] = 0;
	q.push({ 0, s });
	while (!q.empty()) {
		node tmp = q.top();
		q.pop();
		int x = tmp.pos, d = tmp.dis;
		if (vis[x])continue;
		vis[x] = 1;
		for (int i = head[x]; i; i = e[i].next) {
			int y = e[i].to;
			if (dis[y] > dis[x] + e[i].dis) {
				dis[y] = dis[x] + e[i].dis;
				if (!vis[y]) {
					q.push({ dis[y],y });
				}
			}
		}
	}
}
int main() {
	std::cin >> n >> m >> s;
	for (int i = 1; i <= n; i++)dis[i] = 0x7fffffff;
	for (register int i = 0; i < m; i++) {
		register int u, v, d;
		std::cin >> u >> v >> d;
		add_edge(u, v, d);
	}
	dijkstra();
	for (int i = 1; i <= n; i++) {
		std::cout << dis[i]<<' ';
	}
	return 0;
}

有向图我们只需要正着存一次,并把dis数组初始化为INT_MAX.

而对于无向图,我们需要存两次,并把边数扩大为原来的两倍,相当于一去一回这个样子.

cpp 复制代码
constexpr int MAXM = 12410; // 修改为无向边转化后的边数的两倍
inline void add_edge(int u, int v, int d) {
	cnt++;
	// 第一条有向边:from u to v
	e[cnt].to = v;
	e[cnt].dis = d;
	e[cnt].next = h[u];
	h[u] = cnt;

	// 第二条有向边:from v to u
	cnt++;
	e[cnt].to = u;
	e[cnt].dis = d;
	e[cnt].next = h[v];
	h[v] = cnt;
}
相关推荐
JieE2123 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
JieE2121 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack201 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树1 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2122 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2122 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术2 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050732 天前
(一)小红的数组操作
算法·编程语言