How far away ?
http://acm.hdu.edu.cn/showproblem.php?pid=2586
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23138 Accepted Submission(s): 9166
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
Recommend
lcy | We have carefully selected several similar problems for you: 3486 2888 3234 2818 3038
裸裸的裸题。就是求一棵树上的两点间的最短距离。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=80000+100;
const int maxm=80000+100;
int cnt,num,head[maxn];
int first[maxn],que[maxn];
int depth[maxn];
int dp[maxn][20];
int dist[maxn];
bool vis[maxn];
struct node{
int to,next,val;
}G[maxm<<1];
void init(){
memset(head,-1,sizeof(head));
memset(vis,false,sizeof(vis));
cnt=0;
num=0;
}
void add(int a,int b,int c){
G[++cnt].to=b;
G[cnt].val=c;
G[cnt].next=head[a];
head[a]=cnt;
}
int dfs(int u,int deep){
vis[u]=true;///表示是否访问过
que[++num]=u;///num在此处相当于dfs_clock
first[u]=num;///表示第一次走的编号
depth[num]=deep;
for(int i=head[u];i!=-1;i=G[i].next){
int v=G[i].to;
int w=G[i].val;
if(!vis[v]){
dist[v]=dist[u]+w;
dfs(v,deep+1);
que[++num]=u;
depth[num]=deep;
}
}
}
void ST(int n)
{
for(int i=1;i<=n;i++)
dp[i][0] = i;
for(int j=1;(1<<j)<=n;j++){
for(int i=1;i+(1<<j)-1<=n;i++){
int a = dp[i][j-1] , b = dp[i+(1<<(j-1))][j-1];
dp[i][j] = depth[a]<depth[b]?a:b;
}
}
}
//两个节点之间的路径深度最小的就是LCA
int RMQ(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1)
k++;
int a = dp[l][k], b = dp[r-(1<<k)+1][k]; //保存的是编号
return depth[a]<depth[b]?a:b;
}
int LCA(int u ,int v)
{
int x = first[u] , y = first[v];
if(x > y) swap(x,y);
int res = RMQ(x,y);
return que[res];
}
int main()
{
int t,n,m,a,b,c;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=n-1;i++){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dfs(1,1);
ST(2*n-1);
while(m--){
scanf("%d%d",&a,&b);
int lca=LCA(a,b);
///cout<<"lca:"<<lca<<endl;
///cout<<dist[a]<<" "<<dist[b]<<" "<<endl;
printf("%d\n",dist[a]+dist[b]-2*dist[lca]);
}
}
return 0;
}