HDU-2586 How far away?

Posted GoesOn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU-2586 How far away?相关的知识,希望对你有一定的参考价值。

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17447    Accepted Submission(s): 6745


Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can\'t visit a place twice) between every two houses. Yout task is to answer all these curious people.
 

 

Input
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
 

 

Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 

 

Sample Input
2 3 2 1 2 10 3 1 15 1 2 2 3 2 2 1 2 100 1 2 2 1
 

 

Sample Output
10 25 100 100
 

 

Source
 

 

Recommend
lcy

 

题目大意:给定n个点和n-1条边,询问两点间的最短距离

解题思路:LCA离线。上一题的代码就改了下输入。。。(http://www.cnblogs.com/WWkkk/p/7409868.html

 

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;

struct node
{
    int v,c;
};

vector<node>tree[maxn],que[maxn];
int dis[maxn],num[maxn],f[maxn];
bool vis[maxn];

void Init(int n)
{
    for(int i=0;i<=n;i++)
    {
        tree[i].clear();
        que[i].clear();
        f[i] = i;
        dis[i] = 0;
        num[i] = 0;
        vis[i] = 0;
    }
}

int Find(int x)
{
    int r=x;
    while(r!=f[r])
    {
        r = f[r];
    }
    while(x!=f[x])
    {
        int j=f[x];
        f[x] = r;
        x = j;
    }
    return x;
}

void lca(int u)
{
    vis[u] = true;
    f[u] = u;
    for(int i=0;i<que[u].size();i++)
    {
        int v = que[u][i].v;
        if(vis[v])
        {
            num[que[u][i].c]=dis[v]+dis[u]-2*dis[Find(v)];
        }
    }
    for(int i=0;i<tree[u].size();i++)
    {
        int v=tree[u][i].v;
        if(!vis[v])
        {
            dis[v] = dis[u]+tree[u][i].c;
            lca(v);
            f[v] = u;
        }
    }
}

int main()
{
    int x,y,c,n,q,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&q);
        Init(n);
        for(int i=0;i<n-1;i++)
        {
            scanf("%d %d %d",&x,&y,&c);
            node temp;
            temp.v = y;
            temp.c = c;
            tree[x].push_back(temp);
            temp.v = x;
            tree[y].push_back(temp);
        }
        for(int i=0;i<q;i++)
        {
            scanf("%d %d",&x,&y);
            node temp;
            temp.v = y;
            temp.c = i;
            que[x].push_back(temp);
            temp.v = x;
            que[y].push_back(temp);
        }
        lca(1);
        for(int i=0;i<q;i++)
            printf("%d\\n",num[i]);
    }

}

 

以上是关于HDU-2586 How far away?的主要内容,如果未能解决你的问题,请参考以下文章

hdu 2586 How far away?(树链剖分+前缀和)

HDU2586How far away? LCA

HDU - 2586 How far away ?(暴力 | LCA)

hdu 2586 How far away ?

HDU 2586 How far away ?

HDU2586---How far away ?(lca算法)