How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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的基础上加dis(距离根的距离)数组,在dfs处理好,ans=dis[a]-2*dis[LCA(a,b)]+dis[b];
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<string>
  5. #include<queue>
  6. #include<algorithm>
  7. #include<stack>
  8. #include<cstring>
  9. #include<vector>
  10. #include<list>
  11. #include<set>
  12. #include<map>
  13. #define true ture
  14. #define false flase
  15. using namespace std;
  16. #define ll long long
  17. #define inf 0xfffffff
  18. int scan()
  19. {
  20. int res = , ch ;
  21. while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
  22. {
  23. if( ch == EOF ) return << ;
  24. }
  25. res = ch - '' ;
  26. while( ( ch = getchar() ) >= '' && ch <= '' )
  27. res = res * + ( ch - '' ) ;
  28. return res ;
  29. }
  30. #define maxn 40010
  31. #define M 22
  32. struct is
  33. {
  34. int v,next,w;
  35. } edge[maxn*];
  36. int deep[maxn],jiedge;
  37. int dis[maxn];
  38. int head[maxn];
  39. int rudu[maxn];
  40. int fa[maxn][M];
  41. void add(int u,int v,int w)
  42. {
  43. jiedge++;
  44. edge[jiedge].v=v;
  45. edge[jiedge].w=w;
  46. edge[jiedge].next=head[u];
  47. head[u]=jiedge;
  48. }
  49. void dfs(int u)
  50. {
  51. for(int i=head[u]; i; i=edge[i].next)
  52. {
  53. int v=edge[i].v;
  54. int w=edge[i].w;
  55. if(!deep[v])
  56. {
  57. dis[v]=dis[u]+edge[i].w;
  58. deep[v]=deep[u]+;
  59. fa[v][]=u;
  60. dfs(v);
  61. }
  62. }
  63. }
  64. void st(int n)
  65. {
  66. for(int j=; j<M; j++)
  67. for(int i=; i<=n; i++)
  68. fa[i][j]=fa[fa[i][j-]][j-];
  69. }
  70. int LCA(int u , int v)
  71. {
  72. if(deep[u] < deep[v]) swap(u , v) ;
  73. int d = deep[u] - deep[v] ;
  74. int i ;
  75. for(i = ; i < M ; i ++)
  76. {
  77. if( ( << i) & d ) // 注意此处,动手模拟一下,就会明白的
  78. {
  79. u = fa[u][i] ;
  80. }
  81. }
  82. if(u == v) return u ;
  83. for(i = M - ; i >= ; i --)
  84. {
  85. if(fa[u][i] != fa[v][i])
  86. {
  87. u = fa[u][i] ;
  88. v = fa[v][i] ;
  89. }
  90. }
  91. u = fa[u][] ;
  92. return u ;
  93. }
  94. void init()
  95. {
  96. memset(head,,sizeof(head));
  97. memset(fa,,sizeof(fa));
  98. memset(rudu,,sizeof(rudu));
  99. memset(deep,,sizeof(deep));
  100. jiedge=;
  101. }
  102. int main()
  103. {
  104. int x,n;
  105. int t;
  106. scanf("%d",&t);
  107. while(t--)
  108. {
  109. init();
  110. scanf("%d%d",&n,&x);
  111. for(int i=; i<n; i++)
  112. {
  113. int u,v,w;
  114. scanf("%d%d%d",&u,&v,&w);
  115. add(u,v,w);
  116. rudu[v]++;
  117. }
  118. for(int i=;i<=n;i++)
  119. {
  120. if(!rudu[i])
  121. {
  122. deep[i]=;
  123. dis[i]=;
  124. dfs(i);
  125. break;
  126. }
  127. }
  128. st(n);
  129. while(x--)
  130. {
  131. int a,b;
  132. scanf("%d%d",&a,&b);
  133. printf("%d\n",dis[a]-*dis[LCA(a,b)]+dis[b]);
  134. }
  135. }
  136. return ;
  137. }

hdu 2586 How far away ? 带权lca的更多相关文章

  1. hdu 2874 Connections between cities 带权lca判是否联通

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)

    3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...

  3. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  4. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  5. HDU 3047 Zjnu Stadium(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3047 题意: 给出n个座位,有m次询问,每次a,b,d表示b要在a右边d个位置处,问有几个询问是错误的. 思路: ...

  6. poj1986带权lca

    lca求距离,带权值 的树上求lca,我是用倍增法求的,求两点之间的距离转化为到根节点之间的距离 (de了一个小时 的bug,重打居然就过了....) #include<map> #inc ...

  7. hdu 3074 Zjnu Stadium (带权并查集)

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. HDU 4359 Easy Tree DP? 带权二叉树的构造方法 dp

    题意: 给定n deep 1.构造一个n个节点的带权树,且最大深度为deep,每一个节点最多仅仅能有2个儿子 2.每一个节点的值为2^0, 2^1 ··· 2^(n-1)  随意两个节点值不能同样 3 ...

  9. HDU 2586 How far away ?【LCA模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给你N个点,M次询问.1~N-1行输入点与点之间的权值,之后M行输入两个点(a,b)之间的最 ...

随机推荐

  1. Scala中的数组和集合操作

    package test /* 1.在scala集合中,Iterable是共同的Trait,Iterable要求继承者实现一些共同的方法,例如元素的遍历 * 2.Array是scala基础的数据结构, ...

  2. http协议基础(八)请求首部字段

    请求首部字段 定义:请求首部字段是从客户端到服务器发送请求报文中所使用的字段,里面包含了附加信息.客户端信息以及对响应内容相关的优先级等内容 1.Accept 通知服务器用户代理可处理的媒体类型及媒体 ...

  3. 【剑指Offer学习】【面试题3 :二维数组中的查找】

    package 二维数组查找; public class Test03 { /** * 在一个二维数组中,每一行都按 package 二维数组查找; public class Test03 { /** ...

  4. #C++初学记录(算法考试1)

    B - Maximal Continuous Rest Each day in Berland consists of n hours. Polycarp likes time management. ...

  5. 用python实现一个简单的socket网络聊天通讯 (Linux --py2.7平台与windows--py3.6平台)

    windows   --> windows 写法均在py3.6 客户端写法 import socket client = socket.socket() client.connect(('192 ...

  6. 【codenet】代码相似度计算框架调研 -- 把内容与形式分开

    首发于我的gitpages博客 https://helenawang.github.io/2018/10/10/代码相似度计算框架调研 代码相似度计算框架调研 研究现状 代码相似度计算是一个已有40年 ...

  7. linux常用命令:ps 命令

    Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...

  8. 蓝牙协议 HFP,HSP,A2DP,A2DP_CT,A2DP_TG,AVRCP,OPP,PBAP,SPP,FTP,TP,DTMF,DUN,SDP

    简介: HSP(手机规格)– 提供手机(移动电话)与耳机之间通信所需的基本功能. HFP(免提规格)– 在 HSP 的基础上增加了某些扩展功能,原来只用于从固定车载免提装置来控制移动电话. A2DP( ...

  9. 访问Hsql .data数据库文件

    一.Hsql简介: hsql数据库是一款纯Java编写的免费数据库,许可是BSD-style的协议. 仅一个hsqldb.jar文件就包括了数据库引擎,数据库驱动,还有其他用户界面操作等内容.下载地址 ...

  10. 9大行为导致Java程序员薪资过低, 你有几个?

    Java程序员薪水有高有低,有的人一个月可能拿30K.50K,有的人可能只有2K.3K.同样有五年工作经验的Java程序员,可能一个人每月拿20K,一个拿5K.是什么因素导致了这种差异?本文整理导致J ...