单源最短路模板(大概)

图论基本问题,最短路问题。包含单源最短路,多源汇最短路等问题。

下面是一个基本的导图

 

 

Dijkstra算法实际上是一种贪心的思想:

第一个距离起始点最短距离的点作为下一次的起点,则下一次找距离最小的点的距离一定也是最小的。(这也是为什么不能处理负权边)。 

首先是朴素版的Dijsktra算法使用邻接矩阵(二维数组)来存储图 

部分代码: 

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 510;
int n, m;
int g[N][N];//边权数组
int dist[N];//距离数组
bool st[N];//判断点是否已经经过
int dijkstra()
{
	memset(dist, 0x3f, sizeof dist);//初始所有的点距离为正无穷
	dist[1] = 0;
	for (int i = 0; i < n; i++)
	{
		int t = -1;
		for (int j = 1; j <= n; j++)
		{
			if (!st[j] && (t == -1 || dist[t] > dist[j]))
				t = j;
			st[i] = true;

			for (int j = 1; j <= n; j++)
			{
				dist[j] = min(dist[j], dist[t] + g[t][j]);
			}
		}
	}
	if (dist[n] == 0x3f3f3f3f) return -1;
	return dist[n];
}

 堆优化版的Dijkstra算法通过堆的特性来降低每一次选取最短边的点的时间复杂度

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stdio.h>
using namespace std;
const int N = 500010;
int e[N], ne[N], w[N], h[N], dist[N], idx;//链表所需的变量e[]存储点ne[]指向下一个节点w[]存储权边h[]存储每一个链表的头节点
bool st[N];//判断是否经过
typedef pair<int, int> PII;
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dijkstra()
{
	memset(dist, 0x3f, sizeof dist);
	dist[1] = 0;
	priority_queue<PII, vector<PII>, greater<PII> >heap;//小根堆
	heap.push({ 0,1 });
	while (heap.size())
	{
		auto t = heap.top();
		heap.pop();
		int ver = t.second, distance = t.first;
		if (st[ver]) continue;
		st[ver] = true;//防止重复
		for (int i = h[ver]; i != -1; i = ne[i])
		{
			int j = e[i];
			if (dist[j] > distance + w[i])
			{
				dist[j] = distance + w[i];
				heap.push({ dist[j],j });
			}
		}
	}
}

那么存在负权边时,就不能用Dijstra算法了。

Bellman-Ford算法

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 510;
int dist[N], backup[N];//backup[]记录之前的未更新的状态
int n, m, k;
struct Edge
{
	int a, b, w;
}edges[N];//a,b是加入的点,w是边权。
int bellman_ford()
{
	memset(dist, 0x3f, sizeof dist);
	dist[1] = 0;
	for (int i = 0; i < k; i++)
	{
		memcpy(backup, dist, sizeof dist);//复制之前状态防止漏解
		for (int j = 0; j < m; j++)
		{
			int a = edges[j].a, b = edges[j].b, w = edges[j].w;
			dist[b] = min(dist[b], backup[a] + w);
		}
	}
	if (dist[n] > 0x3f3f3f3f / 2) return -1;//>0x3f3f3f3f/2而不是==0x3f3f3f3f,因为存在负权边,可能导致最后更新时不相等
	return dist[n];
}

SPFA算法,可以说是上一个算法的优化版本,利用优先队列。事实上,第二遍循环,可以发现,可以通过上一个符合条件点来找到下一个点,当我们让符合条件的点进入队列后,就不需要循环操作来找到下一个点。 

 

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 510;
int e[N], ne[N], h[N], w[N],idx;
int dist[N];
bool st[N];
void add(int a,int b,int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
int spfa()
{
	memset(dist, 0x3f, sizeof dist);
	dist[1] = 0;
	queue<int> q;//定义队列
	q.push(1);
	st[1] = true;
	while (q.size())
	{
		int t = q.front();
		q.pop();
		st[t] = true;//判断点是否重复进入队列
		for (int i = h[t]; i != -1; i = ne[i])
		{
			int j = e[i];
			if (dist[j] > dist[t] + w[i])
			{
				dist[j] = dist[t] + w[i];
				if (!st[i])
				{
					q.push(j);
					st[j] = true;
				}
			}
		}

	}
}

多源汇最短路.... 

Folyd..emmm...

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值