码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 题目2586 How far away ?(LCA)

时间:2015-04-17 22:26:19      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7090    Accepted Submission(s): 2578


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 2874 2888 3234 3461 
 


ac代码

#include<stdio.h>
#include<string.h>
int vis[40040],head[40040],n,m,x[40040],y[40040],dis[40040],cnt,pre[40040],z[40040];
struct s
{
	int u,v,w,next;
}edge[40005*2];
void init()
{
	memset(vis,0,sizeof(vis));
	memset(head,-1,sizeof(head));
	memset(dis,0,sizeof(dis));
	cnt=0;
}
void add(int u,int v,int w)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
int find(int x)
{
	if(x==pre[x])
		return x;
	return find(pre[x]);
}
void tarjan(int u)
{
	int i;
	pre[u]=u;
	vis[u]=1;
	for(i=1;i<=m;i++)
	{
		if(x[i]==u&&vis[y[i]])
			z[i]=find(y[i]);
		else
			if(y[i]==u&&vis[x[i]])
				z[i]=find(x[i]);
	}
	for(i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(!vis[v])
		{
			dis[v]=dis[u]+edge[i].w;
			tarjan(v);
			pre[v]=u;
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		int i;
		init();
		for(i=0;i<n-1;i++)
		{
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w);
			add(v,u,w);
		}
		for(i=1;i<=m;i++)
		{
			scanf("%d%d",&x[i],&y[i]);
		}
		tarjan(1);
		for(i=1;i<=m;i++)
		{
			printf("%d\n",dis[x[i]]+dis[y[i]]-2*dis[z[i]]);
		}
	}
}


 

HDOJ 题目2586 How far away ?(LCA)

标签:

原文地址:http://blog.csdn.net/yu_ch_sh/article/details/45099811

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!