【模板】最短路算法的优化

SPFA__SLF
SPFA在进行松弛操作的时候 肯定存在解使得答案更差 那么我们可以后考虑它们
这样就可以用双端队列来维护了

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 233333;
const int inf = 2147483647;
int tot;
int first[maxn],nxt[maxn],d[maxn];
struct edge{
    int from,to,cost;
}es[maxn];
void init()
{
    memset(first,-1,sizeof(first));
    tot=0;
}
void build(int ff,int tt,int dd)
{
    es[++tot]=(edge){ff,tt,dd};
    nxt[tot]=first[ff];
    first[ff]=tot;
}
bool vis[maxn];
deque<int>q;
void spfa(int s)
{
    d[s]=0;
    vis[s]=1;
    q.push_front(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop_front();
        vis[u]=0;
        for(int i=first[u];i!=-1;i=nxt[i])
        {
            int v=es[i].to;
            if(d[v]>d[u]+es[i].cost)
            {
                d[v]=d[u]+es[i].cost;
                if(!vis[v])
                {
                    vis[v]=1;
                    if(q.empty())
                        q.push_front(v);
                    else if(d[v]>d[q.front()])
                        q.push_back(v);
                    else
                        q.push_front(v);
                }
            }
        }
    }
}

堆优化dijkstra优先队列实现
C++内置的STL 函数优先队列 本质上是一个大根堆 首先我们需要进行重载操作
为什么是可行的呢???
因为dijkstra每次更新的时候,在没有负权的情况下,我们当前最小的答案一定是最优的,那么我们记录一下当前点的编号以及它现在的权值之和,压入一个小根堆,那么我们访问的时候就会优先访问答案更优的呢 时间复杂度也优化成了nlogn呢

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 23333;
int t,c,ts,te,first[maxn],next[maxn],d[maxn],tot;
bool vis[maxn];
struct edge{
    int from,to,cost;
}es[maxn];
struct point{
    int num,v;
};
bool operator < (point a,point b)
{
    return a.v > b.v;
}
void build(int ff,int tt,int dd)
{
    es[++tot] = (edge){ff,tt,dd};
    next[tot] = first[ff];
    first[ff] = tot;
}
priority_queue<point>q;
void dij_heap(int s)
{
    d[s] = 0;
    q.push((point){s,0});
    while(!q.empty())
    {
        point now = q.top();
        q.pop();
        int u = now.num;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = first[u];i != -1;i = next[i])
        {
            int v = es[i].to ;
            if(d[v] > d[u] + es[i].cost)
            {
                d[v] = d[u] + es[i].cost;
                q.push((point){v,d[v]});
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值