最短路径 Dijkstra+priority_queue优化+bellman_ford+Floyd

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 1005;
const int inf = 0x3f3f3f3f;
struct HeapNode {
    int d, u;
    HeapNode() = default;
    HeapNode(int a, int b) :d(a), u(b) {}
    bool operator<(const HeapNode& b)const {
        return d > b.d;
    }
};
struct Edge {
    int to, dist;
    Edge(int v,int d):to(v),dist(d){}
};
int arc[maxn][maxn],vis[maxn],n,d[maxn],pre[maxn],cnt[maxn],f[maxn][maxn];
//vector表示图
vector<Edge> G[maxn];
//
void Dij(int s) {
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < n; ++i)
        d[i] = inf;
    d[s] = 0;
    for (int i = 0; i < n; ++i) {
        int x, m = inf;
        for (int y = 0; y < n; ++y)
            if (!vis[y] && d[y] <= m) m = d[x = y];
        vis[x] = 1;
        //邻接矩阵的写法
        /*for (int y = 0; y < n; ++y) d[y] = min(d[y], d[x] + arc[x][y]);*/
        //vector表示图的写法
        for (auto &y : G[x])
            d[y.to] = min(d[y.to], d[x] + y.dist), pre[y.to] = x;
    }
}
void Dij_pri(int s) {
    memset(vis, 0, sizeof(vis));
    priority_queue <HeapNode> Q;
    for (int i = 0; i < n; ++i)
        d[i] = inf;
    d[s] = 0;
    Q.push(HeapNode(0, s));
    while (!Q.empty()) {
        HeapNode x = Q.top(); Q.pop();
        if (x.d != d[x.u]) continue;//由于priority_queue不提供修改优先级操作,故冗余加入,老的跳过
        for (auto &y : G[x.u]) {
            if (d[y.to] > d[x.u] + y.dist)
                d[y.to] = d[x.u] + y.dist, pre[y.to] = x.u,Q.push(HeapNode(d[y.to],y.to));
        }
    }
}
bool bellman_ford(int s) {
    queue<int> Q;
    memset(vis, 0, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < n; ++i)
        d[i] = inf;
    d[s] = 0, vis[s] = 1, Q.push(s);
    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        vis[u] = 0;
        for (auto y : G[u]) {
            if (d[u]<inf && d[y.to]>d[u] + y.dist) {
                d[y.to] = d[u] + y.dist, pre[y.to] = u;
                if (!vis[y.to]) {
                    Q.push(y.to), vis[y.to] = 1;
                    if (++cnt[y.to] > n) return false;
                }
            }
        }
    }
    return true;
}
void Floyd() {
    memcpy(f, arc, sizeof(arc));
    for (int k = 0; k < n; ++k)
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
int main() {
    n = 5;
    G[0].push_back(Edge(1, 1));
    G[1].push_back(Edge(2, 1)), G[1].push_back(Edge(3, 3));
    G[2].push_back(Edge(4, 3)), G[2].push_back(Edge(3, 1));
    G[3].push_back(Edge(4, 2));
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            arc[i][j] = i==j?0:inf;
    arc[0][1] = arc[1][2] = arc[2][3] = 1;
    arc[3][4] = 2;
    arc[1][3] = arc[2][4] = 3;
    Dij(0);
    Dij_pri(0);
    bellman_ford(0);
    Floyd();
    cout << 5;
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/Bendaai/article/details/79283545
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