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. ubuntu dd烧录镜像文件

    df命令查看挂在的U盘和硬盘,ssd 右边可以看到路径 sudo dd if=cn_windows_10_multiple_editions_version_1703_updated_july_201 ...

  2. 使用py 和flask 实现的服务器系统目录浏览,日志文件实时显示到网页的功能

    看日志希望带有彩色,希望从浏览器上看到,不用连到机器上看. 浏览系统的文件夹,scan + 系统文件夹的层级名字当做url路由,可以深层次看到机器上任何层级的文件夹,实现系统文件夹浏览下载. 如果是点 ...

  3. C#设计模式(12)——享元模式

    一.概念 外观模式提供了一个统一的接口,用来访问子系统中的一群接口.外观定义了一个高层接口,让子系统更容易使用.使用外观模式时,我们创建了一个统一的类,用来包装子系统中一个或多个复杂的类,客户端可以直 ...

  4. 进程中t.start(), t.daemon() t.jion()的使用

    #!/usr/bin/env python import multiprocessing import time def f1(a1): time.sleep(2) print(a1) if __na ...

  5. linux 安装输入法

    简述 Ubuntu16.04安装完后,和12.04以及14.04都不一样,并没有中文输入功能.于是搜索一些安装中文输入法的方法. 开始安装了ibus pinyin输入法,但是系统重启之后发现有些时候不 ...

  6. Material使用05 MdListModule模块 MdButtonToggleModule模块

    1 在共享模块中导入MdListModule模块 import { NgModule } from '@angular/core'; import { CommonModule } from '@an ...

  7. DOS查看端口占用及杀掉进程命令

    转载自:http://www.cnblogs.com/rainman/p/3457227.html 1. 查看端口占用 在windows命令行窗口下执行: netstat -aon|findstr & ...

  8. HDFS内容追加

    配置:hdfs-site.xml <property> <name>dfs.support.append</name> <value>true</ ...

  9. 使用python内置模块os和openpyxl搜索指定文件夹下Excel中的内容

    在指定路径下,搜索Excel文件中包含的指定内容,首先需要遍历指定路径,得到该路径下所有Excel文件的绝对/相对路径:然后读取Excel中内容,将文件中的每个单元格的值与要搜索的内容进行判断(正则比 ...

  10. ARC097D Equals

    传送门 题目 We have a permutation of the integers from 1 through N, p1, p2, .., pN. We also have M pairs ...