zoukankan      html  css  js  c++  java
  • 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<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;
                int w = edge[i].w;
                if(!vis[v] && dis[v] > dis[u] + w){
                    dis[v] = dis[u] + w;
                    que.push(qnode(v, dis[v]));
                }
            }
        }
    }
    
  • 相关阅读:
    python虚拟环境--virtualenv
    python使用smtplib发送邮件
    python网络编程
    python操作MySQL数据库
    python面向对象
    python内置函数总结
    python异常处理
    python文件I/O
    python模块
    python函数
  • 原文地址:https://www.cnblogs.com/KirinSB/p/11335601.html
Copyright © 2011-2022 走看看