堆优前向星T#1#6求助(QwQ)

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

foglake @ 2023-05-12 18:49:40

感激不尽,蒟蒻实在做不动了。麻烦dalao们了

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, maxm = 2e5 + 10;
int dis[maxn], s[maxn], w[maxm], to[maxm], e[maxm];
priority_queue <pair<int, int> > q;
int st, n, m;
void dij() {
    dis[st] = 0;
    q.push(make_pair(0, st));
    while (!q.empty()) {
        int x = q.top().second, wei = q.top().first;
        q.pop();
        if (wei != dis[x]) continue;
        for (int i = s[x]; i; i = to[i])
            if (dis[x] + w[i] < dis[e[i]]) {
                dis[e[i]] = dis[x] + w[i];
                q.push(make_pair(dis[e[i]], e[i]));
            }
    }
}
int main() {
    scanf("%d%d%d", &n, &m, &st);
    for (int i = 1; i <= n; i++) dis[i] = 1e9;
    for (int i = 1; i <= m; i++) {
        int u, v, wei;
        scanf("%d%d%d", &u, &v, &wei);
        e[i] = v, to[i] = s[u], s[u] = i, w[i] = wei;
    }
    dij();
    for (int i = 1; i <= n; i++)
        printf("%d ", dis[i]);
}

by luo_shen @ 2023-05-12 19:28:12

您堆优怎么用大根堆啊


by luo_shen @ 2023-05-12 19:28:22

@Boenzhang


by foglake @ 2023-05-12 19:58:08

e..我悟了


by foglake @ 2023-05-12 20:03:19

感谢大佬!!


|