栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

dijkstra(朴素版+堆优化版)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

dijkstra(朴素版+堆优化版)

原理:

1.每次从未标记的节点中选择距离出发最近的节点,标记,收录到最优路径的集合中
 2.计算刚加入节点a的邻近节点b的距离,并根据条件更新节点b的距离

dijkstra堆优化版

#include
using namespace std;

const int N = 3000, M = 20000;

typedef pair PII;

int h[N], w[M], e[M], ne[M], idx;
bool ste[N];
priority_queue, greater> heap;

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

int dijkstra(int st, int ed)
{
    int dist[M];
    memset(dist, 0x3f, sizeof dist);
    dist[st] = 0;
    //ste[st] = true;
    heap.push({ 0,st });
    while (heap.size())
    {
        auto t = heap.top();
        heap.pop();
        //ste[t.second] = false;
        //int distance=t.first,res=t.second;
        for (int i = h[t.second]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > t.first + w[i])
            {
                dist[j] = t.first + w[i];
                heap.push({ dist[j],j });
            }
        }
    }
    return dist[ed];
}

int main()
{
    int n, m, st, ed;
    cin >> n >> m >> st >> ed;
    memset(h, -1, sizeof h);
    for (int i = 0; i < m; i++)
    {
        int a, b, w;
        scanf("%d%d%d", &a, &b, &w);
        add(a, b, w), add(b, a, w);
    }
    int t = dijkstra(st, ed);
    cout << t << endl;
    return 0;
}

dijkstra朴素版

#include
using namespace std;

const int N = 3000;

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

int dijkstra(int s, int e)
{
    memset(dist, 0x3f, sizeof dist);
    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]);
    }
    return dist[e];
}

int main()
{
    cin >> n >> m >> s >> e;
    memset(g, 0x3f, sizeof g);
    for (int i = 0; i < m; i++)
    {
        int a, b, w;
        scanf("%d%d%d", &a, &b, &w);
        g[a][b] = g[b][a] = min(g[a][b], w);
    }
    int t = dijkstra(s, e);
    cout << t << endl;
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/297847.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号