zoukankan      html  css  js  c++  java
  • POJ 3463 Sightseeing

    次短路计数。

    类似于最短路计数 + 次短路,在跑最短路的时候同时维护最短路,次短路,最短路的条数,次短路的条数,每一次更新在权值相同的地方计数。

    要把(点,最/次短路)的二元组压成一个状态,每一次取出一个状态去扩展,一共有$4$种情况,具体实现可以参照代码。

    $dij$或者$spfa$实现均可,这题数据很小,不带堆优化也可以。

    注意读题,要在次短路$-1$$==$最短路的时候加入次短路的条数到答案中去。

    时间复杂度$O(nlogn)$。

    Code:

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    const int N = 1005;
    const int M = 10005;
    
    int testCase, n, m, tot = 0, head[N];
    int dis1[N], cnt1[N], dis[N][2], cnt[N][2];
    bool vis[N][2];
    
    struct Edge {
        int to, nxt, val;
    } e[M];
    
    inline void add(int from, int to, int val) {
        e[++tot].to = to;
        e[tot].val = val;
        e[tot].nxt = head[from];
        head[from] = tot;
    }
    
    inline void read(int &X) {
        X = 0; char ch = 0; int op = 1;
        for(; ch > '9'|| ch < '0'; ch = getchar())
            if(ch == '-') op = -1;
        for(; ch >= '0' && ch <= '9'; ch = getchar())
            X = (X << 3) + (X << 1) + ch - 48;
        X *= op;
    }
    
    struct Node {
        int now, s, d;
        
        inline Node(int nowNode, int sta, int dist) {
            now = nowNode, s = sta, d = dist;
        }
        
        friend bool operator < (const Node &x, const Node &y) {
            return x.d > y.d;
        }
        
    };
    priority_queue <Node> Q; 
    
    void dij(int st) {
        memset(dis, 0x3f, sizeof(dis));
        memset(cnt, 0, sizeof(cnt));
        memset(vis, 0, sizeof(vis));
        
        Q.push(Node(st, 0, 0));
        dis[st][0] = 0, cnt[st][0] = 1;
        
        for(; !Q.empty(); ) {
            int x = Q.top().now, s = Q.top().s; Q.pop();
            
            if(vis[x][s]) continue;
            vis[x][s] = 1;
            
            for(int i = head[x]; i; i = e[i].nxt) {
                int y = e[i].to;
                if(dis[y][0] > dis[x][s] + e[i].val) {
                    dis[y][1] = dis[y][0], cnt[y][1] = cnt[y][0];
                    dis[y][0] = dis[x][s] + e[i].val;
                    cnt[y][0] = cnt[x][s];
                    Q.push(Node(y, 0, dis[y][0])), Q.push(Node(y, 1, dis[y][1]));
                } else if(dis[y][0] == dis[x][s] + e[i].val) {
                    cnt[y][0] += cnt[x][s];
                } else if(dis[y][1] > dis[x][s] + e[i].val) {
                        dis[y][1] = dis[x][s] + e[i].val;
                        cnt[y][1] = cnt[x][s];
                        Q.push(Node(y, 1, dis[y][1]));
                } else if(dis[y][1] == dis[x][s] + e[i].val) 
                        cnt[y][1] += cnt[x][s];
            }
        }
    }
    
    int main() {
        for(read(testCase); testCase--; ) {
            read(n), read(m);
            
            tot = 0; memset(head, 0, sizeof(head));
            for(int x, y, v, i = 1; i <= m; i++) {
                read(x), read(y), read(v);
                add(x, y, v);
            }
            
            int st, ed;
            read(st), read(ed);
            dij(st);
            
            int ans = cnt[ed][0];
            if(dis[ed][1] - 1 == dis[ed][0]) ans += cnt[ed][1];
            printf("%d
    ", ans);
         }
         return 0;
    }
    View Code
  • 相关阅读:
    .NetCore Grpc 客服端 工厂模式配置授权
    DOCKER 拉取 dotnet 镜像太慢 docker pull mcr.microsoft.com too slow
    Introducing .NET 5
    VSCode 出现错误 System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached.
    Omnisharp VsCode Attaching to remote processes
    zookeeper3.5.5 centos7 完全分布式 搭建随记
    Hadoop2.7.7 centos7 完全分布式 配置与问题随记
    MySQL索引 索引分类 最左前缀原则 覆盖索引 索引下推 联合索引顺序
    SQL基础随记3 范式 键
    MySQL调优 优化需要考虑哪些方面
  • 原文地址:https://www.cnblogs.com/CzxingcHen/p/9702918.html
Copyright © 2011-2022 走看看