题目链接: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.
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
题意,给你n个点,n-1条边,求m个查询下两点的最短距离。。因为n和m都很大,所以用LCA离线tarjan算法,时间为O(n+m)
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=80005;
struct node
{
int from,to,lca,next;
}e1[maxn],e2[maxn];
int head1[maxn],head2[maxn];
int vis[maxn],dist[maxn],p[maxn];
int n,m,cnt1,cnt2;
void add1(int x,int y,int z)
{
e1[cnt1].from=x;
e1[cnt1].to=y;
e1[cnt1].lca=z;
e1[cnt1].next=head1[x];
head1[x]=cnt1++;
}
void add2(int x,int y)
{
e2[cnt2].from=x;
e2[cnt2].to=y;
e2[cnt2].lca=-1;
e2[cnt2].next=head2[x];
head2[x]=cnt2++;
}
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
void LCA(int u)
{
p[u]=u;
vis[u]=1;
for(int k=head1[u];k!=-1;k=e1[k].next)
{
if(!vis[e1[k].to])
{
dist[e1[k].to]=dist[u]+e1[k].lca;
LCA(e1[k].to);
p[e1[k].to]=u;
}
}
for(int k=head2[u];k!=-1;k=e2[k].next)
{
int v=e2[k].to;
if(vis[v])
{
int t=find(v);
e2[k].lca=dist[v]-2*dist[t]+dist[u];
e2[k^1].lca=e2[k].lca;
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
cnt1=0;
cnt2=0;
memset(head1,-1,sizeof(head1));
memset(head2,-1,sizeof(head2));
memset(vis,0,sizeof(vis));
memset(p,-1,sizeof(p));
memset(dist,0,sizeof(dist));
scanf("%d %d",&n,&m);
int x,y,z;
for(int i=1;i<n;i++)
{
scanf("%d %d %d",&x,&y,&z);
add1(x,y,z);
add1(y,x,z);
}
for(int i=1;i<=m;i++)
{
scanf("%d %d",&x,&y);
add2(x,y);
add2(y,x);
}
//printf("???");
for(int i=1;i<=n;i++)
if(!vis[i])
LCA(i);
for(int i=0;i<cnt2;i+=2)
{
int u=e2[i].from;
int v=e2[i].to;
printf("%d\n",e2[i].lca);
}
}
return 0;
}