AcWing
  • 首页
  • 课程
  • 题库
  • 更多
    • 竞赛
    • 题解
    • 分享
    • 问答
    • 应用
    • 校园
  • 关闭
    历史记录
    清除记录
    猜你想搜
    AcWing热点
  • App
  • 登录/注册

AcWing 1129. 热浪【单源最短路】    原题链接    简单

作者: 作者的头像   一只野生彩色铅笔 ,  2021-12-28 20:20:20 ,  所有人可见 ,  阅读 3916


37


1

提高课题解补全计划

题目描述

对于一个含有 $n$ 个点,和 $m$ 条边的 无向图,且边的权值都是 正数

求解从起点 $s$ 到 终点 $t$ 的 最短路径长度

分析

单源点,正权图,求到终点的 最短路径

经典的 单源最短路 裸题,点数的范围是 $2500$,边数的范围是 $12400$

Dijkstra 朴素版 的 时间复杂度 为 $O(n^2)$,本题运算上界是 $6.26 \times 10^6$

Dijkstra 堆优化版 的 时间复杂度 为 $O(m\log n)$,本题运算上界是 $1.36 \times 10^5$

任意选择其中之一都可

(还有,关于spfa他死了)

Code(朴素版Dijkstra)

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

using namespace std;

const int N = 2510, M = 6210;

int n, m, s, t;
int g[N][N];
int dist[N];
bool st[N];

void dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    memset(st, 0, sizeof st);
    dist[s] = 0;
    for (int i = 1; i <= n; i ++ )
    {
        int t = -1;
        for (int j = 1; j <= n; j ++ )
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        st[t] = true;
        for (int j = 1; j <= n; j ++ )
            dist[j] = min(dist[j], dist[t] + g[t][j]);
    }
}
int main()
{
    memset(g, 0x3f, sizeof g);
    cin >> n >> m >> s >> t;
    while (m -- )
    {
        int a, b, c;
        cin >> a >> b >> c;
        g[a][b] = g[b][a] = min(g[a][b], c);
    }
    dijkstra();
    cout << dist[t] << endl;
    return 0;
}

Code(堆优化版Dijkstra)

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

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;
const int N = 2510, M = 6210 << 1;

int n, m, s, t;
int h[N], e[M], ne[M], w[M], idx;
int dist[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 ++ ;
}
void dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    memset(st, 0, sizeof st);
    priority_queue<PII, vector<PII>, greater<>> heap;
    heap.push({0, s});
    dist[s] = 0;

    while (!heap.empty())
    {
        PII t = heap.top();
        heap.pop();

        st[t.y] = true;

        for (int i = h[t.y]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[t.y] + w[i])
            {
                dist[j] = dist[t.y] + w[i];
                heap.push({dist[j], j});
            }
        }
    }
}
int main()
{
    memset(h, -1, sizeof h);
    cin >> n >> m >> s >> t;
    while (m -- )
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c), add(b, a, c);
    }
    dijkstra();
    cout << dist[t] << endl;
    return 0;
}

21 评论


用户头像
H.L   2022-11-11 13:39         踩      回复

堆优化版本的st其实没用上吧,是不是在入队的时候判断一下会更优?

用户头像
_Dijkstra   2023-04-19 15:07         踩      回复

应该是作者忘记写了,st是用来标记堆顶的点是否扩散过,如果扩散过就不再扩散 continue掉 ,提高效率,虽然不用也能过但是很满


用户头像
gtt   2022-06-04 18:48         踩      回复

记录美好生活


用户头像
taez   2022-03-31 10:46         踩      回复

大佬们,spfa在这道题的最坏情况下是O(n*m)=3千多万,为什么不会被卡?一般多少才会被卡

用户头像
一只野生彩色铅笔   2022-03-31 11:32      1    踩      回复

1e8以内不会被卡

用户头像
taez   2022-04-02 09:31    回复了 一只野生彩色铅笔 的评论         踩      回复

3q


用户头像
糖豆   2022-03-11 15:51         踩      回复

彩铅巨巨,欢迎回来~


用户头像
xiuzhiyuan   2022-03-10 20:57         踩      回复

理论上来讲卡spafa的话这道题是过不了的。不建议使用。但是做法还是可以介绍一下吧?著明就可以了


用户头像
yanli   2022-01-02 14:48         踩      回复

# why M = 6210 << 1;
# because bidirection


用户头像
诗人kris   2022-01-01 17:31         踩      回复

为什么说spfa死了呢,只是在正权图会被卡吗

用户头像
一只野生彩色铅笔   2022-01-01 17:56         踩      回复

现在很多出题人会出数据卡SPFA卡成 O(nm)


用户头像
明明菜的要死   2022-01-01 11:46         踩      回复

彩铅巨巨 orz

用户头像
一只野生彩色铅笔   2022-01-01 12:09         踩      回复

你好 orz


用户头像
Hygen   2021-12-29 19:46         踩      回复

来迟哩,加油彩铅巨巨

用户头像
一只野生彩色铅笔   2021-12-29 21:21         踩      回复

谢谢 w


用户头像
科宇   2021-12-28 21:58         踩      回复

又开始啦666

用户头像
一只野生彩色铅笔   2021-12-28 22:03         踩      回复

科宇大佬好 w


用户头像
vector_int   2021-12-28 21:02         踩      回复

欢迎彩铅大佬回归!

用户头像
一只野生彩色铅笔   2021-12-28 21:21         踩      回复

谢谢 w


用户头像
Tilbur   2021-12-28 20:43         踩      回复

梦重新开始的地方

用户头像
一只野生彩色铅笔   2021-12-28 21:20         踩      回复

是的hh


App 内打开
你确定删除吗?
1024
x

© 2018-2025 AcWing 版权所有  |  京ICP备2021015969号-2
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息