Dijkstra大红大紫,求助……(样例可过)

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

rainygame @ 2023-03-25 10:41:51

堆优模板:

#include <bits/stdc++.h>
using namespace std;
#define MAXN 100001

int n, m, s, u, v, w;

struct Edge{
    int v, w;
};

struct Node{
    int u, dis;
    bool operator>(const Node a)const{
        return dis > a.dis;
    }
};

vector<Edge> e[MAXN];
int dis[MAXN];
bitset<MAXN> vis;
priority_queue<Node, deque<Node>, greater<Node>> pq;

void dijkstra(){
    dis[s] = 0;
    pq.push({s, 0});
    while (!pq.empty()){
        int u = pq.top().u;
        pq.pop();
        if (vis[u]) continue;
        vis[u] = true;
        for (auto i: e[u]){
            v = i.v;
            w = i.w;
            if (dis[v] > dis[u] + w){
                dis[v] = dis[u] + w;
                pq.push({dis[v], v});
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m >> s;

    while (m--){
        cin >> u >> v >> w;
        e[u].push_back({v, w});
    }

    memset(dis, 0x3f, sizeof(dis));
    dijkstra();

    for (int i=1; i<=n; i++) cout << dis[i] << ' ';

    return 0;
}

by Yoly_while @ 2023-03-25 10:45:41

 pq.push({dis[v], v});

改成

 pq.push({v, dis[v]});

by Yoly_while @ 2023-03-25 10:46:29

AC


|