很简洁干净的堆优化版,为什么只对后两个?

P4779 【模板】单源最短路径(标准版)

kmg_is_so_cute @ 2024-03-27 20:38:57

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;
const int N = 9000100;
long long h[N], e[N], ne[N], idx, w[N];
long long  dist[N] , f[N];
int n, m, s , a , b , c;

typedef pair<long long, long long>pp;

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

void dijkstra(int u)
{
    dist[u] = 0;
    priority_queue< pp, vector<pp>, greater<pp> >q;
    q.push({ u , 0 });

    while (q.size())
    {
        pp t = q.top();
        q.pop();

        long long dis = t.second , n = t.first;

        if (f[n] || dis > dist[n]) continue;
        f[n] = 1;

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

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

    cin >> n >> m >> s;

    for (int i = 1; i <= n; i++)dist[i] = 1e10;

    while (m--)
    {
        scanf("%d %d %d", &a, &b, &c);
        add(a, b, c);
    }

    dijkstra(s);

    for (int i = 1; i <= n; i++)printf("%lld ", dist[i]);

    return 0;
}

by s1mp1ng @ 2024-03-27 20:46:29

存反了吧,你这 pp 不是先排 first 再排 second 的吗


by s1mp1ng @ 2024-03-27 20:48:51

我帮你反过来就过了: 记录


by Mostly_Harmless @ 2024-03-28 00:45:35

@s1mp1ng 还是好人多啊


|