zoukankan      html  css  js  c++  java
  • 2018 Multi-University Training Contest 7

    dijkstra

    应该是签到题了。。最短路裸题,优先队列维护就行了

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    #define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 100005;
    int n, m, cnt, head[N], dist[N];
    bool vis[N];
    struct Edge { int v, next, id;} edge[N<<2];
    struct Node{
        int s, dis, id;
        Node(int s, int dis, int id): s(s), dis(dis), id(id){}
        bool operator < (const Node &rhs) const {
            return dis > rhs.dis;
        }
    };
    
    void addEdge(int a, int b, int c){
        edge[cnt].v = b, edge[cnt].next = head[a], edge[cnt].id = c, head[a] = cnt ++;
    }
    
    int dijkstra(){
        full(dist, INF), full(vis, false);
        priority_queue<Node> pq;
        dist[1] = 0, pq.push(Node(1, dist[1], 0));
        while(!pq.empty()){
            Node cur = pq.top(); pq.pop();
            if(vis[cur.s]) continue;
            vis[cur.s] = true;
            for(int i = head[cur.s]; i != -1; i = edge[i].next){
                int u = edge[i].v;
                if(dist[u] > cur.dis + (cur.id != edge[i].id)){
                    dist[u] = cur.dis + (cur.id != edge[i].id);
                    pq.push(Node(u, dist[u], edge[i].id));
                }
            }
        }
        return dist[n];
    }
    
    int main(){
    
        FAST_IO;
        while(cin >> n >> m){
            full(head, -1), cnt = 0;
            for(int i = 0; i < m; i ++){
                int u, v, id;
                cin >> u >> v >> id;
                addEdge(u, v, id), addEdge(v, u, id);
            }
            int ans = dijkstra();
            if(ans == INF) cout << "-1" << endl;
            else cout << ans << endl;
        }
        return 0;
    }
    
  • 相关阅读:
    如何在linux下使用sudo命令不用输入密码
    QT读写ini配置文件
    dubbo的灰度发布
    dubbo的重试原则
    dubbo的启动时检查
    dubbo的超时处理和配置覆盖
    dubbo配置文件加载顺序
    java基础-泛型的优点
    idea提交多个项目到github的同一个repository
    【掌握】dubbo环境搭建linux
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10943235.html
Copyright © 2011-2022 走看看