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

ECJTU 2009 Spring Contest

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]);
    }

}
时间: 2024-10-04 21:30:14

HDU-2586 How far away?的相关文章

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): 4936    Accepted Submission(s): 1866 Problem Description There are n houses in the village and some bidirectional roads conne

hdu 2586 How far away ?倍增LCA

hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增求取LCA,同时跟新距离数组 因为 \(2^{16} > 40000\) 所以所以表示祖先的数组dp[][]第二维取到16即可 就这道题来说,与比较tarjan比较,稍快一点 代码: #include <iostream> #include <algorithm> #includ

HDU 2586 How far away ? (LCA最近公共祖先)

题目地址:HDU 2586 LCA第一发. 纯模板题. 偷懒用的vector,结果一直爆栈.把G++改成C++就过了.. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <se

HDU 2586 How far away ? (离线LCA Tarjan算法模板)

How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6422    Accepted Submission(s): 2411 Problem Description There are n houses in the village and some bidirectional roads connecting

HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 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

hdu - 2586 How far away ?(最短路共同祖先问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~ 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问两个房子a,b之间的距离是多少. 很明显的最近公共祖先问题,先建一棵树,然后求出每一点i到树根的距离dis[i],然后每次询问a,b之间的距离=dis[a]+dis[b]-2*dis[LCA(a,b)]; LCA(a,b)即是a,b的最近公共祖先.. 关于最近公共祖先,给大家推荐一个

hdu 2586 How far away ?(Tarjan离线LCA)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:对于一个有 n 个节点的图,有 n - 1 条无向边,权值给出.有 m 个查询, 每个查询 a b 表示询问 a b 两节点间的距离. 思路: 把这个联通图以树的形式表现出来,取任意两点,假设其最近公共祖先(Least Common Ancestors)为 lca,则两点间的距离等于: dis(a, b) = dis(a, root) + dis(b, root) - 2 * dis(r

HDU 2586 How far away ?(LCA在线算法实现)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的距离,然后只要计算出两点的LCA,这样一来答案就是distance[u]+distance[v]-2distance[LCA]. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #includ

HDU 2586 How far away ?(LCA模板 最近公共祖先啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 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

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目: 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 ho