Distance Queries(LCA)

Distance Queries
Time Limit: 2000MS  Memory Limit: 30000K
Total Submissions: 8642  Accepted: 3036
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. 

      题意:

      给出 N 和 M,代表有 N 个节点 M 条无向边。后给出这 M 条边的两端点编号和距离和方位(方位可以无视)。后有 K 个询问,每个询问输出一个答案,输出这两个点的距离。

 

      思路:

      LCA(最近公共祖先)(倍增法)。预处理每个结点的深度值 和 根节点到每个节点的距离值 dis,求出 两个点的 LCA 后 d = dis [ A ] + dis [ B ] - 2 X dis [ LCA ] 即可。

 

      AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int NMAX = 40005;
const int EMAX = NMAX * 5;

int ind, n, MAX_V;
int fir[NMAX], next[EMAX], v[EMAX], w[EMAX];
int par[20][NMAX], dep[NMAX], dis[NMAX];

void add_edge(int f, int t, int val) {
        v[ind] = t;
        next[ind] = fir[f];
        fir[f] = ind;
        w[ind] = val;
        ++ind;
}

void dfs(int i, int fa, int k, int d) {
        par[0][i] = fa;
        dep[i] = k;
        dis[i] = d;
        for (int e = fir[i]; e != -1; e = next[e]) {
                if (v[e] != fa) dfs(v[e], i, k + 1, d + w[e]);
        }
}

void init() {
        dfs(1, -1, 0, 0);

        for (int i = 0; i < MAX_V - 1; ++i) {
                for (int j = 1; j <= n; ++j) {
                        if (par[i][j] < 0) par[i + 1][j] = -1;
                        else par[i + 1][j] = par[i][ par[i][j] ];
                }
        }
}

int lca(int f, int t) {
        if (dep[t] > dep[f]) { int a = f; f = t; t = a; }

        int dis = dep[f] - dep[t];
        for (int i = 0; i < MAX_V; ++i)
                if ((dis >> i) & 1) f = par[i][f];

        if (f == t) return f;

        for (int i = MAX_V - 1; i >= 0; --i) {
                if (par[i][f] != par[i][t]) {
                        f = par[i][f];
                        t = par[i][t];
                }
        }

        return par[0][f];
}

int main() {
        int m;
        scanf("%d%d", &n, &m);

        memset(fir, -1, sizeof(fir));
        ind = 0;
        MAX_V = 0;
        while ((1 << MAX_V) <= n) ++MAX_V;

        while (m--) {
                int a, b, num;
                char d;
                scanf("%d%d%d %c", &a, &b, &num, &d);
                add_edge(a, b, num);
                add_edge(b, a, num);
        }

        init();

        int k;
        scanf("%d", &k);
        while (k--) {
                int f, t;
                scanf("%d%d", &f, &t);
                int ans = lca(f, t);
                printf("%d\n", dis[f] + dis[t] - 2 * dis[ans]);
        }

        return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值