HDU - 2586

Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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

题意是给你一棵带权树,以及任意两点,让你算出两点的距离。那么可以通过求两点的最近公共祖先(LCA),然后两点到根节点的距离的和减去两倍最近公共祖先到根节点的距离就是两点的距离了。求LCA可以用tarjan,是利用dfs的性质加上并查集找祖先的功能实现的。具体实现看代码

可以参考http://blog.csdn.net/l_bestcoder/article/details/52087126

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define X first
#define Y second
using namespace std;
typedef pair<int,int> pii;
const int maxn=;
struct node
{
int v,w,next;
} E[maxn*];
pii P[];
int cnt,head[maxn],Lca[maxn],n,m,dis[maxn],f[maxn];
bool vis[maxn];
void init()
{
cnt=;
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
}
void addedge(int v,int u,int w)
{
E[cnt].v=v,E[cnt].w=w,E[cnt].next=head[u];
head[u]=cnt++;
E[cnt].v=u,E[cnt].w=w,E[cnt].next=head[v];
head[v]=cnt++;
}
int find(int x)
{
return x==f[x]?x:f[x]=find(f[x]);
}
void tarjan(int root)
{
vis[root]=;
f[root]=root;//初始化
for (int i=; i<=m; i++)
{
if (P[i].X==root&&vis[P[i].Y])
Lca[i]=find(P[i].Y);
if (P[i].Y==root&&vis[P[i].X])
Lca[i]=find(P[i].X);
}
for (int i=head[root]; i!=-; i=E[i].next)
{
if (!vis[E[i].v])
{
dis[E[i].v]=dis[root]+E[i].w;
tarjan(E[i].v);
f[E[i].v]=root;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
init();
int a,b,c;
scanf("%d%d",&n,&m);
for (int i=;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
for (int i=;i<=m;i++)
scanf("%d%d",&P[i].X,&P[i].Y);
dis[]=;
tarjan();
for (int i=;i<=m;i++)
printf("%d\n",dis[P[i].X]+dis[P[i].Y]-*dis[Lca[i]]);
}
return ;
}

HDU - 2586 How far away ?(LCA模板题)的更多相关文章

  1. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. HDU 2586——How far away ?——————【LCA模板题】

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  4. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  5. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  6. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

  7. HDU 1874 畅通工程续(模板题——Floyd算法)

    题目: 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰 ...

  8. HDU 1301-Jungle Roads【Kruscal】模板题

    题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下 ...

  9. 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)

    思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...

随机推荐

  1. Chapter 20_1 table库

    table库是由一些辅助函数构成,把table作为数组来操作,所有的函数都忽略传入参数的那张表中的非数字键. 无论如何,若一个操作需要取表的长度,这个表必须是一个真序列,或是拥有__len元方法. 提 ...

  2. Openjudge-NOI题库-Pell数列

    题目描述 Description Pell数列a1, a2, a3, ...的定义是这样的,a1 = 1, a2 = 2, ... , an = 2 * an − 1 + an - 2 (n > ...

  3. CharSequence 接口

    java中有些方法需要用到CharSequence 类型的参数,笔者百度了一下,总结出一下几点: 1.CharSequence 是一个接口,可以直接用“=”赋值一段字符串,但是不能用new新建一个对象 ...

  4. Java基础之异常

    1.异常的概念 异常:程序在运行时出现的不正常情况,也可以说是出现的问题: Java中的异常:出现的不正常的问题也是一类事物,这类事物有一些共性的东西,比如有名称,有产生的原因等,将这些共性的部分抽取 ...

  5. Jquery跨域调用后台方法

    //前端JS function CallHandlerByJquery() { var url = "http://" + window.location.hostname + & ...

  6. sql 函数 DATEADD 使用

    DATEADD ( datepart , number, date ) 在日期中添加或减去指定的时间间隔.   datepart 是规定应向日期的哪一部分返回新值的参数. number 为要增加或减去 ...

  7. DIV+CSS 让同一行的图片和文字对齐

    在div+css布局中,如果一行(或一个DIV)内容中有图片和文字的话,图片和文字往往会一个在上一个在下,这是一个新手都会遇到问题,我的解决方法有三: 1.添加CSS属性:vertical-align ...

  8. 组合,关联,聚合的区别(转自http://jimmyleeee.blog.163.com/blog/static/9309618200932014422932/)

    类间关系 在类图中,除了需要描述单独的类的名称.属性和操作外,我们还需要描述类之间的联系,因为没有类是单独存在的,它们通常需要和别的类协作,创造比单独工作更大的语义.在UML类图中,关系用类框之间的连 ...

  9. C#隐藏(new)方法和重写(override)方法

    在基类调用的时候 隐藏方法还是调用基类的方法 而重写方法调用的就是子类的中的方法 同时,当子类中的方法与父类或者所实现的接口中的扩展方法冲突时,那么此时相当于一个隐藏方法 基类调用或者接口调用的时候使 ...

  10. PHP学习过程_Symfony_(3)_整理_十分钟学会Symfony

    这篇文章主要介绍了Symfony学习十分钟入门教程,详细介绍了Symfony的安装配置,项目初始化,建立Bundle,设计实体,添加约束,增删改查等基本操作技巧,需要的朋友可以参考下 (此文章已被多人 ...