How far away ?

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

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
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练手。
 
 
在线:
vset[]数组含义:dfs过程中记录经过的节点编号,其实下标可以看做是时间
dep[]数组含义:表示节点的在树中的深度
first[]数组含义:dfs过程中第一次到达节点的时间
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5;
struct AdjEdge{
int to,w,next;
}adjedges[maxn];
int head[maxn];
int dis[maxn],vset[maxn],dep[maxn],d[maxn][30],first[maxn];
int tot,nn;
void init(){
tot=0;
nn=0;
memset(dep,0,sizeof(dep));
memset(head,-1,sizeof(head));
memset(dis,0,sizeof(dis));
memset(d,0,sizeof(d));
memset(first,0,sizeof(first));
}
void addedge(int _u,int _v,int _w){ //
adjedges[tot].to=_v;
adjedges[tot].w=_w;
adjedges[tot].next=head[_u];
head[_u]=tot++;
adjedges[tot].to=_u;
adjedges[tot].w=_w;
adjedges[tot].next=head[_v];
head[_v]=tot++;
} void dfs(int _u,int _fa,int _dep){
// printf("%d %d\n",_u,_dep);
dep[_u]=_dep;
vset[++nn]=_u;
first[_u]=nn;
for(int i=head[_u];i!=-1;i=adjedges[i].next){
AdjEdge & e = adjedges[i];
if(e.to!=_fa){
dis[e.to]=dis[_u]+e.w;
dfs(e.to,_u,_dep+1);
vset[++nn]=_u;
}
}
}
void ST(){
for(int i=1;i<=nn;i++)
d[i][0]=vset[i];
for(int j=1;(1<<j)<=nn;j++){
for(int i=1; i+(1<<j)-1<=nn ; i++){
if(dep[d[i][j-1]]<dep[d[i+(1<<(j-1))][j-1]])
d[i][j]=d[i][j-1];
else d[i][j]=d[i+(1 << (j-1))][j-1];
}
}
} int RMQ(int L,int R){
int k=0;
while((1<<(k+1))<=R-L+1) k++;
if(dep[d[L][k]]<=dep[d[R-(1<<k)+1][k]])
return d[L][k];
return d[R-(1<<k)+1][k];
} int main(){
int T,n,q;
scanf("%d",&T);
while(T--){
init();
int a,b,c;
scanf("%d%d",&n,&q);
for(int i=1;i<=n-1;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
dfs(1,-1,1);
ST();
for(int i=0;i<q;i++){
scanf("%d%d",&a,&b);
if(first[a]<=first[b]){
int tmp1=RMQ(first[a],first[b]);
printf("%d\n",dis[a]+dis[b]-2*dis[tmp1]);
}else{
int tmp1=RMQ(first[b],first[a]);
printf("%d\n",dis[a]+dis[b]-2*dis[tmp1]);
}
}
}
return 0;
}

  

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模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  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. Python模块-logging模块(一)

    logging模块用来写日志文件 有5个级别,debug(),info(),warning(),error()和critical(),级别最高的为critical() debug()为调试模式,inf ...

  2. 使用TRY CATCH进行SQL Server异常处理

    TRY...CATCH是Sql Server 2005/2008令人印象深刻的新特性.提高了开发人员异常处理能力.没有理由不尝试一下Try.. Catch功能. *      TRY 块 - 包含可能 ...

  3. File 类 操作实例

    File 操作 <介绍> 尽管java.io定义的大多数类是实行流式操作的,File类不是.它直接处理文件和文件系统.也就是说,File类没有指定信息怎样从文件读取或向文件存储:它描述了文 ...

  4. 核PCA与PCA的精髓和核函数的映射实质

    1.PCA简介 遭遇维度危机的时候,进行特征选择有两种方法,即特征选择和特征抽取.特征选择即经过某种法则直接扔掉某些特征,特征抽取即利用映射的方法,将高维度的样本映射至低维度.PCA(或者K-L变换) ...

  5. top查看CPU情况

    Linux查看CPU情况 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CPU 使用状况.运行 top ...

  6. Learning Python 003 缩进

    Python 缩进 Python的代码中不使用{}大括号来来表示一个代码块,而是使用缩进方式.像下面这段代码: # print absolute value of an integer: a = 10 ...

  7. 1、转载 bwa的使用方法

    http://bio-bwa.sourceforge.net/bwa.shtml http://www.plob.org/?p=25 bwa的使用需要两中输入文件: Reference genome ...

  8. window 删除文件提示指定的文件名无效或太长

    方法0: 使用 chkdsk 磁盘修复工具 .单击“开始”,点击“运行”,输入cmd并回车打开命令提示符窗口: .在此窗口输入以下命令: 例如:检查并修复D分区 chkdsk D: /f 回车,输入 ...

  9. C++基础之类和对象一

    (1)类是一种复杂的数据类型,它是抽象数据类型的实现,是数据和相关操作的封装体.类用来确定一类对象的形为,而这些行为是通过类的内部数据和操作来确定的.这些行为是通过一种操作接口来描述的.(2)类的定义 ...

  10. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...