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

AcWing 1128. 信使【单源最短路】    原题链接    简单

作者: 作者的头像   一只野生彩色铅笔 ,  2022-01-03 16:33:51 ,  所有人可见 ,  阅读 1871


21


2

提高课题解补全计划

题目描述

给定 $n$ 个 点 和 $m$ 条 边

起点 编号为 $1$

求从起点出发,抵达所有点的 最短距离的最大值

分析

也是一道 ” 单源最短路 “问题

点数为 $100$,边数为 $200$,朴素版 和 堆优化版 的 Dijkstra 都可

朴素版 Dijkstra 的运算上界为 $10000$

对优化办 Dijkstra 的运算上界为 $1992$

Code(朴素版)

时间复杂度: $O(n^2)$

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

using namespace std;

const int N = 110;

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

int dijkstra()
{
    int res = 0;
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 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;
        res = max(res, dist[t]);
        for (int j = 1; j <= n; j ++ )
            dist[j] = min(dist[j], dist[t] + g[t][j]);
    }
    return res == 0x3f3f3f3f ? -1 : res;
}
int main()
{
    memset(g, 0x3f, sizeof g);
    scanf("%d%d", &n, &m);
    while (m -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        g[a][b] = g[b][a] = min(g[a][b], c);
    }
    cout << dijkstra() << endl;
    return 0;
}

Code(堆优化版)

时间复杂度: $O(m\log n)$

#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 = 110, M = N << 2;

int n, m;
int h[N], e[M], w[M], ne[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 ++ ;
}
int dijkstra()
{
    int res = 0, cnt = 0;

    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<>> heap;
    heap.push({0, 1});
    while (!heap.empty())
    {
        PII u = heap.top();
        heap.pop();

        if (st[u.y]) continue;
        st[u.y] = true;
        res = max(res, u.x);
        cnt ++ ;

        for (int i = h[u.y]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[u.y] + w[i])
            {
                dist[j] = dist[u.y] + w[i];
                heap.push({dist[j], j});
            }
        }
    }
    return cnt == n ? res : -1;
}
int main()
{
    memset(h, -1, sizeof h);
    scanf("%d%d", &n, &m);
    while (m -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c), add(b, a, c);
    }
    cout << dijkstra() << endl;
    return 0;
}

4 评论


用户头像
jatro13   2022-11-15 21:13      2    踩      回复

👍


用户头像
incra   2022-11-17 15:01      1    踩      回复

堆优化版打错了吧hh

用户头像
RiL在路上   2023-04-30 10:36         踩      回复

好像是可以ac的

用户头像
incra   2023-04-30 10:57    回复了 RiL在路上 的评论         踩      回复

那是我见识少了hh


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

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