题目大意

题目链接

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
2
3
4
3
2

2

分析

树上三点最短距离为 所有两点距离之和除以二
明白这个就可以了, 还有就是注意别pe……

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <vector>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

typedef unsigned long long ull;
typedef pair<int, int> P;
#define ms(a, b) memset((a), (b), sizeof(a))
const int N = (int)5e4 + 5;
const int M = (int)2e5 + 10;
const int INF = 0x3f3f3f3f;

inline int read() {
int res = 0;bool f = 0;char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
while (ch <= '9' && ch >= '0') { res = (res << 3) + (res << 1) + ch - '0'; ch = getchar(); }
return f ? (~ res + 1) : res;
}

struct edge{
int u, v, w, ne;
}ed[N << 1];

int head[N], dis[N], cnt, n, m;

int deep[N], fa[N][20];

bool vis[N];

inline void init(){
ms(head, -1);
cnt = 0;
ms(deep, 0);
ms(dis, 0);
ms(vis, 0);

}

inline void add(int u, int v, int w){
ed[cnt] = {u, v, w, head[u]};
head[u] = cnt++;
}

inline void dfs(int u, int pre){
deep[u] = deep[pre] + 1; // deep数组表示深度
fa[u][0] = pre; // fa[i][j] 表示i的第2^j辈的祖先
for (int i = 1; i < 20; ++i)
fa[u][i] = fa[fa[u][i - 1]][i - 1];
// fa[i][j] = fa[fa[i][j - 1]][j - 1] i的第2^j辈的祖先 等于 i的第2^(j-1)辈的祖先的第2^(j-1)辈的祖先
for (int i = head[u]; ~i; i = ed[i].ne){
int v = ed[i].v;
if(v == pre) continue;
dis[v] = dis[u] + ed[i].w;
dfs(v, u);
}
}

inline int lca(int a, int b){
if (deep[a] < deep[b]) swap(a, b);
// 运用二进制的思想 使得deep[a] == deep[b] 比如 7 可以用4 2 1表示
for (int i = 19; i >= 0; --i)
if (deep[a] - (1 << i) >= deep[b])
a = fa[a][i];

if (a == b) return a;
// 倍增 2^k 步的爬, 找最小的满足 a != b 的
for (int i = 19; i >= 0; --i){
if (fa[a][i] != fa[b][i])
a = fa[a][i], b = fa[b][i];
}
return fa[a][0];
}
int t, u, v, w, q;
int main(){
t = 1;
while(~scanf("%d", &n)){
if (t++ != 1) // 防pe
putchar(10);
init();
for (int i = 1; i <= n - 1; ++i){
u = read(), v = read(), w = read();
add(u, v, w);
add(v, u, w);
}
dfs(0, -1);
q = read();
while(q--){
u = read(), v = read(), w = read();
int x1 = lca(u, v);
int x2 = lca(u, w);
int x3 = lca(w, v);
printf("%d\n", dis[u] + dis[v] + dis[w] - dis[x1] - dis[x2] - dis[x3]);
}

}
return 0;
}
1
恰似你一低头的温柔,娇弱水莲花不胜寒风的娇羞, 我的心为你悸动不休。  --mingfuyan