zoukankan      html  css  js  c++  java
  • P4779 【模板】单源最短路径

    P4779 【模板】单源最短路径(标准版

    #include <bits/stdc++.h>
    using namespace std;
    const int nn = 1e5 + 5;
    const int mm = 2e5 + 5;
    int d[nn],head[nn],ver[mm],edge[mm],nxt[mm],tot;
    bool v[nn];
    int n,m,s;
    priority_queue<pair<int,int> >  que;
    
    void add(int x,int y,int z){
        ver[++tot] = y;edge[tot] = z;//这条边的终点和边权
        nxt[tot] = head[x];head[x] = tot;//相当于指针
    }
    void dijstra(){
        memset(d,0x3f,sizeof(d));
        d[s] = 0;
        que.push(make_pair(0,s));
    
        while(!que.empty()){
            int x = que.top().second;//表示节点编号
            que.pop();
            if(v[x]) continue;
            v[x] = 1;
            //遍历所有出边
            for(int i = head[x];i;i=nxt[i]){
                int y = ver[i],z = edge[i];
                if(d[y] > d[x] + z){
                    d[y] = d[x] + z;
                    que.push(make_pair(-d[y],y));
                }
            }
        }
    }
    int main(){
        //freopen("in","r",stdin);
        ios::sync_with_stdio(0);
        cin >> n >> m >> s;
        while(m--){
            int x,y,w;
            cin >> x >> y >> w;
            add(x,y,w);
        }
    
        dijstra();
        for(int i = 1; i <= n; i++){
            if(i == 1) cout << d[i];
            else cout << " " << d[i];
        }
        return 0;
    }
    View Code
  • 相关阅读:
    union
    大端和小端
    迭代器
    STL
    动态内存管理2
    动态内存管理
    关于 FlexBox
    CSS的居中问题
    操作符
    JavaScript介绍
  • 原文地址:https://www.cnblogs.com/xcfxcf/p/12301565.html
Copyright © 2011-2022 走看看