zoukankan      html  css  js  c++  java
  • POJ 1986 Distance Queries 离线LCA

    Distance Queries
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 8694   Accepted: 3052
    Case Time Limit: 1000MS

    Description

    Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!

    Input

    * Lines 1..1+M: Same format as "Navigation Nightmare"

    * Line 2+M: A single integer, K. 1 <= K <= 10,000

    * Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.

    Output

    * Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.

    Sample Input

    7 6
    1 6 13 E
    6 3 9 E
    3 5 7 S
    4 1 3 N
    2 4 20 W
    4 7 2 S
    3
    1 6
    1 4
    2 6
    

    Sample Output

    13
    3
    36
    

    Hint

    Farms 2 and 6 are 20+3+13=36 apart.

    Source

     
    题目大意:给你一颗树,树边有长度,再给你多个询问,每次询问需要回答两点间的最小长度。
     
    题目分析:设树根为root,则两点a,b间的最小长度等于dis(root,a) + dis(root,b) - 2 * dis(root,lca(a,b))。用一遍Tarjan的LCA算法求一下即可。
     
    代码如下:
     
    #include <stdio.h>
    #include <string.h>
    const int oo = 0x3f3f3f3f;
    const int maxE = 1000000;
    const int maxN = 100005;
    struct Edge{
        int n, v, d, lca;
    };
    Edge edge[maxE];
    int p[maxN];
    int Adj[maxN], l;
    Edge qedge[maxE];
    int qAdj[maxN], ll;
    int vis[maxN];
    int dis[maxN];
    int ask[maxN];
    int n, m, q;
    int min(int a, int b){
        if(a > b) return b;
        return a;
    }
    int find(int x){
        return p[x] == x ? x : (p[x] = find(p[x]));
    }
    void addedge(int u, int v, int d){
        edge[l].v = v; edge[l].d = d; edge[l].n = Adj[u]; Adj[u] = l++;
        edge[l].v = u; edge[l].d = d; edge[l].n = Adj[v]; Adj[v] = l++;
    }
    void qaddedge(int u, int v){
        qedge[ll].v = v; qedge[ll].n = qAdj[u]; qAdj[u] = ll++;
        qedge[ll].v = u; qedge[ll].n = qAdj[v]; qAdj[v] = ll++;
    }
    void init(){
        for(int i = 0; i <= n; ++i) p[i] = i;
        memset(vis, 0, sizeof vis);
        memset(dis, oo, sizeof dis);
        memset(Adj, -1, sizeof Adj);
        memset(qAdj, -1, sizeof qAdj);
        l = 0;
        ll = 0;
    }
    int LCA(int u){
        p[u] = u;
        vis[u] = 1;
        for(int i = Adj[u]; ~i; i = edge[i].n){
            int v = edge[i].v;
            if(!vis[v]){
                dis[v] = min(dis[v], dis[u] + edge[i].d);
                LCA(v);
                p[v] = u;
            }
        }
        for(int i = qAdj[u]; ~i; i = qedge[i].n){
            int v = qedge[i].v;
            if(vis[v]){
                qedge[i].lca = find(v);
                qedge[i ^ 1].lca = qedge[i].lca;
                qedge[i].d = dis[v] + dis[u] - 2 * dis[qedge[i].lca];
                qedge[i ^ 1].d = qedge[i].d;
            }
        }
        return 0;
    }
    void work(){
        int u, v, d;
        while(~scanf("%d%d", &n, &m)){
            init();
            for(int i = 0; i < m; ++i){
                scanf("%d%d%d%*s", &u, &v, &d);
                addedge(u, v, d);
            }
            scanf("%d", &q);
            for(int i = 0; i < q; ++i){
                scanf("%d%d", &u, &v);
                ask[i] = ll;
                qaddedge(u, v);
    
            }
            dis[u] = 0;
            LCA(u);
            for(int i = 0; i < q; ++i){
                printf("%d
    ", qedge[ask[i]].d);
            }
        }
    }
    int main(){
        work();
        return 0;
    }
    POJ 1986
  • 相关阅读:
    从 0 配置 webpack(一)
    Redux
    React Hooks 全解(二)
    日本人要把核污水排进大海,我突然想到几个问题
    突然发现,我的代码还花花绿绿的,挺好看的
    Ghidra ,改道吧,我也准备改道这玩意了
    语音控制?这,看起来很眼熟。
    winafl 工具的编译
    关于 TX 的 WeGame 的一点疑问
    新年的第一个随笔,随便写写吧
  • 原文地址:https://www.cnblogs.com/ac-luna/p/3758535.html
Copyright © 2011-2022 走看看