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. [置顶] 什么是C语言结构体字节对齐,为什么要对齐?

    一.概念 对齐跟数据在内存中的位置有关.如果一个变量的内存地址正好位于它长度的整数倍,他就被称做自然对齐.比如在32位cpu下,假设一个整型变量的地址为0x00000004,那它就是自然对齐的.   ...

  2. java基础知识(7)---多态

    多 态:(面向对象特征之一):函数本身就具备多态性,某一种事物有不同的具体的体现.体现:父类引用或者接口的引用指向了自己的子类对象.//Animal a = new Cat();多态的好处:提高了程序 ...

  3. LAMP 1.4 PHP编译安装问题解决

    环境:centos X64 最小化安装 php版本:php-5.4.3 安装前.先安装些软件和库文件 yum install -y gcc gcc-c++ make zlib zlib-devel p ...

  4. python+selenium简单实现拖动元素实例

    from  selenium  import  webdriver#引入ActionChains类from  selenium.webdriver.common.action_chains  impo ...

  5. ansible的使用

    ansible主要分为单条命令和组命令(即配置后缀为名yml的文件,使用ansible-playbook执行)的使用,以下分别对两者进行说明.如不会安装ansible,请参考我的文章 centos7安 ...

  6. JNA 传参char[] 和结构体等

    近日项目中需要用java调用c/c++编写的dll库,所有了解到jna这个东东,下面是使用的一些经验: 一.java使用Jna需要两个jar包,eg:jna-3.5.1.jar和platform-3. ...

  7. Learning Python 001 第一个程序

    Python 第一个程序 我使用的开发工具是PyCharm软件.我们使用的是Python3.5 for windows . 如果你还没有安装PyCharm软件 和 Python3.5,请到这里来看如果 ...

  8. java类什么时候初始化?

    Java虚拟机规范中并没有进行强制玉树什么情况下需要开始类加载过程.但是对于初始化阶段,虚拟机规范则是严格规定了有且仅有5种情况必须立即对类进行“初始化”(而加载,验证,准备自然需要在此之前开始): ...

  9. 项目中缺少org.wltea.ik-analyzer如何解决?

    IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包.从2006年12月推出1.0版开始,IKAnalyzer已经推出了3个大版本.最初,它是以开源项目Luence为应用主体 ...

  10. POJ 2398 Toy Storage (叉积判断点和线段的关系)

    题目链接 Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4104   Accepted: 2433 ...