How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3653    Accepted Submission(s): 1379

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
 
题目分析:题目中说有n个房子,有n-1条道路,并且都是联通的,典型的树状图,要求任意两点的距离,可以用线段树做,此处不提,用最短路
n的范围是4000,用一般最短路的方法一定会超时,因此利用树状结构的特点,最好用LCA;
程序:
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"queue"
#include"map"
using namespace std;
#define M 40005
int dis[M],pre[M],head[M],t,sum,use[M],rank[M];
struct st
{
int u,v,w,next;
}edge[M*3];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void bfs(int s)
{
queue<int>q;
memset(use,0,sizeof(use));
memset(rank,0,sizeof(rank));
use[s]=1;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(!use[v])
{
use[v]=1;
pre[v]=u;//记录父节点;
rank[v]=rank[u]+1;//记录层数
dis[v]=edge[i].w;//记录子节点到父节点的距离
q.push(v);
}
}
}
}
void targan(int a,int b)
{
if(a==b)
return;
else if(rank[a]>rank[b])
{
sum+=dis[a];
targan(pre[a],b);
}
else
{
sum+=dis[b];
targan(a,pre[b]);
}
}//用深搜的方法求最近公共祖先;sum记录路径长度
int main()
{
int w,a,b,c,i,m,n;
scanf("%d",&w);
while(w--)
{
init();
scanf("%d%d",&n,&m);
for(i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
bfs(1);
while(m--)
{
scanf("%d%d",&a,&b);
sum=0;
targan(a,b);
printf("%d\n",sum);
}
}
}






hdu2586(LCA最近公共祖先)的更多相关文章

  1. lca 最近公共祖先

    http://poj.org/problem?id=1330 #include<cstdio> #include<cstring> #include<algorithm& ...

  2. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)

    Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...

  3. LCA(最近公共祖先)模板

    Tarjan版本 /* gyt Live up to every day */ #pragma comment(linker,"/STACK:1024000000,1024000000&qu ...

  4. CodeVs.1036 商务旅行 ( LCA 最近公共祖先 )

    CodeVs.1036 商务旅行 ( LCA 最近公共祖先 ) 题意分析 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从 ...

  5. LCA近期公共祖先

    LCA近期公共祖先 该分析转之:http://kmplayer.iteye.com/blog/604518 1,并查集+dfs 对整个树进行深度优先遍历.并在遍历的过程中不断地把一些眼下可能查询到的而 ...

  6. LCA 近期公共祖先 小结

    LCA 近期公共祖先 小结 以poj 1330为例.对LCA的3种经常使用的算法进行介绍,分别为 1. 离线tarjan 2. 基于倍增法的LCA 3. 基于RMQ的LCA 1. 离线tarjan / ...

  7. LCA最近公共祖先 ST+RMQ在线算法

    对于一类题目,是一棵树或者森林,有多次查询,求2点间的距离,可以用LCA来解决.     这一类的问题有2中解决方法.第一种就是tarjan的离线算法,还有一中是基于ST算法的在线算法.复杂度都是O( ...

  8. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】

    一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...

  9. (转)Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)

    基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...

随机推荐

  1. cakephp文件结构

    一个项目的开发会用到cakephp的那些文件呢? 如果你的项目使用cake1.3.6,那么可以参考下面的内容   根据我的经验,会涉及一下文件夹: config controllers models ...

  2. Phpcms v9 实现首页|列表页|内容页调用点击量的代码

    很多朋友经常问Phpcms v9的首页.列表页.内容页点击量如何调用.今天给大家分享phpcms V9如何分别在首页.列表页.内容页调用点击量代码 1,Phpcms v9首页调用点击量 {pc:con ...

  3. linux避免crontab的执行输出将磁盘目录占满?用户的mail占用大的空间?

    需求描述: 早上设置了ntp客户端的定时任务,发现不断的有You have new mail in /var/spool/mail/root这种提示. 然后,就看了具体的文件,由于ntpdate是每分 ...

  4. iOS AppsFlyer的使用注意事项

    AppFlyer 是近期比較火的一款广告追踪统计工具,当然统计的功能友盟也能够实现,而appsflyer更是具有定向投放,是app跳转到对应的页面. 详细的:当点击广告的时候,假设没有安装应用.则会跳 ...

  5. [Unity官方文档翻译]Primitive and Placeholder Objects Unity原生3D物体教程

    Primitive and Placeholder Objects 原始的基础物体 Unity can work with 3D models of any shape that can be cre ...

  6. ios开发之--sizeToFit的用法

    sizeToFit :即当前视图便捷和便捷大小变化(自动根据文本大小改变自身的宽度) 代码如下: - (void)sizeToFitDemo { UILabel * label = [[UILabel ...

  7. 使用 urllib 处理 HTTP 异常

    (1) 我们发起 HTTP 请求,有时会发生异常,如请求超时,登录密码错误,请求链接不存在等等,使用 urllib.request.URLError 可以捕获这些与 URL 相关的异常(2) urll ...

  8. 最简单的GLSL,Shader

    Vertex Shader void main() { gl_FrontColor = gl_Color; gl_Position = ftransform(); } Fragment Shader ...

  9. creat-react-app 如何在组件中img的src引入图片路径??

    把图片文件夹放到public中,然后以这种方式来动态写路径: process.env.PUBLIC_URL + '/img/' + url + '.jpg'

  10. MVC的简单分页【转】

    传值的方式是通过querystring. 本例子是把整需要的数据查出来再分页的,因为当时做的时候数据很少,只有几十条. 如果数据多的话,可以在存储过程里分页,只是要传页码和记录的条数过来. 控制器: ...