zoukankan      html  css  js  c++  java
  • 洛谷 P4779 【模板】单源最短路径(标准版)

    传送门

    Version 1: Dijkstra

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const double pi(acos(-1));
    const int inf(0x3f3f3f3f);
    const ll _inf(0x3f3f3f3f3f3f3f3f);
    const int mod(1e9 + 7);
    const int maxn(1e5 + 10);
    const int maxm(2e5 + 10);
    int ecnt, head[maxn];
    ll dis[maxn];
    bool vis[maxn];
    
    struct edge {
        int to, wt, nxt;
    } edges[maxm];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, char c)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (c) putchar(c);
    }
    
    void addEdge(int u, int v, int w)
    {
        edges[ecnt].to = v;
        edges[ecnt].wt = w;
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    void dijkstra(int src)
    {
        priority_queue<p, vector<p>, greater<p>> q;
        q.push(p(0, src));
        dis[src] = 0;
        while (not q.empty()) {
            int u = q.top().second;
            q.pop();
            if (vis[u]) continue;
            vis[u] = true;
            for (int i = head[u]; ~i; i = edges[i].nxt) {
                int v = edges[i].to, w = edges[i].wt;
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    q.push(p(dis[v], v));
                }
            }
        }
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        memset(head, -1, sizeof head);
        memset(dis, 0x3f, sizeof dis);
        int n = read(), m = read(), s = read();
        while (m--) {
            int u = read(), v = read(), w = read();
            addEdge(u, v, w);
        }
        dijkstra(s);
        for (int i = 1; i <= n; ++i) {
            write(dis[i] == _inf ? INT_MAX : dis[i], i == n ? '
    ' : ' ');
        }
        return 0;
    }
    

    Version 2: SPFA

    SPFA 已死

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const double pi(acos(-1));
    const int inf(0x3f3f3f3f);
    const ll _inf(0x3f3f3f3f3f3f3f3f);
    const int mod(1e9 + 7);
    const int maxn(1e5 + 10);
    const int maxm(2e5 + 10);
    int ecnt, head[maxn];
    ll dis[maxn];
    bool vis[maxn];
    
    struct edge {
        int to, wt, nxt;
    } edges[maxm];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, char c)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (c) putchar(c);
    }
    
    void addEdge(int u, int v, int w)
    {
        edges[ecnt].to = v;
        edges[ecnt].wt = w;
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    void spfa(int src)
    {
        queue<int> q;
        q.push(src);
        dis[src] = 0;
        vis[src] = true;
        while (not q.empty()) {
            int u = q.front();
            q.pop();
            vis[u] = false;
            for (int i = head[u]; ~i; i = edges[i].nxt) {
                int v = edges[i].to, w = edges[i].wt;
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    if (not vis[v]) {
                        q.push(v);
                        vis[v] = true;
                    }
                }
            }
        }
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        memset(head, -1, sizeof head);
        memset(dis, 0x3f, sizeof dis);
        int n = read(), m = read(), s = read();
        while (m--) {
            int u = read(), v = read(), w = read();
            addEdge(u, v, w);
        }
        spfa(s);
        for (int i = 1; i <= n; ++i) {
            write(dis[i] == _inf ? INT_MAX : dis[i], i == n ? '
    ' : ' ');
        }
        return 0;
    }
    

    Version 3 SPFA Swap-SLF 优化

    SPFA 玄学优化 https://www.luogu.com.cn/blog/xhhkwy/spfa-hacker-orzorz

    tql

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const double pi(acos(-1));
    const int inf(0x3f3f3f3f);
    const int mod(1e9 + 7);
    const int maxn(1e5 + 10);
    const int maxm(1e6 + 10);
    int ecnt, head[maxn];
    int dis[maxn];
    bool vis[maxn];
    deque<int> q;
    
    struct edge {
        int to, wt, nxt;
    } edges[maxm];
    
    template<typename T = int>
    inline const T read()
    {
        T x = 0, f = 1;
        char ch = getchar();
        while (ch < '0' || ch > '9') {
            if (ch == '-') f = -1;
            ch = getchar();
        }
        while (ch >= '0' && ch <= '9') {
            x = (x << 3) + (x << 1) + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    
    template<typename T>
    inline void write(T x, char c)
    {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9) write(x / 10, false);
        putchar(x % 10 + '0');
        if (c) putchar(c);
    }
    
    void addEdge(int u, int v, int w)
    {
        edges[ecnt].to = v;
        edges[ecnt].wt = w;
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    void swap_slf()
    {
        if (q.size() > 2 and dis[q.front()] > dis[q.back()]) {
            swap(q.front(), q.back());
        }
    }
    
    void spfa(int src)
    {
        dis[src] = 0;
        q.push_back(src);
        vis[src] = true;
        while (not q.empty()) {
            int u = q.front();
            q.pop_front();
            swap_slf();
            vis[u] = false;
            for (int i = head[u]; compl i; i = edges[i].nxt) {
                int v = edges[i].to, w = edges[i].wt;
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    if (not vis[v]) {
                        q.push_back(v);
                        swap_slf();
                        vis[v] = true;
                    }
                }
            }
        }
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        memset(head, -1, sizeof head);
        memset(dis, 0x3f, sizeof dis);
        int n = read(), m = read(), s = read();
        while (m--) {
            int u = read(), v = read(), w = read();
            addEdge(u, v, w);
        }
        spfa(s);
        for (int i = 1; i <= n; ++i) {
            if (dis[i] == inf) {
                puts("NO PATH");
            } else {
                write(dis[i], i == n ? 10 : 32);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    python 时间等待
    python threading多线程
    c 字符串的结束标志
    c 输出是自动显示输出类型
    c 的占位符
    c数据类型
    游戏引擎
    java 数据类型
    python 读写json数据
    python 多线程_thread
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13838899.html
Copyright © 2011-2022 走看看