HDU 2586 How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18652 Accepted Submission(s):
7268
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.
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.
the answer of the query. Output a bland line after each test case.
3 2
1 2 10
3 1 15
1 2
2 3
2 2
1 2 100
1 2
2 1
25
100
100
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=1e5+;
inline int read()
{
char c=getchar();int x=,f=;
while(c<''||c>'') {if(c=='-') f=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-,c=getchar();return x*f;
}
int n,m,S=;
int f[MAXN][],deep[MAXN],g[MAXN];
struct node
{
int u,v,w,nxt;
}edge[MAXN];
int head[MAXN];
int num=;
inline void add_edge(int x,int y,int z)
{
edge[num].u=x;
edge[num].v=y;
edge[num].w=z;
edge[num].nxt=head[x];
head[x]=num++;
}
void dfs(int now)
{
for(int i=head[now];i!=-;i=edge[i].nxt)
if(deep[edge[i].v]==)
{
deep[edge[i].v]=deep[now]+;
f[edge[i].v][]=now;
g[edge[i].v]=g[now]+edge[i].w;
dfs(edge[i].v);
} }
inline void pre()
{
for(int i=;i<=;i++)
for(int j=;j<=n;j++)
f[j][i]=f[f[j][i-]][i-];
}
inline int LCA(int x,int y)
{
if(deep[x]<deep[y]) swap(x,y);
for(int i=;i>=;i--)
if(deep[f[x][i]]>=deep[y])
x=f[x][i];
if(x==y) return x; for(int i=;i>=;i--)
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][];
}
int main()
{
int T=read();
while(T--)
{
n=read();m=read();
memset(head,-,sizeof(head));num=;
memset(f,,sizeof(f));
memset(deep,,sizeof(deep));
for(int i=;i<=n-;i++)
{
int x=read(),y=read(),z=read();
add_edge(x,y,z);
add_edge(y,x,z);
}
deep[S]=;
dfs(S);pre();
while(m--)
{
int x=read(),y=read();
printf("%d\n",g[x]+g[y]-*g[LCA(x,y)]);
}
} return ;
}
HDU 2586 How far away ?的更多相关文章
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- hdu 2586 How far away ?倍增LCA
hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...
- HDU 2586 How far away ?【LCA】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 2586.How far away ?-离线LCA(Tarjan)
2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...
- HDU 2586 How far away ? (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...
- hdu - 2586 How far away ?(最短路共同祖先问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...
- HDU 2586 How far away ?(LCA在线算法实现)
http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...
- HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...
- HDU 2586 How far away ?【LCA模板题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...
- 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 ...
随机推荐
- DCT(离散余弦变换)算法原理和源码(python)
原理: 离散余弦变换(DCT for Discrete Cosine Transform)是与傅里叶变换相关的一种变换,它类似于离散傅里叶变换(DFT for Discrete Fourier Tra ...
- Robot Framework - 建立本地测试环境
注意:本文内容是以“在Window7系统中安装本地RobotFrmamework自动化测试环境”为例. Robot Framework简介 HomePage:http://robotframework ...
- .NET Core SDK在Windows系统安装后出现Failed to load the hostfxr.dll等问题的解决方法
这次无论如何也要记录下,原因是今天在一台Windows2008R2的电脑上安装.NET Core SDK后再命令行执行dotnet --info 居然爆出了"Failed to load t ...
- Eclipse打包出错——提示GC overhead limit exceeded
版权声明:本文为博主原创文章,未经博主允许不得转载. 在Eclipse开发环境中打包发布apk安装包的时候,有时候会出现下面的错误: 原因 在打包的时候,Eclipse占用的内存会增大,当分配给Ecl ...
- mysql 下 计算 两点 经纬度 之间的距离(转)
公式如下,单位米: 第一点经纬度:lng1 lat1 第二点经纬度:lng2 lat2 round(6378.138*2*asin(sqrt(pow(sin( (lat1*pi()/180-lat2* ...
- 用js如何获取file是否存在
其实注意点就可以知道了. 举个例子 firebug看出这代码: <div id="SWFUpload_0_0" class="uploadify-queue-ite ...
- excel 中批量生成mysql的脚本
一.假设你的表格有A.B.C三列数据,希望导入到你的数据库中表格table,对应的字段分别是col1.col2.col3 二.在你的表格中增加一列,利用excel的公式自动生成sql语句,具体方法如下 ...
- Java——对象比较
前言 本篇博客主要梳理一下Java中对象比较的需要注意的地方,将分为以下几个方面进行介绍: ==和equals()方法 hashCode()方法和equals()方法 Comparator接口和Com ...
- Docker的基本操作与示例
一.RunC RunC是一个由OCI(Open Container Initiative)制定的标准化轻量容器运行工具.OCI是专门致力于制定容器格式和运行时开放的工业化标准的组织.那容器标准化后Do ...
- springmvc和structs2的区别
1.从安全性角度分析spring mvc和struts2的区别: spring mvc:controller 1.spring mvc 默认controller是单实例(通过注解@Scope(“pro ...