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

三个点的距离等于任意两点间距离加和除以2

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
inline void read(int &x)
{
char ch = getchar();char c = ch;x = 0;
while(ch < '0' || ch > '9')c = ch, ch = getchar();
while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
if(c == '-')x = -x;
}
inline void swap(int& a, int& b){int tmp = a;a = b;b = tmp;}
const int MAXN = 50000 + 10;
struct Edge{int u,v,w,next;}edge[MAXN << 1];
int head[MAXN], cnt, n, m;
inline void insert(int a,int b, int c){edge[++cnt] = Edge{a,b,c,head[a]};head[a] = cnt;}
int log2[MAXN], pow2[30];
int p[30][MAXN], deep[MAXN], len[MAXN];int b[MAXN]; void dfs(int u)
{
for(int pos = head[u];pos;pos = edge[pos].next)
{
int v = edge[pos].v;
if(b[v])continue;
b[v] = true;
len[v] = len[u] + edge[pos].w;
deep[v] = deep[u] + 1;
p[0][v] = u;
dfs(v);
}
} inline void yuchuli()
{
b[1] = true;
deep[1] = 0;
dfs(1);
for(register int i = 1;i <= log2[n];i ++)
for(register int j = 1;j <= n;j ++)
p[i][j] = p[i - 1][p[i - 1][j]];
} inline int lca(int va, int vb)
{
if(deep[va] < deep[vb])swap(va,vb);
for(register int i = log2[n];i >= 0;i --)
if(deep[va] - pow2[i] >= deep[vb])
va= p[i][va];
if(va == vb)return va;
for(register int i = log2[n];i >= 0;i --)
{
if(p[i][va] != p[i][vb])
{
va = p[i][va];
vb = p[i][vb];
}
}
return p[0][va];
} inline int l(int va, int vb)
{
int k = lca(va, vb);
return len[va] + len[vb] - (len[lca(va, vb)] << 1);
} int main()
{
register int tmp1,tmp2,tmp3;
log2[0] = -1;
for(register int i = 1;i <= MAXN;++ i)log2[i] = log2[i >> 1] + 1;
pow2[0] = 1;
for(register int i = 1;i <= 30;++ i)pow2[i] = pow2[i - 1] << 1;
bool ok = false;
while(scanf("%d", &n) != EOF)
{
if(ok)putchar('\n'),putchar('\n');
cnt = 0;memset(head, 0, sizeof(head));
memset(edge, 0, sizeof(edge));
memset(deep, 0, sizeof(deep));
memset(p, 0, sizeof(p));m = 0;
memset(b, 0, sizeof(b));
memset(len, 0, sizeof(len));
for(register int i = 1;i < n;++ i)
{
read(tmp1);read(tmp2);read(tmp3);
insert(tmp1 + 1, tmp2 + 1, tmp3);
insert(tmp1 + 1, tmp1 + 1, tmp3);
}
yuchuli();
read(m);
read(tmp1);read(tmp2);read(tmp3);
++ tmp1;++ tmp2;++ tmp3;
printf("%d", (l(tmp1, tmp2) + l(tmp2, tmp3) + l(tmp1, tmp3))>> 1);
for(register int i = 2;i <= m;++ i)
{
read(tmp1);read(tmp2);read(tmp3);
tmp1 ++;tmp2 ++;tmp3 ++;
int a = l(tmp1, tmp2);int b = l(tmp2, tmp3);int c = l(tmp1, tmp3);
printf("\n%d", (l(tmp1, tmp2) + l(tmp2, tmp3) + l(tmp1, tmp3))>> 1);
}
ok = true;
}
return 0;
}

