Distance Queries

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
题意:给出一棵树,其实是个图。求出两点之间的最短距离。
#include <stdio.h>
#include 
<stdlib.h>
#include 
<math.h>
#define maxn 600010
int son[maxn], loc[maxn], dis[maxn], sw[maxn], indegree[maxn], th;
struct T
{
    
int e, d;
}rmq[maxn
*2][30];
struct F
{
    
int u, v, w, next; 
}fn[maxn 
* 2];
void set(int n)
{
    
for (int i = 1; i <= n; i++)
    {
        son[i] 
= 0;
        indegree[i] 
= 0;
        dis[i] 
= -1;
        loc[i] 
= -1;
    }
    
for (int i = 1; i <= n; i++)
    {
        fn[i].next 
= 0;
    }
}
void link(int u, int v, int w)
{
    
/*
    树的遍历,这个得记住。节省空间。
    
*/
    th
++;
    fn[th].u 
= u, fn[th].v = v, fn[th].w = w, fn[th].next = son[u], son[u] = th;
    th
++;
    fn[th].u 
= v, fn[th].v = u, fn[th].w = w, fn[th].next = son[v], son[v] = th;
}
/*
通过搜索来生成树的遍历节点
*/
void dfs(int d, int u, int f, int sd)
{
    
int v, i;
    th
++;
    
if (loc[u] == -1)//只还没出现过的节点在遍历中的时间戳(位置)
    {
        loc[u] 
= th;
        dis[u] 
= sd;//距离是叠加的。
    }
    rmq[th][
0].e = u;
    rmq[th][
0].d = d;
    
for (i = son[u]; i; i = fn[i].next)
    {
        
if (fn[i].v != f)
        {
            dfs(d 
+ 1, fn[i].v, u, sd + fn[i].w);
            th
++;
            rmq[th][
0].e = u;
            rmq[th][
0].d = d;
        }
    }
}
void build (int n)
{
    
int c = log((double)n) / log(2.0), i, j, m;
    
for (i = 1; i <= c; i++)
    {
        
for (j = 1; j <= n; j++)
        {
            m 
= j + (1 << (i - 1));
            
if (m > n)
            {
                rmq[j][i] 
= rmq[j][i-1];
            }
            
else
            {
                
if (rmq[j][i-1].d <= rmq[m][i-1].d)
                {
                    rmq[j][i] 
= rmq[j][i-1];
                }
                
else
                {
                    rmq[j][i] 
= rmq[m][i-1];
                }
            }
        }
    }
}
int query(int u, int v)
{
    
if (loc[u] > loc[v])
    {
        u 
^= v;
        v 
^= u;
        u 
^= v;
    }
    
int i = loc[u], j = loc[v];
    
int len = j - i + 1;
    
int c = log((double)len) / log(2.0);
    
int m = j - (1 << c) + 1;
    
int ancestor;
    
if (rmq[i][c].d <= rmq[m][c].d)
    {
        ancestor 
= rmq[i][c].e;
    }
    
else
    {
        ancestor 
= rmq[m][c].e;
    }
    
return dis[u] + dis[v] - 2 * dis[ancestor];
}
int main()
{
    
char c[2];
    
int n, m, u, v, w, k, t, i;
    scanf(
"%d%d"&n, &m);
    
set(n);
    th 
= 0;
    
while (m--)
    {
        scanf(
"%d%d%d%s"&u, &v, &w, c);
        link(u, v, w);
    }
    th 
= 0;
    dfs(
0100);
    build (
2 * n - 1);
    scanf(
"%d"&k);
    
while (k--)
    {
        scanf(
"%d%d"&u, &v);
        printf(
"%d\n", query(u, v));
    }
    
return 0;
}