hdu2586(LCA最近公共祖先)
2024-09-17 09:17:50
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.
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
题目分析:题目中说有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);
}
}
}
最新文章
- react 表单
- [COPY] How to become a hacker
- Android的消息处理机制Looper,Handler,Message
- group by 获取总记录数
- CleanMyMac2清理 lanchpad里面的图标没了
- PHP学习笔记 - 入门篇(5)
- asp.net中的Application概述
- 你好,C++(4)2.1.3 我的父亲母亲:编译器和链接器 2.1.4 C++程序执行背后的故事
- 有关mysql数据库的编码
- python操作redis-过期时间
- React VR 技术开发群 579149907
- Java精选笔记_多线程(创建、生命周期及状态转换、调度、同步、通信)
- ARM、MCU、DSP、FPGA、SOC各是什么?区别是什么?(转)
- 170627、springboot编程之定时任务
- Hive0.13.1介绍及安装部署
- 常用移动web开发框架--转载
- linux动态链接库
- iOS之蓝牙开发—CoreBluetooth详解
- Selenium2+python自动化46-js解决click失效问题【转载】
- MATLAB循环和函数定义,调用
热门文章
- jsp新建项目
- arcview、arcinfo、arceditor的区别
- [RN] 02 - Overview: React Native Practice of 50 lectures
- AES-128-CBC加密
- Matlab 随机数字
- NetBpm 安装篇(1)
- Windows系统下运行某些程序时缺少“Msflxgrd.ocx”的解决方法
- 【AI】face_recognition
- Nginx(十二)-- Nginx+keepalived实现高可用
- 如何使Ubuntu Linux12.04 LTS版可以用root用户登陆