Dijkstra堆优化板子

struct Edge{
    int to, next;
    ll w;
}edge[maxn << 1];
struct qnode{
    int u;
    ll c;
    qnode(int _u = 0, ll _c = 0):u(_u), c(_c){}
    bool operator < (const qnode &r) const{
        return r.c < c;
    }
};
int tot, head[maxn], big[maxn], vis[maxn], id[maxn];
ll dis[maxn], ans[maxn];
void addEdge(int u, int v, ll 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<qnode> 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;
            ll w = edge[i].w;
            if(!vis[v] && dis[v] > dis[u] + w){
                dis[v] = dis[u] + w;
                que.push(qnode(v, dis[v]));
            }
        }
    }
}
void init(){
    memset(head, -1, sizeof(head));
    tot = 0;
}

猜你喜欢

转载自www.cnblogs.com/KirinSB/p/11335601.html
今日推荐