Dijkstra堆优化板子

struct Edge{
    int to, next;
    int w;
}edge[MAXM];
struct qnode{
    int u;
    int c;
    qnode(int _u = 0, int _c = 0):u(_u), c(_c){}
    bool operator < (const qnode &r) const{
        return r.c < c;
    }
};
int tot, head[maxn], vis[maxn];
int dis[maxn];
void addEdge(int u, int v, int w){
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].next = head[u];
    head[u] = tot++;
}
void Dijkstra(int n, int st){
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i <= n; i++) dis[i] = INF;
    priority_queue que;
    while(!que.empty()) que.pop();
    dis[st] = 0;
    que.push(qnode(st, 0));
    qnode temp;
    while(!que.empty()){
        temp = que.top();
        que.pop();
        int u = temp.u;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = head[u]; i != -1; i = edge[i].next){
            int v = edge[i].to;
            int w = edge[i].w;
            if(!vis[v] && dis[v] > dis[u] + w){
                dis[v] = dis[u] + w;
                que.push(qnode(v, dis[v]));
            }
        }
    }
}

转载于:https://www.cnblogs.com/KirinSB/p/11335601.html

你可能感兴趣的:(Dijkstra堆优化板子)