Design the city


Time Limit: 1 Second      Memory Limit: 32768 KB

Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.

In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.

Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.

Input

The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.

Process to the end of file.

Output

Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.

Output a blank line between each test cases.

Sample Input

4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3

Sample Output

3
2 2
2

Author: HE, Zhuobin
Source: ZOJ Monthly, May 2009

题意: 给你一个无向图,然后M个询问,每次询问给你三个点,问你链接这三个点的最短距离

题解:ST算法,分别求出aa=LCA(a,b),bb=LCA(a,c),cc=LCA(c,b);最短距离就是dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc]);

//
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <math.h>
#include <vector>
#include <string.h>
using namespace std;
#define maxn 55000
typedef long long ll;
const int inf = (int)1E9+;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
} //*******************************
int first[maxn],ver[maxn*];
int R[maxn*],head[maxn];
int dist[maxn],t,vis[maxn],tot;
struct ss
{
int from,to,value,next;
} e[maxn*];
int dp[maxn*][];
void init()
{
t=;
memset(head,,sizeof(head));
}
void add(int u,int v,int w)
{
ss kk= {u,v,w,head[u]};
e[t]=kk;
head[u]=t++;
}
void dfs(int u,int dep)
{
vis[u]=;
ver[++tot]=u;
first[u]=tot;
R[tot]=dep;
for(int i=head[u]; i; i=e[i].next)
{
if(!vis[e[i].to])
{
int vv=e[i].to;
dist[vv]=dist[u]+e[i].value;
dfs(vv,dep+);
ver[++tot]=u;
R[tot]=dep;
}
}
}
void find_depth()
{
tot=;
memset(vis,,sizeof(vis));
memset(first,,sizeof(first));///首次出现位置
memset(ver,,sizeof(ver));///节点编号
dfs(,);
}
void ST(int n)
{
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-];
int b=dp[i+(<<(j-))][j-];
dp[i][j]=R[a]>R[b]?b:a;
}
}
}
int RMQ(int x,int y)
{
int k=;
while((<<(k+))<=y-x+)
{
k++;
}
int a=dp[x][k];
int b=dp[y-(<<k)+][k];
return R[a]>R[b]?b:a;
}
int LCA(int x,int y)
{
x=first[x];
y=first[y];
if(x>y)swap(x,y);
return ver[RMQ(x,y)];
}
int main()
{
int n;
int a,b,c;
int T=;
while(scanf("%d",&n)!=EOF)
{
if(T)cout<<endl;
init();
for(int i=; i<n; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a+,b+,c);
add(b+,a+,c);
}
dist[]=;
find_depth();
ST(*n-);
int q=read();
for(int i=; i<=q; i++)
{
scanf("%d%d%d",&a,&b,&c);
a++;
b++;
c++;
int aa=LCA(a,b);
int bb=LCA(a,c);
int cc=LCA(b,c);
cout<<dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc])<<endl;
}
T++;
}
return ;
}

代码

最新文章

  1. 面向科学计算的Python IDE--Anaconda
  2. 算法_bitmap算法
  3. ios移动端部分手机不支持background-attachment: fixed 的解决办法
  4. Git版本控制工具学习
  5. poj1741 树上的点分治
  6. oracle学习 六 删除表空间,数据文件的语句以及导入导出dmp文件的方法(持续更新中)
  7. Java json设置时间格式,Jackson设置时间格式,json设置单引号
  8. Haskell 差点儿无痛苦上手指南
  9. Lazarus中TreeView导出XML以及XML导入TreeView
  10. 使用GDB调试Android NDK native(C/C++)程序-转
  11. 错误:Cannot set property &#39;innerHTML&#39; of null
  12. FFmpeg的HEVC解码器源代码简单分析:解码器主干部分
  13. 1049. Counting Ones (30)
  14. C++ 死循环在语言层面的检测
  15. Javascript获取服务器时间
  16. SharePoint 2013 解惑 无法打开文件浏览器
  17. CNPM
  18. 前端之css样式(选择器)。。。
  19. VS 2013 无法启动IIS Express Web 服务器
  20. --num 与 num-- 的区别

热门文章

  1. 【VScode】使用VScode 来写markdown时序图
  2. MyBatis 实现分页功能
  3. [Python3网络爬虫开发实战] 7-动态渲染页面爬取
  4. php扩展1:filp/whoops(用于调试,方便定位错误点)
  5. Go:slice
  6. WinMain名词解析
  7. 集训第五周动态规划 F题 最大子矩阵和
  8. java 通过反射机制调用某个类的方法
  9. LOJ#541. 「LibreOJ NOIP Round #1」七曜圣贤
  10. codevs3728 联合权值