2018.9.10 0:40 重新敲一遍,然后很认真的写了注释,方便自己和队友看,刚过去的一天的下午打网络赛有一题用到了这个,但是没写注释,队友改板子有点伤,因为我没注释。。。

以后写博客,代码要写注释,要把题目意思写上。。。

我好捞啊。。。

How far away ?

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

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
2
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模板题
 
 
代码:
 //HDU2586
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=4e4+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int dp[maxn<<][];//数组记得开2倍,因为遍历之后序列长度变为2*n-1
bool vis[maxn];//标记 struct node{
int u,v,w,next;
}edge[maxn<<]; int tot,head[maxn];//head保存的是以当前节点为起点的所有边中最后一条边的编号 int num; inline void add(int u,int v,int w)
{
edge[num].u=u;edge[num].v=v;edge[num].w=w;//存边和权值
edge[num].next=head[u];head[u]=num++;//next保存的是以u为起点的上一条边的编号
u=u^v;v=u^v;u=u^v;//节点互换,存两次,因为为无向图,(u,v)存一次,(v,u)存一次,以下操作同上
edge[num].u=u;edge[num].v=v;edge[num].w=w;
edge[num].next=head[u];head[u]=num++;
} int ver[maxn<<],deep[maxn<<],node[maxn<<],dir[maxn<<];
//ver节点编号,dfs搜索过程中的序列,deep深度,node点编号位置,dir距离 void dfs(int u,int dep)
{
vis[u]=true;//标记u节点被访问过
ver[++tot]=u;//存dfs序
node[u]=tot;//节点的dfs序的编号
deep[tot]=dep;//该编号的深度
for(int k=head[u];k!=-;k=edge[k].next)//倒着遍历以u节点为起点的所有边的编号
if(!vis[edge[k].v]){//如果该编号的边未被访问过
int v=edge[k].v,w=edge[k].w;//v表示该边的终点,w表示该边的权值
dir[v]=dir[u]+w;//权值和
dfs(v,dep+);//再往下dfsv节点的深度
ver[++tot]=u;deep[tot]=dep;//表示dfs的时候还要回溯到上面,就是把dfs序保存一下,走到底再返回去去遍历没走过的点
}
}
//可以看以前写的RMQ(ST)的详解https://www.cnblogs.com/ZERO-/p/8456910.html
void ST(int n)//ST操作
{
for(int i=;i<=n;i++)
dp[i][]=i;//初始化为自己
for(int j=;(<<j)<=n;j++){
for(int i=;i+(<<j)-<=n;i++){
int a=dp[i][j-],b=dp[i+(<<(j-))][j-];
dp[i][j]=deep[a]<deep[b]?a:b;
}
}
} int RMQ(int l,int r)
{
int k=;
while((<<(k+))<=r-l+)k++;//最多能跳2的多少次幂
int a=dp[l][k],b=dp[r-(<<k)+][k];//保存的是编号
return deep[a]<deep[b]?a:b;
} int LCA(int u,int v)
{
int x=node[u],y=node[v];
if(x>y)swap(x,y);
int res=RMQ(x,y);
return ver[res];
} int main()
{
int cas;
scanf("%d",&cas);
while(cas--){
int n,q;
num=;
scanf("%d%d",&n,&q);
memset(head,-,sizeof(head));//初始化
memset(vis,false,sizeof(vis));
for(int i=;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);//存边
}
tot=;dir[]=;
dfs(,);
ST(*n-);
while(q--){
int u,v;
scanf("%d%d",&u,&v);
int lca=LCA(u,v);
printf("%d\n",dir[u]+dir[v]-*dir[lca]);//两点最短距离为每个点到根节点的距离-最近公共祖先到根节点的距离的*2
}
}
return ;
}

就这样,捞咸鱼。。。

༼༎ຶᴗ༎ຶ༽

