• 洛谷P4779 【模板】单源最短路径(标准版)


    题意

    。。。求最短路

    Sol

    前几天写dijkstra的时候没打vis标记居然A了,然后天真的我就以为Dijkstra不用打标记。

    事实上dijkstra真的不用打标记,只不过会被卡成SPFA的复杂度

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<ext/pb_ds/priority_queue.hpp>
    #define MP(x, y) make_pair(x, y)
    #define Pair pair<int, int> 
    using namespace std;
    const int MAXN = 1e6 + 10, INF = 2147483646, B = 19;
    inline int read() {
        char c = getchar(); int x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = 1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
        return x * f;
    }
    int N, M, S;
    struct Edge {
        int u, v, w, nxt;
    }E[MAXN];
    int head[MAXN], num = 1;
    void AddEdge(int x, int y, int z) {
        E[num] = (Edge) {x, y, z, head[x]};
        head[x] = num++;
    }
    int dis[MAXN], vis[MAXN];
    priority_queue<Pair> q; 
    void Dij() {
        for(int i = 1; i <= N; i++) dis[i] = 2147483647;
        dis[S] = 0; q.push(MP(0, S));
        while(!q.empty()) {
            int p = q.top().second; q.pop();
            if(vis[p]) continue; vis[p] = 1;
            for(int i = head[p]; i != -1; i = E[i].nxt) {
                int to = E[i].v;
                if(dis[to] > dis[p] + E[i].w) 
                    dis[to] = dis[p] + E[i].w,
                    q.push(MP(-dis[to], to));
            }
        }
        for(int i = 1; i <= N; i++)
            printf("%d ", dis[i]);
    }
    main() {
        memset(head, -1, sizeof(head));
        N = read(); M = read(); S = read();
        for(int i = 1; i <= M; i++) {
            int x = read(), y = read(), z = read();
            AddEdge(x, y, z);
        }
        Dij();
        return 0;
    }
  • 相关阅读:
    【机器学习】scikit-learn中的特征选择小结
    【机器学习】scikit-learn中的数据预处理小结(归一化、缺失值填充、离散特征编码、连续值分箱)
    【机器学习】随机森林原理与调参小结
    用find命令巧查目录下文件的个数
    git
    数据库删除主键
    Linux安装JDK
    计算机进制
    java虚拟机故障处理工具
    线程的六种状态
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9358074.html
Copyright © 2020-2023  润新知