返回顶部

模板 - 单源最短路Dijkstra

int dis[105];
bool vis[105];

priority_queue<pair<int, int> > pq;
void dijkstra(int s, int n) {
    while(!pq.empty())
        pq.pop();
    for(int i = 1; i <= n; ++i) {
        dis[i] = 0x3f3f3f3f;
        vis[i] = 0;
    }
    dis[s] = 0;
    pq.push({-dis[s], s});
    while(!pq.empty()) {
        int u = pq.top().second;
        pq.pop();
        if(vis[u])
            continue;
        vis[u] = 1;
        for(int i = 0; i < G[u].size(); ++i) {
            int v = G[u][i].first, w = G[u][i].second;
            if(!vis[v] && dis[v] > dis[u] + w) {
                dis[v] = dis[u] + w;
                pq.push({-dis[v], v});
            }
        }
    }
}
posted @ 2019-10-21 18:57  Inko  阅读(...)  评论(...编辑  收藏