zoukankan      html  css  js  c++  java
  • BZOJ 2200 道路与航线 (算竞进阶习题)

    dijkstra + 拓扑排序

    这道题有负权边,但是卡了spfa,所以我们应该观察题目性质。
    负权边一定是单向的,且不构成环,那么我们考虑先将正权边连上。然后dfs一次找到所有正权边构成的联通块,将他们看成点,那么负权边和这些点就构成了一张DAG。
    对于DAG,我们可以拓扑排序,在排序的过程中,我们把入度为0的联通块里的所有点松弛一次,如果访问到联通块外的点,就让其入度减1,然后重复在拓扑排序中跑最短路的过程即可得到答案。

    输出答案的时候注意一个坑,因为存在负权边,当有的点不能从起点达到的时候,INF可能被负权边松弛。。

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    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 a % b ? gcd(b, a % b) : b; }
    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 = 40005;
    int t, r, p, s, cnt, tot, head[N], c[N], d[N], dist[N];
    bool vis[N];
    struct Edge { int v, next, w; } edge[N<<5];
    
    void addEdge(int a, int b, int w){
        edge[cnt].v = b, edge[cnt].w = w, edge[cnt].next = head[a], head[a] = cnt ++;
    }
    
    void dfs(int s){
        vis[s] = true;
        c[s] = tot;
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(vis[u]) continue;
            dfs(u);
        }
    }
    
    void topSort(){
        full(dist, INF);
        queue<int> q;
        for(int i = 1; i <= tot; i ++){
            if(!d[i]) q.push(i);
        }
        dist[s] = 0;
        while(!q.empty()){
            full(vis, false);
            int cur = q.front(); q.pop();
            priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > pq;
            for(int i = 1; i <= t; i ++){
                if(c[i] == cur) pq.push(make_pair(dist[i], i));
            }
            while(!pq.empty()){
                int f = pq.top().second, dis = pq.top().first; pq.pop();
                if(vis[f]) continue;
                vis[f] = true;
                for(int i = head[f]; i != -1; i = edge[i].next){
                    int u = edge[i].v;
                    if(dist[u] > dis + edge[i].w){
                        dist[u] = dis + edge[i].w;
                        if(c[u] == c[f]) pq.push(make_pair(dist[u], u));
                    }
                    if(c[u] != c[f]){
                        if(!--d[c[u]]) q.push(c[u]);
                    }
                }
            }
        }
    }
    
    int main(){
    
        full(head, -1);
        t = read(), r = read(), p = read(), s = read();
        for(int i = 0; i < r; i ++){
            int u = read(), v = read(), c = read();
            addEdge(u, v, c), addEdge(v, u, c);
        }
        for(int i = 1; i <= t; i ++){
            if(!vis[i]) ++tot, dfs(i);
        }
        for(int i = 0; i < p; i ++){
            int u = read(), v = read(), c = read();
            addEdge(u, v, c);
        }
        for(int cur = 1; cur <= t; cur ++){
            for(int i = head[cur]; i != -1; i = edge[i].next){
                int u = edge[i].v;
                if(c[u] != c[cur]) d[c[u]] ++;
            }
        }
        topSort();
        for(int i = 1; i <= t; i ++){
            printf(dist[i] >= 100000000 ? "NO PATH
    " : "%d
    ", dist[i]);
        }
        return 0;
    }
    
  • 相关阅读:
    矩阵快速幂模板
    Kuangbin带你飞 AC自动机
    Kuangbin 带你飞 KMP扩展KMP Manacher
    Kuangbin 带你飞 数位DP题解
    kuangbin 带你飞 数学基础
    Kuangbin 带你飞-基础计算几何专题 题解
    最大团问题
    头文件
    Codeforces 362E Petya and Pipes 费用流建图
    kuangbin带你飞 匹配问题 二分匹配 + 二分图多重匹配 + 二分图最大权匹配 + 一般图匹配带花树
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10732659.html
Copyright © 2011-2022 走看看