最短路

dijkstra

Dijkstra求最短路 I

\(O(n^2)\)
单源最短路(Single Source Shortest Path)
题目链接

#include <bits/stdc++.h>


using namespace std;

const int N = 510, INF = 0x3f3f3f3f;
int n, m;
int g[N][N], d[N];
bool vis[N];

int dijkstra(int s) {
    memset(d, 0x3f, sizeof d);
    d[s] = 0;
    for (int i = 0; i < n - 1; i++) {
        int t = -1;
        for (int j = 1; j <= n; j++)
            if (!vis[j] && (t == -1 || d[t] > d[j])) 
    t = j;  //如果没被访问过并且是第一次访问i结点或者可优化
        for (int j = 1; j <= n; j++) 
    d[j] = min(d[j], d[t] + g[t][j]);
        vis[t] = true;
    }
    if (d[n] == INF) return -1;
    return d[n];
}

int main() {
    cin >> n >> m;
    memset(g, 0x3f, sizeof g);
    while (m--) {
        int a, b, c;
        cin >> a >> b >> c;
        g[a][b] = min(g[a][b], c);
    }
    cout << dijkstra(1) << endl;
    return 0;
}

Dijkstra求最短路 II

\(O(mlogn)\)

题目链接

#include <bits/stdc++.h>


using namespace std;

typedef pair<int, int> PII;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;
int n, m;
int h[N], w[N], ne[N], e[N], idx;
int d[N];
bool st[N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

int dijkstra(int s) {
    memset(d, 0x3f, sizeof d);
    d[s] = 0;
    priority_queue<PII, vector<PII>, greater<PII>>heap;
    heap.push({0, 1});  //距离,点
    while (heap.size()) {
        auto t = heap.top();
        heap.pop();
        int id = t.second, dist = t.first;
        if (st[id]) continue;
        st[id] = true;
        for (int i = h[id]; ~i; i = ne[i]) {
            int j = e[i];
            if (d[j] > d[id] + w[i]) {
                d[j] = d[id] + w[i];
                heap.push({d[j], j});
            }
        }
    }
    if (d[n] == INF) return -1;
    return d[n];
}

int main() {
    cin >> n >> m;
    memset(h, -1, sizeof h);
    while (m--) {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }
    cout << dijkstra(1) << endl;
    return 0;
}

bellman-ford

\(O(nm)\)
有边数限制的最短路,存在负权回路
题目链接

#include <bits/stdc++.h>


using namespace std;

const int N = 510, M = 1e4 + 10, INF = 0x3f3f3f3f;
struct Edge{
    int a, b, c;
}edge[M];
int n, m, k;
int d[N], backup[N];
bool st[N];

void bellman_ford(int s) {
    memset(d, 0x3f, sizeof d);
    d[s] = 0;
    for (int i = 0; i < k; i++) {
        memcpy(backup, d, sizeof d);
        for (int j = 0; j < m; j++) {
            auto e = edge[j];
            d[e.b] = min(d[e.b], backup[e.a] + e.c);
        }
    }
}

int main() {
    cin >> n >> m >> k;
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        edge[i] = {a, b, c};
    }
    bellman_ford(1);
    if (d[n] > INF / 2) cout << "impossible" << endl;
    else cout << d[n] << endl;
    return 0;
}

spfa

spfa求最短路

长方形图会被卡
\(O(m)\) ~ \(O(nm)\)
题目链接

#include <bits/stdc++.h>


using namespace std;

int n, m;
const int N = 1e5 + 10;
int d[N], e[N], h[N], ne[N], w[N], idx;
bool st[N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

int spfa(int s) {
    memset(d, 0x3f, sizeof d);
    d[s] = 0;
    queue<int> q;
    q.push(s);
    st[s] = true;
    while (q.size()) {
        int t = q.front();
        q.pop();
        st[t] = false;
        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if (d[j] > d[t] + w[i]) {
                d[j] = d[t] + w[i];
                if (!st[j]) {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }
    return d[n];
}

int main() {
    cin >> n >> m;
    memset(h, -1, sizeof h);
    while (m--) {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }
    int t = spfa(1);
    if (t == 0x3f3f3f3f) cout << "impossible" << endl;
    else cout << t << endl;
    return 0; 
}

spfa判断负环

\(O(m)\) ~ \(O(nm)\)
题目链接



using namespace std;

const int M = 1e4 + 10, N = 2010;
int d[N], h[N], e[M], ne[M], w[M], idx, cnt[N];
bool st[N];
int n, m;

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

bool spfa() {
    queue<int> q;
    for (int i = 1; i <= n; i++) {
        q.push(i);
        st[i] = true;
    }
    while (q.size()) {
        int t = q.front();
        q.pop();
        st[t] = false;
        for (int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if (d[j] > d[t] + w[i]) {
                d[j] = d[t] + w[i];
                cnt[j] = cnt[t] + 1;
                if (cnt[j] >= n) return true;
                if (!st[j]) {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }
    return false;
}


int main() {
    memset(h, -1, sizeof h);
    cin >> n >> m;
    while (m--) {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }
    if (spfa()) cout << "Yes" << endl;
    else puts("No");
    return 0;
}

floyd

\(O(N³)\)
题目链接

#include <bits/stdc++.h>


using namespace std;

const int N = 210, INF = 0x3f3f3f3f;
int n, m, k;
int d[N][N];

void floyd() {
    for (int t = 1; t <= n; t++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                d[i][j] = min(d[i][j], d[i][t] + d[t][j]);
}

int main() {
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (i == j) d[i][j] = 0;
            else d[i][j] = INF;
    while (m--) {
        int a, b, c;
        cin >> a >> b >> c;
        d[a][b] = min(c, d[a][b]);
    }
    floyd();
    while (k--) {
        int a, b;
        cin >> a >> b;
        int t = d[a][b];
        if (t > INF / 2) puts("impossible");
        else printf("%d\n", t);
    }
    return 0;
}

区别

- floyd (弗洛伊德算法) Dijkstra(迪杰斯特拉算法) bellman-ford(贝尔曼夫德算法) spfa
空间复杂度 O(N²) O(M) O(M) O(M)
时间复杂度 O(N³) O((m+n)logN) O(MN) 最坏也是O(NM)
适用情况 稠密图和顶点关系密切 稠密图和顶点关系密切 稀疏图和边关系密切 稀疏图和边关系密切
负权 可以 不能 可以 可以
有负权边时可否处理 可以 不能 可以 可以
判断是否存在负权回路 不能 不能 可以 可以
posted @ 2020-08-13 15:13  moonwhite1  阅读(93)  评论(0编辑  收藏  举报