各种求最短路问题

文字性复习:

Dijkstra-朴素O(n^2)

    初始化距离数组, dist[1] = 0, dist[i] = inf;
    for n次循环 每次循环确定一个min加入S集合中,n次之后就得出所有的最短距离
    将不在S中dist_min的点->t
    t->S加入最短路集合
    用t更新到其他点的距离

Dijkstra-堆优化O(mlogm)

    利用邻接表,优先队列
    在priority_queue[HTML_REMOVED], greater[HTML_REMOVED] > heap;中将返回堆顶
    利用堆顶来更新其他点,并加入堆中类似宽搜

Bellman_fordO(nm)

    注意连锁想象需要备份, struct Edge{inta,b,c} Edge[M];
    初始化dist, 松弛dist[x.b] = min(dist[x.b], backup[x.a]+x.w);
    松弛k次,每次访问m条边

Spfa O(n)~O(nm)

    利用队列优化仅加入修改过的地方
    for k次
    for 所有边利用宽搜模型去优化bellman_ford算法
    更新队列中当前点的所有出边

Floyd O(n^3)

    初始化d
    k, i, j 去更新d

无负权边

无负权边的稠密图——朴素版dijkstra

基本思路:

n为点数,m为边数。

遍历n - 1遍,每次遍历寻找从1 到 t的距离的最小值,用该t点到1的距离更新各边长度,dist[n]就为最短路长度

时间复杂度:O(n2)

#include <bits/stdc++.h>
using namespace std;

const int N = 510;
const int M = 1e5 + 10;

int n, m;
int g[N][N];//图
int dist[M];//从1到i的最短长度
bool st[N];//判断每个点是否已经选过

int dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;

    for(int i = 1; i <= n - 1; i ++)
    {
        int t = -1;
        for(int j = 1; j <= n; j ++)
        {
            if(!st[j] && (t == -1 || dist[t] > dist[j]))//选择最短的dist
                t = j;
        }

        st[t] = true;

        for(int j = 1; j <= n; j ++)//用t的dist更新各点到1的距离
            dist[j] = min(dist[j], dist[t] + g[t][j]);
    }

    if(dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main()
{
    memset(g, 0x3f, sizeof g);

    cin >> n >> m;

    while(m --)
    {
        int a, b, w;
        cin >> a >> b >> w;

        g[a][b] = min(g[a][b], w);//防止重边和自环,取其最小值
    }

    cout << dijkstra() << endl;
}

 无负权边的稀疏图——堆优化版dijkstra

基本思路:

用priority_queue可以实现根据dist的排序,从而不需要查找过程。

时间复杂度:O(mlog(n))

#include <bits/stdc++.h>
using namespace std;

const int N = 2 * 1e5 + 10;

typedef pair<int, int> PII;

int n, m;
int e[N], ne[N], h[N], w[N], idx;
bool st[N];
int dist[N];

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

int dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;

    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1});

    while(heap.size())
    {
        auto t = heap.top();
        heap.pop();
        int ver = t.second, distance = t.first;

        if(st[ver]) continue;
        st[ver] = true;

        for(int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];

            if(dist[j] > distance + w[i])
            {
                dist[j] = distance + w[i];
                heap.push({dist[j], j});
            }
        }
    }

    if(dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main()
{
    memset(h, -1, sizeof h);

    cin >> n >> m;

    while(m --)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }

    cout << dijkstra() << endl;
}

有负权边

有边数限制的最短路(可用负权回路)——bellman-ford算法(用struct存图)

基本思路:

每次循环,每个点都根据自身的dist更新该点指向的点的dist,循环多少次。

时间复杂度:O(nm)

#include <bits/stdc++.h>
using namespace std;

const int N = 510;
const int M = 1e5 + 10;

struct Edge{
    int a, b, c;
}edges[M];

int n, m, k;
int dist[N], last[N];

int bellman_ford()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;

    for(int i = 0; i < k; i ++)//求k条边的最短路,遍历k次
    {
        memcpy(last, dist, sizeof dist);//因为如果不复制一编,在接下来的遍历中,dist[j - 1]可能已经被改变,从而导致dist[j]的值不对,
        for(int j = 1; j <= m; j ++)
        {
            auto e = edges[j];
            dist[e.b] = min(dist[e.b], last[e.a] + e.c);
        }
    }
    return 0;
}

int main()
{
    cin >> n >> m >> k;
    for(int i = 1; i <= m; i ++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        edges[i] = {a, b, c};
    }

    bellman_ford();
    if(dist[n] > 0x3f3f3f3f / 2) cout << "impossible" << endl;//除以2是因为有负权边,每次更新中会使得本来是INF的dist比dist稍小,所以/2来防止判断出错
    else cout << dist[n] << endl;
}

 

无负权回路的最短路——SPFA算法

基本思路:(转自ACWING海绵宝宝)

建立一个队列,初始时队列里只有起始点。

再建立一个数组记录起始点到所有点的最短路径(该表格的初始值要赋为极大值,该点到他本身的路径赋为0)。

再建立一个数组,标记点是否在队列中。

队头不断出队,计算始点起点经过队头到其他点的距离是否变短,如果变短且被点不在队列中,则把该点加入到队尾。

重复执行直到队列为空。

在保存最短路径的数组中,就得到了最短路径。

时间复杂度:SPFA一般情况复杂度是O(m) 最坏情况下复杂度和朴素 Bellman-Ford 相同,为O(nm)。

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int n, m;
int e[N], ne[N], h[N], w[N], idx;
bool st[N];
int dist[N];

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

void spfa()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    queue<int> q;
    q.push(1);
    st[1] = true;

    while(q.size())
    {
        int t = q.front();
        q.pop();
        st[t] = false;

        for(int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                if(!st[j])
                {
                    st[j] = true;
                    q.push(j);
                }
            }
        }
    }
}

int main()
{
    memset(h, -1, sizeof h);

    cin >> n >> m;

    while(m --)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }

    spfa();

    if(dist[n] == 0x3f3f3f3f) cout << "impossible" << endl;
    else cout << dist[n] << endl;
}

SPFA判断负环

基本思路:

创建一个计数器cnt,每次更新dist时+1,若cnt >= n,则说明该路存在n条边,从此可以推出存在负环

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int n, m;
int e[N], ne[N], w[N], h[N], idx;
int dist[N], cnt[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 ++;
}

bool spfa()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;

    queue<int> q;
    for(int i = 1; i <= n; i ++)
    {
        st[i] = true;
        q.push(i);
    }

    while(q.size())
    {
        int t = q.front();
        q.pop();

        st[t] = false;

        for(int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];

            if(dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                cnt[j] = cnt[t] + 1;
                if(cnt[j] >= n) return true;
                if(!st[j])
                {
                    st[j] = true;
                    q.push(j);
                }
            }
        }
    }

    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);
    }
    
    int flag = spfa();
    if(flag) cout << "Yes" << endl;
    else cout << "No" << endl;
}

不存在负权回路的多源做短路——Floyd算法

基本思路:

dp思想,三重循环找最小值

#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 210, INF = 1e9;

int n, m, Q;
int d[N][N];

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

int main()
{
    scanf("%d%d%d", &n, &m, &Q);

    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;
        scanf("%d%d%d", &a, &b, &c);
        d[a][b] = min(d[a][b], c);
    }

    floyd();

    while (Q -- )
    {
        int a, b;
        scanf("%d%d", &a, &b);

        int t = d[a][b];
        if (t > INF / 2) puts("impossible");
        else printf("%d\n", t);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值