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. 【jQuery】jquery.metadata.js验证失效

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. 想要table表格垂直滚动,加点CSS即可

    <style> /*设置 tbody高度大于400px时 出现滚动条*/ table tbody { display: block; height: 400px; overflow-y: ...

  3. 关于Windows文件读写_暗涌_新浪博客

    关于Windows文件读写_暗涌_新浪博客     这几天在研究怎么才能加快windows文件读写速度,搜了很多文章,MSDN也看了不少.稍微给大家分享一下.     限制windows文件读写速度的 ...

  4. new/delete 和malloc/free 的区别

    new/delete 和malloc/free 的区别 一.基本概念malloc/free:1.函数原型及说明:      void *malloc(long NumBytes):该函数分配了NumB ...

  5. 4、linux-grep awk sed and cuf sort uniq join

    1.grep 1.1 grep [-invc] [--color=auto] '搜寻字符串' filename选项与参数:-i :忽略大小写的不同-n :顺便输出行号-v :显示没有 '搜寻字符串' ...

  6. 利用Hadoop自带example实现wordCount

    上次虽然把环境搭好了,但是实际运行起来一堆错误,下面简述一下踩的坑. 1.hadoop fs -put上传文件失败,WARN org.apache.hadoop.hdfs.server.datanod ...

  7. HN669打包工具--打包工具使用文档

    打包工具主要包含下载更新资源文件以及打包两个部分 一.下载更新资源文件 1.终端进入工具根目录,即HN669SDK_iOS目录,并运行./api.sh脚本 2.输入游戏id (note:此游戏id为我 ...

  8. fs-hasher工具介绍

    工具列表 hf-stat hf-simdex fs-hasher hf-stat工具 使用方法 以-f参数为例 Usage: ./hf-stat -f hashfile 参数用途 -f : 显示.ha ...

  9. Go:Nsq消息队列

    Nsq服务端简介 在使用Nsq服务之前,还是有必要了解一下Nsq的几个核心组件整个Nsq服务包含三个主要部分 nsqlookupd 先看看官方的原话是怎么说:nsqlookupd是守护进程负责管理拓扑 ...

  10. 洛谷P2060 faebdc玩扑克2

    P2060 faebdc玩扑克2 题目背景 faebdc和zky又在玩扑克 题目描述 给你2N张牌,编号为1,2,3..n,n+1,..2n.这也是最初的牌的顺序. 一次洗牌是把序列变为n+1,1,n ...