HDU 2586.How far away ?-在线LCA(ST)-代码很认真的写了注释(捞到变形)的更多相关文章

  1. HDU 2586 How far away ? (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...

  2. 【HDU 2586 How far away?】LCA问题 Tarjan算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵n个节点的无根树,每条边有各自的权值.给出m个查询,对于每条查询返回节点u到v的最 ...

  3. HDU 2586 How far away ? (LCA,Tarjan, spfa)

    题意:给定N个节点一棵树,现在要求询问任意两点之间的简单路径的距离,其实也就是最短路径距离. 析:用LCA问题的Tarjan算法,利用并查集的优越性,产生把所有的点都储存下来,然后把所有的询问也储存下 ...

  4. Hdu 2586 树链剖分求LCA

    Code: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> ...

  5. 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 ...

  6. hdu 2586 How far away? (LCA模板)

    题意: N个点,形成一棵树,边有长度. M个询问,每个询问(a,b),询问a和b的距离 思路: 模板题,看代码.DFS预处理算出每个结点离根结点的距离. 注意: qhead[maxn],而不是qhea ...

  7. 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 ...

  8. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

  9. [hdu 2586]lca模板题(在线+离线两种版本)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 在线版本: 在线方法的思路很简单,就是倍增.一遍dfs得到每个节点的父亲,以及每个点的深度.然后 ...

随机推荐

  1. 2016年后web开发趋势是什么?

    2016 年后 Web开发趋势是什么 来源:yafeilee.me 发布时间:2016-05-06 阅读次数:1378 3   近二年的进展 前端发展日新月异, 甚至有一句戏言: "每六星期 ...

  2. WPF 利用键盘钩子来捕获键盘,做一些不为人知的事情...完整实例

    键盘钩子是一种可以监控键盘操作的指令. 看到这句话是不是觉得其实键盘钩子可以做很多事情. 场景 当你的程序需要一个全局的快捷键时,可以考虑使用键盘钩子,如大家常用qq的截图快捷键,那么在WPF里怎么去 ...

  3. TO_CHAR 和 TO_DATE的一些用法总结

    对于初学者来说,日期处理那快一向是问题的集中地.今天刚刚看了个案例,将的就是ORACLE日期处理中的错误,其关键原因,就是TO_CHAR 和TO_DATE的用法不清晰,,事实上,这两个东西也特别容易混 ...

  4. Python调用Webservice

    使用Python调用webservice 推荐使用 suds包 该包一般在Python2.x   python3各种麻烦 略过 实例 import suds # webservice url url ...

  5. Codeforces Round #329(Div2)

    CodeForces 593A 题意:n个字符串,选一些字符串,在这些字符串中使得不同字母最多有两个,求满足这个条件可选得的最多字母个数. 思路:用c[i][j]统计文章中只有i,j对应两个字母出现的 ...

  6. SqlServer中截取小数位数

    方法一:convert(float,字段名) as 别名 select convert(float,round(10.123232,2)) 结果:10.12 select convert(float, ...

  7. CSS3 em && rem 详细教程

    1 # mobile css & rem & em & px > 1 rem === 16px 任意浏览器的默认字体高都是 16px, 所有未经调整的浏览器都符合: 1e ...

  8. 【CZY选讲·Yjq的棺材】

    题目描述 Yjq想要将一个长为宽为的矩形棺材(棺材表面绝对光滑,所以棺材可以任意的滑动)拖过一个L型墓道. 如图所示,L型墓道两个走廊的宽度分别是和,呈90°,并且走廊的长度远大于. 现在Hja ...

  9. Cisco IPC Emergency Responder Error

    Upon startup of the newer Cisco IP Communicator clients (especially on Windows Vista/7), sometimes y ...

  10. classpath: spring 中的查找方式

    Spring可以通过指定classpath*:与classpath:前缀加路径的方式从classpath加载文件,如bean的定义文件.classpath*:的出现是为了从多个jar文件中加载相同的文 ...