图论——Johnson全源最短路

· · 个人记录

Johnson 全源最短路

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

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

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

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

$Bellman-Ford$单源最短路径算法:时间复杂度为$O(VE)$ ,适用于带负权值情况; 如果对全图顶点遍历,使用 $Dijkstra$ 算法,时间复杂度将变成 $O(VE+V^2logV)$,看起来优于 $Floyd$ 算法的 $O(V^3)$ 。不过,$Dijkstra$ 算法要求权值重不能为负。 ### $re-weight

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

接下来重新计算新增顶点到其它顶点的最短路径,利用单源最短路径算法,图中存在负权重节点,使用

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

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

#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;
}

正确性证明

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

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

假如存在一条从 u 点到 v 点,边权为 w 的边,则我们将该边的边权重新设置为 w+h_u-h_v。

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

(w(s,p_1)+h_s-h_p1)+(w(p_1,p_2)+h_p1-h_p2)+$……$+(w(p_k,t)+h_pk-h_t)

化简后得到:

w(s,p_1)+w(p_1,p_2)+$……$+w(p_k,t)+h_s-h_t

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

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