最短路模板大合集

7.17更:

昨天spfa堆优化写错了,重发一下

#include <iostream>
#include <queue>
#include <utility>
#include <functional>
#include <cstring>
#include <stack>
using namespace std;
struct Edge
{
    long long u, v, w, nxt;
}edge[5000001];
long long head[5000001], cnt, n, m, s, u, v, w, dis[5000001], vis[5000001];
void init()
{
    for(int i = 1;i <= n;++i)
        dis[i] = 2147483647;
    dis[s] = 0;
}
void ford(int s)
{
    init();
    for(int i = 1;i < n;++i)
        for(int j = 1;j <= m;++j)
            dis[edge[j].v] = min(dis[edge[j].v], dis[edge[j].u] + edge[j].w);
}
void spfa(int s)
{
    init();queue<int> q;q.push(s);
    while(!q.empty())
    {
        int u = q.front();q.pop();
        for(int i = head[u];i;i = edge[i].nxt)
            if(dis[edge[i].v] > dis[u] + edge[i].w)
            {
                dis[edge[i].v] = dis[u] + edge[i].w;
                q.push(edge[i].v);
            }
    }
}
void spfa_heap(int s)
{
    init();
    priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
    q.push(make_pair(0, s));
    while(!q.empty())
    {
        int u = q.top().second;q.pop();
        for(int i = head[u];i;i = edge[i].nxt)
            if(dis[edge[i].v] > dis[u] + edge[i].w)
            {
                dis[edge[i].v] = dis[u] + edge[i].w;
                q.push(make_pair(dis[edge[i].v], edge[i].v));
            }
    }
}
void spfa_dfs(int s)
{
    init();stack<int> q;q.push(s);
    while(!q.empty())
    {
        int u = q.top();q.pop();
        for(int i = head[u];i;i = edge[i].nxt)
            if(dis[edge[i].v] > dis[u] + edge[i].w)
            {
                dis[edge[i].v] = dis[u] + edge[i].w;
                q.push(edge[i].v);
            }
    }
}
void spfa_slf(int s)
{
    init();deque<int> q;q.push_back(s);
    while(!q.empty())
    {
        int u = q.front();q.pop_front();
        for(int i = head[u];i;i = edge[i].nxt)
            if(dis[edge[i].v] > dis[u] + edge[i].w)
            {
                dis[edge[i].v] = dis[u] + edge[i].w;
                if(!q.empty() && dis[edge[i].v] < dis[q.front()])
                    q.push_front(edge[i].v);
                else q.push_back(edge[i].v);
            }
    }
}
void dijkstra(int s)
{
    init();memset(vis, 0, sizeof vis);
    priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
    q.push(make_pair(0, s));dis[s] = 0;
    while(!q.empty())
    {
        int u = q.top().second;q.pop();
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = head[u];i;i = edge[i].nxt)
            if(dis[edge[i].v] > dis[u] + edge[i].w)
            {
                dis[edge[i].v] = dis[u] + edge[i].w;
                q.push(make_pair(dis[edge[i].v], edge[i].v));
            }
    }
}
int main()
{
    cin >> n >> m >> s;
    for(int i = 0;i < m;++i)
    {
        cin >> u >> v >> w;++cnt;
        edge[cnt].u = u;
        edge[cnt].v = v;
        edge[cnt].w = w;
        edge[cnt].nxt = head[u];
        head[u] = cnt;
    }
    //在这里输入一种最短路算法
    for(int i = 1;i <= n;++i)
        cout << dis[i] << " ";
    return 0;
}
posted @ 2021-07-16 17:22  5k_sync_closer  阅读(4)  评论(0编辑  收藏  举报  来源