跳到正文

个人记录

图论——Johnson全源最短路

Steadywelkin约 7 分钟
原文
文章信息与版本

文章信息

发布
2021年3月10日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 7 分钟

版本

  1. v1当前

JohnsonJohnson 全源最短路

我们最熟悉的全源最短路算法那肯定是 FloydFloyd。

不过Floyd的复杂度是 O(n3)O(n^3),显然难以接受。

当图比较稀疏的时候,即点数 nn 与边数 mm 同阶时,JohnsonJohnson 算法便有了用武之地。

全源最短路径求解其实是单源最短路径的推广,求解单源最短路径的两种算法时间复杂度分别为:

DijkstraDijkstra 单源最短路径算法:时间复杂度为 O(E+VlogV)O(E + VlogV) ,要求权值非负;

Bellman−FordBellman-Ford单源最短路径算法:时间复杂度为O(VE)O(VE) ,适用于带负权值情况;

如果对全图顶点遍历,使用 DijkstraDijkstra 算法,时间复杂度将变成 O(VE+V2logV)O(VE+V^2logV),看起来优于 FloydFloyd 算法的 O(V3)O(V^3) 。不过,DijkstraDijkstra 算法要求权值重不能为负。

re−weightre-weight

以下图为例,JohnsonJohnson 算法对下图进行 re−weightre-weight 操作,使权重不为负,并且 re−weightre-weight 后,计算出来的最短路径仍然正确。

接下来重新计算新增顶点到其它顶点的最短路径,利用单源最短路径算法,图中存在负权重节点,使用 bellmanfordbellman ford (SPFASPFA)算法,计算新增节点到其它节点的最短路径 h[]h[],然后使用如下公式对所有边的权值进行"re−weight""re-weight":

w(u,v)+(h[u]−h[v])w(u, v) + (h[u] - h[v])

现在除新增结点外,其它结点的相关边权重值都已经为正数了,可以将新增结点删除,对其它结点使用 DijkstraDijkstra算法了。

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define int long long
using namespace std;
typedef long long ll;

const int INF=1e9;
const int mod=1e9+7;
const int N=1e4+5;

struct node
{
	int to;ll dis;
	node(int to,ll dis):to(to),dis(dis){}
	bool operator<(const node& a)const{
		return dis>a.dis;
	}
};

vector<node> G[N];
int n,m,f[N],cnt[N];
bool inq[N];
queue<int> q;

bool SPFA(void)
{
	for(int i=1;i<=n;i++)
	{
		G[0].push_back(node(i,0));
		f[i]=INF;
	}
	inq[0]=true;
	q.push(0);
	while(!q.empty())
	{
		int u=q.front();q.pop();
		inq[u]=false;
		for(int i=0;i<(int)G[u].size();i++)
		{
			node& e=G[u][i];
			if(f[e.to]>f[u]+e.dis)
			{
				f[e.to]=f[u]+e.dis;
				if(!inq[e.to])
				{
					q.push(e.to);
					inq[e.to]=true;
					if(++cnt[e.to]>n)
						return false;
				}
			}
		}
	}
	return true;
}

ll d[N];
bool done[N];
priority_queue<node> pq;

ll dijkstra(int s)
{
	for(int i=1;i<=n;i++)
	{
		d[i]=INF;
		done[i]=false;
	}
	d[s]=0;
	pq.push(node(s,0));
	while(!pq.empty())
	{
		int u=pq.top().to;pq.pop();
		if(done[u])continue;
		done[u]=true;
		for(int i=0;i<(int)G[u].size();i++)
		{
			node& e=G[u][i];
			if(!done[e.to] && d[u]!=INF)
			{
				if(d[e.to]>d[u]+e.dis-(f[e.to]-f[u]))
				{
					d[e.to]=d[u]+e.dis-(f[e.to]-f[u]);
					pq.push(node(e.to, d[e.to]));
				}
			}
		}
	}
	ll sum=0;
	for(int i=1;i<=n;++i) 
	{
		if(d[i]!=INF)
			sum+=i*(d[i]-(f[s]-f[i]));
		else 
			sum+=(ll)i*INF;
	}
	return sum;
}

inline int read()
{
	int a=0,f=1;
	char ch=getchar();
	for(;!isdigit(ch);ch=getchar())
		if(ch=='-')f=-1;
	for(;isdigit(ch);ch=getchar())
		a=(a<<3)+(a<<1)+ch-'0';
	return a*f;
}

signed main()
{
	n=read();m=read();
	while(m--)
	{
		int u=read(),v=read(),w=read();
		G[u].push_back(node(v,w));
	}
	if(SPFA())
		for(int i=1;i<=n;i++)
			printf("%lld\n",dijkstra(i));
	else puts("-1");
	return 0;
}

正确性证明

我们新建一个虚拟节点(在这里我们就设它的编号为 00 )。从这个点向其他所有点连一条边权为 00 的边。

接下来用 Bellman−FordBellman-Ford 算法求出从 00 号点到其他所有点的最短路,记为 hih_i。

假如存在一条从 uu 点到 vv 点,边权为 ww 的边,则我们将该边的边权重新设置为 w+hu−hvw+h_u-h_v。

在重新标记后的图上,从 ss 点到 tt 点的一条路径 s−>p1−>p2−>s->p_1->p_2->……−>pk−>t->p_k->t的长度表达式如下:

(w(s,p1)+hs−hp1)+(w(p1,p2)+hp1−hp2)+(w(s,p_1)+h_s-h_p1)+(w(p_1,p_2)+h_p1-h_p2)+……+(w(pk,t)+hpk−ht)+(w(p_k,t)+h_pk-h_t)

化简后得到:

w(s,p1)+w(p1,p2)+w(s,p_1)+w(p_1,p_2)+……+w(pk,t)+hs−ht+w(p_k,t)+h_s-h_t

无论我们从 ss 到 tt 走的是哪一条路径,hs−hth_s-h_t 的值是不变的,于是上面新图中 s−>ts->t 的最短路变为了两部分。

根据三角形不等式,图上任意一边 (u,v)(u,v) 上两点满足:hv<=hu+w(u,v)h_v<=h_u+w(u,v)。这条边重新标记后的边权为 w′(u,v)=w(u,v)+hu−hvw'(u,v)=w(u,v)+h_u-h_v。这样我们证明了新图上的边权均非负.

原文