HDU 2586 How far away ?
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
ECJTU 2009 Spring Contest
Recommend
lcy | We have carefully selected several similar problems for you: 3486 2874 2888 3234 2818
带权的LCA问题
我们用g[i]表示i号节点走到根的权值
那么两个点之间的路径权值为,g[x]+g[y]-2*g[LCA(x,y)]
大概是这个样子
被圆圈出来的是需要减去的
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 const int MAXN=1e5+10;
6 inline int read()
7 {
8 char c=getchar();int x=0,f=1;
9 while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
10 while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();return x*f;
11 }
12 int n,m,S=1;
13 int f[MAXN][21],deep[MAXN],g[MAXN];
14 struct node
15 {
16 int u,v,w,nxt;
17 }edge[MAXN];
18 int head[MAXN];
19 int num=1;
20 inline void add_edge(int x,int y,int z)
21 {
22 edge[num].u=x;
23 edge[num].v=y;
24 edge[num].w=z;
25 edge[num].nxt=head[x];
26 head[x]=num++;
27 }
28 void dfs(int now)
29 {
30 for(int i=head[now];i!=-1;i=edge[i].nxt)
31 if(deep[edge[i].v]==0)
32 {
33 deep[edge[i].v]=deep[now]+1;
34 f[edge[i].v][0]=now;
35 g[edge[i].v]=g[now]+edge[i].w;
36 dfs(edge[i].v);
37 }
38
39 }
40 inline void pre()
41 {
42 for(int i=1;i<=19;i++)
43 for(int j=1;j<=n;j++)
44 f[j][i]=f[f[j][i-1]][i-1];
45 }
46 inline int LCA(int x,int y)
47 {
48 if(deep[x]<deep[y]) swap(x,y);
49 for(int i=19;i>=0;i--)
50 if(deep[f[x][i]]>=deep[y])
51 x=f[x][i];
52 if(x==y) return x;
53
54 for(int i=19;i>=0;i--)
55 if(f[x][i]!=f[y][i])
56 x=f[x][i],y=f[y][i];
57 return f[x][0];
58 }
59 int main()
60 {
61 int T=read();
62 while(T--)
63 {
64 n=read();m=read();
65 memset(head,-1,sizeof(head));num=1;
66 memset(f,0,sizeof(f));
67 memset(deep,0,sizeof(deep));
68 for(int i=1;i<=n-1;i++)
69 {
70 int x=read(),y=read(),z=read();
71 add_edge(x,y,z);
72 add_edge(y,x,z);
73 }
74 deep[S]=1;
75 dfs(S);pre();
76 while(m--)
77 {
78 int x=read(),y=read();
79 printf("%dn",g[x]+g[y]-2*g[LCA(x,y)]);
80 }
81 }
82
83 return 0;
84 }
- 【Go 语言社区】各种变量的声明
- 【Go 语言社区】Golang 高效字符串拼接
- 实战 | Elasticsearch实现类Google高级检索
- Golang中time包用法--转
- 干货 | Elasticsearch 集群健康值红色终极解决方案
- Go语言interface的value.(type)使用小技巧-转
- 干货 | Elasticsearch5.X Mapping万能模板
- MySQL 5.7安装部署总结(r10笔记第77天)
- Go语言中Socket通信TCP服务端
- MySQL和Oracle的添加字段的处理差别 (r10笔记第73天)
- MySQL修改数据类型的问题总结(r10笔记第74天)
- 深究|Elasticsearch单字段支持的最大字符数?
- Go语言中Socket通信之Tcp客户端
- Oracle 12c PDB迁移及ORA-00600错误分析和解决(r10笔记第72天)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- CentOS 7如何实现定时执行python脚本
- PHP自动生成缩略图函数的源码示例
- 解决tensorflow 释放图,删除变量问题
- php生成word并下载代码实例
- TensorFlow保存TensorBoard图像操作
- 浅谈PHP SHA1withRSA加密生成签名及验签
- PHP PDO数据库操作预处理与注意事项
- laravel 框架配置404等异常页面
- Django –Xadmin 判断登录者身份实例
- Laravel 队列使用的实现
- keras 两种训练模型方式详解fit和fit_generator(节省内存)
- Keras 中Leaky ReLU等高级激活函数的用法
- Python错误的处理方法
- python3的pip路径在哪
- MySQL 案例:init_connect 引发的 Lost connection