• 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;
    }
    
  • 相关阅读:
    第17章—前端分页(Bootstrap-Table)
    第16章—日志(slf4j&logback)
    第15章—数据库连接池(DBCP2)
    第14章—数据库连接池(C3P0)
    第13章—数据库连接池(Druid)
    第00章—IDEA
    第12章—整合Redis
    第11章—常用注解(持续更新中)
    获取图片的宽高
    button按钮文字超出范围后省略号位置设置
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10943235.html
Copyright © 2020-2023  润新知