ZOJ3195 Design the city [2017年6月计划 树上问题04]的更多相关文章

  1. HDU3887 Counting Offspring [2017年6月计划 树上问题03]

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. 洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]

    [POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byte ...

  3. 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]

    P2912 [USACO08OCT]牧场散步Pasture Walking 题目描述 The N cows (2 <= N <= 1,000) conveniently numbered ...

  4. ZOJ3195 Design the city(LCA)

    题目大概说给一棵树,每次询问三个点,问要把三个点连在一起的最少边权和是多少. 分几种情况..三个点LCA都相同,三个点有两对的LCA是某一点,三个点有两对的LCA各不相同...%……¥…… 画画图可以 ...

  5. [zoj3195]Design the city(LCA)

    解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ //#pragma comment(linker, "/S ...

  6. RQNOJ PID192 梦幻大PK [2017年6月计划 二分图02]

    PID192 / 梦幻大PK ☆ 提交你的代码 查看讨论和题解 你还木有做过哦 我的状态         查看最后一次评测记录 质量 7 题目评价 质量 7 ★★★★★ ★★★★☆ ★★★☆☆ ★★☆ ...

  7. 洛谷P1368 均分纸牌(加强版) [2017年6月计划 数论14]

    P1368 均分纸牌(加强版) 题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,纸牌总数必为 N 的倍数.可以在任一堆上取1张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取 ...

  8. 洛谷P1621 集合 [2017年6月计划 数论13]

    P1621 集合 题目描述 现在给你一些连续的整数,它们是从A到B的整数.一开始每个整数都属于各自的集合,然后你需要进行一下的操作: 每次选择两个属于不同集合的整数,如果这两个整数拥有大于等于P的公共 ...

  9. 洛谷P1390 公约数的和 [2017年6月计划 数论12]

    P1390 公约数的和 题目描述 有一天,TIBBAR和LXL比赛谁先算出1~N这N个数中每任意两个不同的数的最大公约数的和.LXL还在敲一个复杂而冗长的程序,争取能在100s内出解.而TIBBAR则 ...

随机推荐

  1. CF1158F Density of subarrays

    CF1158F Density of subarrays 首先可以发现,有值的p最大是n/c 对于密度为p,每个数至少出现c次,且其实是每出现c个数,就分成一段,这样贪心就得到了p %ywy n/c ...

  2. 为什么说 Python 是数据科学的发动机(一)发展历程(附视频中字)

    为什么说 Python 是数据科学的发动机(一)发展历程(附视频中字) 在PyData Seattle 2017中,Jake Vanderplas介绍了Python的发展历程以及最新动态.在这里我们把 ...

  3. 转:链表相交有环 经典面试题(三)附答案 算法+数据结构+代码 微软Microsoft、谷歌Google、百度、腾讯

    源地址:http://blog.csdn.net/sj13051180/article/details/6754228 1.判断单链表是否有环,要求空间尽量少(2011年MTK) 如何找出环的连接点在 ...

  4. Spring SpringMVC SpringBoot SpringCloud 注解整理大全

    Spring SpringMVC SpringBoot SpringCloud 注解整理 才开的博客所以放了一篇以前整理的文档,如果有需要添加修改的地方欢迎指正,我会修改的φ(๑˃∀˂๑)♪ Spri ...

  5. 16_k近邻算法总结

    1.k近邻算法属于分类算法 2.你的“邻居”来推断出你的类别 3.标准定义:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别. 4.计算 ...

  6. python3-常用模块之sys

    import syssys 是和Python解释器打交道的sys.argvprint(sys.argv) # argv的第一个参数 是python这个命令后面的值 主要用途 1. 程序员 运维人员 在 ...

  7. C#窗体随意移动

    //全区域移动 const int WM_NCLBUTTONDOWN = 0xA1; const int HT_CAPTION = 0x2; [DllImport("user32.dll&q ...

  8. appscan如何扫描移动应用APP

    1.前置条件:让手机和电脑处于同一WIFI下 1打开appscan,选择手动探索/外部设备. 2在弹出的对话框页面点击右上角“记录代理配置”. 3在弹出的页面选择记录代理页签,设置Appscan代理端 ...

  9. cdh_hadoop下载地址

    http://archive.cloudera.com/cdh5/cdh/5/

  10. jqGrid的subGrid子表格

    使用完整jqGrid作为子表格 使用子表格,涉及到jqGrid的三个选项: subGrid :首先必须将jqGrid的subGrid选项设置为true,默认为false:当此项设为true的时候,Gr ...