题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2586
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
题意:
给定一个带权的树(双向),节点编号1...n,询问2个节点之间的最近距离。
t组测试数据。 每组数据先给定n,m表示n个点m次询问。
接下来n-1行,表示n-1条边,每条边给定a,b,c,表示a,b边权值为c。
题解:
这题是为了学LCA学的,LCA是最近公共祖先问题,分为离线和在线查询两种,离线查询一般用Tarjan算法比较高效,在线算法则使用ST倍增。 ST倍增以前稍微看过一下,个人感觉比较难以理解,相比之下tarjan则好理解很多。
关于tarjan算法可以参考 https://www.cnblogs.com/JVxie/p/4854719.html , 个人感觉在学过DFS和并查集的前提下半个多小时足够搞懂了。
这个题因为是第一次敲tarjan,对建树之类的不太熟悉,所以大致是跟着网上找的模板写的。大体思路就是,任意找一个节点作为根,采用邻接表的方式按有向图存储树。
用dis数组记录每个节点到根节点的距离,vis标记节点的访问情况(避免递归的时候走回头路)。因为在树上一个节点,网上找其任意一个祖先节点,路径只有一条。 所以找2个节点的最短路,这2个节点肯定有1个最近的公共祖先,这个祖先分别到2个节点的距离之和就是最短路。 所以如果求s,t的距离,则结果result = dis[s] + dis[t] -2*dis[LCA(s,t)].
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define MAXN 44000
using namespace std;
int n,m;
bool visited[MAXN]; // 标记某个节点是否访问过
bool handled[MAXN]; // 标记tarjan
int dis[MAXN]; // 到根节点距离
int pre[MAXN]; // 并查集中的父节点
int result[MAXN];
struct Node {
int to,weight;
};
vector<Node> graph[MAXN];
vector<int> query[MAXN],number[MAXN]; //询问,询问的编号
// 路径压缩查找祖先节点
int Find(int x) {
if(pre[x] == x) return x;
return pre[x] = Find(pre[x]);
}
// 初始化
void Init() {
memset(dis,0,sizeof(dis));
memset(visited,0,sizeof(visited));
memset(handled,0,sizeof(handled));
for(int i = 0;i <= n;i++) {
graph[i].clear(); // 清空图
query[i].clear();
number[i].clear();
pre[i] = i;
}
}
void Tarjan(int root,int len) {
dis[root] = len;
visited[root] = true;
int size = graph[root].size(); // 邻接节点数
for(int i = 0;i < size;i++) {
if(visited[graph[root][i].to]) continue;
Tarjan(graph[root][i].to, len+graph[root][i].weight);
pre[graph[root][i].to] = root; // 合并节点
}
// 处理与根节点相关的询问
size = query[root].size();
for(int i = 0;i < size;i++) {
if(handled[number[root][i]])
result[number[root][i]] = dis[root] + dis[query[root][i]] - 2*dis[Find(query[root][i])];
handled[number[root][i]] = true;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--) {
scanf("%d%d",&n,&m);
Init();
int a,b,c;
// 输入边
for(int i = 1;i < n;i++) {
scanf("%d%d%d",&a,&b,&c);
graph[a].push_back((Node){b,c});
graph[b].push_back((Node){a,c});
}
// m次询问
for(int i = 0;i < m;i++) {
scanf("%d%d",&a,&b);
query[a].push_back(b);
number[a].push_back(i);
query[b].push_back(a);
number[b].push_back(i);
}
Tarjan(1,0);
for(int i = 0;i < m;i++)
printf("%d\n",result[i]);
}
return 0;
}