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
  • 相关阅读:
    中断向量表
    lua绑定C++对象—luna模板
    lua对象调用—用 "." 与 ":" 调用表中函数时的区别
    FreeSWITCH 实现 双线路呼叫(主备线路)
    [转]Freeswitch在阿里云服务器语音不通问题小记(FS的NAT穿越穿透)
    [转]签发使用自签发证书--指定使用多域名、泛域名及直接使用IP地址
    [转]FreeSwitch启用WEBRTC小记
    [转]FreeSwitch1.10版本安装及内置mariadb(mysql)使用记录
    FreeSWITCH 使用 lua 脚本 接管 分机注册,鉴权等
    sip客户端NAT注册和拨打FreeSWITCH ,FreeSWITCH 会发到 sip客户端内网地址的问题
  • 原文地址:https://www.cnblogs.com/ac-luna/p/3758535.html
Copyright © 2011-2022 走看看