【模板】dijstra

typedef struct
{
    bool operator ()(const intpair& a, const intpair& b)const
    {
        return a.second > b.second;
    }
}cmp_dij;

int dis_dij[maxn];
bool vis_dij[maxn];
priority_queue<intpair, vector<intpair>, cmp_dij>q_dij;

void dijstra(int x)
{
    mem(dis_dij, 0x3f);
    mem(vis_dij, false);
    dis_dij[x] = 0;
    q_dij.push(intpair(x, dis_dij[x]));
    while (!q_dij.empty())
    {
        intpair t = q_dij.top();
        q_dij.pop();
        if (vis_dij[t.first]) continue;
        vis_dij[t.first] = true;
        for (Re int i = head[t.first]; i != -1; i = e[i].nxt)
        {
            int v = e[i].v;
            int w = e[i].w;
            if (!vis_dij[v] && dis_dij[v] > t.second + w)
            {
                dis_dij[v] = t.second + w;
                q_dij.push(intpair(v, dis_dij[v]));
            }
        }
    }
}

 

posted on 2019-10-22 13:19  thjkhdf12  阅读(148)  评论(0)    收藏  举报