Spfa


Spfa

  \(Spfa\) 算法的全稱是: \(Shortest\) \(Path\) \(Faster\) \(Algorithm\) ,是 \(Bellman-Ford\) 算法的隊列優化算法的別稱,通常用於求含負權邊的單源最短路徑,以及判負權環。

基本原理

  設立一個先進先出的隊列用來保存待優化的結點,優化時每次取出隊首結點 \(u\),並且用結點 \(u\) 當前的最短路徑估計值對離開結點 \(u\) 所指向的結點 \(v\) 進行松弛操作,即判斷是否有 \(dis[v] \gt dis[u]+w\)\(w\) 是連接 \(u\)\(v\) 的邊的長度),若有,則更新 \(dis[v]\)。如果結點 \(v\) 的最短路徑估計值有所調整,且結點 \(v\) 不在當前的隊列中,就將結點 \(v\) 放入隊尾。這樣不斷從隊列中取出結點來進行松弛操作,直至隊列空為止。
  \(Spfa\) 在形式上和 \(Bfs\) 非常類似,不同的是 \(Bfs\) 中一個結點出了隊列就不可能重新進入隊列,但是 \(Spfa\) 中一個結點可能在出隊列之后再次被放入隊列,也就是一個結點改進過其它的結點之后,過了一段時間可能本身被改進,於是再次將其加入隊列,再次用來改進其它的結點。
  每次將結點放入隊尾,都是經過松弛操作達到的。換言之,每次的優化將會有某個結點 \(v\) 的最短路徑估計值 \(dis[v]\) 變小。所以算法的執行會使 \(dis\) 越來越小。若圖中不存在負權環,則每個結點都有最短路徑值。因此,算法不會無限執行下去,隨着 \(dis\) 值的逐漸變小,直到到達最短路徑值時,算法結束,這時的最短路徑估計值就是對應結點的最短路徑值。
  如果一個結點進入隊列達到 \(n\) 次,則表明圖中存在負權環,沒有最短路徑。

效率分析

  在隨機圖中, \(Spfa\) 的期望時間復雜度為 \(O(KE)\) ,其中 \(K\) 是常數,代表所有結點的平均入隊次數(一般 \(K \leqslant 2\) ), \(E\) 是邊數。但往往因為出題人所造的毒瘤數據而被卡,導致復雜度退化為 \(O(VE)\) ,其中 \(V\) 是結點數。

核心代碼

ll n,m,s,cnt,head[maxn],dis[maxn];
bool vis[maxn];
struct Edge{ll u,v,w,next;}edge[maxm];
inline void add(ll u,ll v,ll w)                    /*鏈式前向星存圖*/
{
    edge[++cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt;
}
inline void Spfa()
{
    queue<ll>q;
    for(ll i=1;i<=n;i++)
        dis[i]=INF,vis[i]=0;                /*初始距離置為INF,訪問標記置為0*/
    dis[s]=0;
    q.push(s);                              /*源點入隊*/
    vis[s]=1;
    while(!q.empty())
    {
        ll u=q.front();                     /*隊首元素出隊*/
        q.pop();
        vis[u]=0;
        for(ll i=head[u];i;i=edge[i].next)  /*尋找與所有以隊首 u 為起點的邊*/
        {
            ll v=edge[i].v,w=edge[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;            /*松弛操作*/
                if(!vis[v])                 /*若終點 v 不在隊列中*/
                {
                    q.push(v);              /*入隊*/
                    vis[v]=1;
                }
            }
        }
    }
}

例題解析

洛谷 P3371 【模板】單源最短路徑(弱化版)

  給出一個有向圖 \(G=<V,E>\) ,一個源點 \(S\) ,求點 \(S\) 到圖中所有點的最短距離。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 10005
#define maxm 500005
#define INF 2147483647
template<class T>inline bool read(T &x)
{
    x=0;register char c=getchar();register bool f=0;
    while(!isdigit(c)){if(c==EOF)return false;f^=c=='-',c=getchar();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
    if(f)x=-x;
    return true;
}
template<class T>inline void print(T x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)print(x/10);
    putchar(x%10^48);
}
template<class T>inline void print(T x,char c){print(x),putchar(c);}
template<class T,class ...S>inline bool read(T &x,S &...y){return read(x)&&read(y...);}
ll n,m,s,cnt,head[maxn],dis[maxn];
bool vis[maxn];
struct Edge{ll u,v,w,next;}edge[maxm];
inline void add(ll u,ll v,ll w)
{
    edge[++cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt;
}
inline void Spfa()
{
    queue<ll>q;
    for(ll i=1;i<=n;i++)
        dis[i]=INF,vis[i]=0;
    dis[s]=0;
    q.push(s);
    vis[s]=1;
    while(!q.empty())
    {
        ll u=q.front();
        q.pop();
        vis[u]=0;
        for(ll i=head[u];i;i=edge[i].next)
        {
            ll v=edge[i].v,w=edge[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
}
int main()
{
    read(n,m,s);
    ll u,v,w;
    for(ll i=1;i<=m;i++)
    {
        read(u,v,w);
        add(u,v,w);
    }
    Spfa();
    for(ll i=1;i<n;i++)print(dis[i],' ');
    print(dis[n],'\n');
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM