How far away ?

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

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个节点的带权树,m次询问,求两点之间的距离
第一行输入t,表示多个测试样例
第二行输入n,m,表示n个节点和m次询问
接下来n-1行,每行输入x,y,w,表示x到y的距离为w
最后m行每行输入两个数xx,yy,询问xx到yy的最短距离为多少
 
这题给人的感觉就是最短路可以做,但是注意数据范围40000,开不了40000的二维数组,所以直接gg
但其实就是简单的最近公共祖先问题,找到x,y两点最近的公共祖先节点z[i],dis[x]+dis[y]-2*dis[z[i]]就是答案
 
 
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
int id;
int len;
}p;
vector<node>mp[];//结构体动态数组
int dis[],xx[],yy[],vis[],z[],fa[];//dis是存距离,xx,yy存询问的点,z[i]表示i的LCA是点z[i],fa是存父亲节点
int t,n,m;
void add(int a,int b,int c)//加边建树
{
p.id=b;
p.len=c;
mp[a].push_back(p);
}
void init()//初始化
{
for(int i=;i<=n;i++)
{
fa[i]=i;
mp[i].clear();
dis[i]=;
vis[i]=;
z[i]=;
}
}
int find(int x)
{
if(fa[x]!=x)
return fa[x]=find(fa[x]);
return fa[x]; } void join(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y)
fa[y]=x;
}
void tarjan(int x)
{
vis[x]=;
for(int i=;i<mp[x].size();i++)
{
int y=mp[x][i].id;
if(!vis[y])//未遍历过
{
dis[y]=dis[x]+mp[x][i].len;//更新距离
tarjan(y);//遍历下一个点
join(x,y);//合并
} }
for(int i=;i<=m;i++)
{
if(xx[i]==x&&vis[yy[i]])
z[i]==find(yy[i]);
if(yy[i]==x&&vis[xx[i]])
z[i]=find(xx[i]);
} }
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
int a,b,c;
init();
for(int i=;i<n;i++)//描述边
{
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
for(int i=;i<=m;i++)//询问
{
cin>>xx[i]>>yy[i];
}
tarjan();
for(int i=;i<=m;i++)
cout<<dis[xx[i]]+dis[yy[i]]-*dis[z[i]]<<endl;
}
return ;
}

hdu 2583 How far away ? 离线算法 带权求最近距离的更多相关文章

  1. HDU 2255 奔小康赚大钱(带权二分图最大匹配)

    HDU 2255 奔小康赚大钱(带权二分图最大匹配) Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊 ...

  2. HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  3. HDU 4605 Magic Ball Game(离线算法)

    题目链接 思路就很难想+代码实现也很麻烦,知道算法后,已经写的很繁琐而且花了很长时间,200+,好久没写过这么长的代码了. #pragma comment(linker, "/STACK:1 ...

  4. HDU 2838 (DP+树状数组维护带权排序)

    Reference: http://blog.csdn.net/me4546/article/details/6333225 题目链接: http://acm.hdu.edu.cn/showprobl ...

  5. hdu 3038 How Many Answers Are Wrong ( 带 权 并 查 集 )

    How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. HDU 3038 How Many Answers Are Wrong(带权并查集)

    太坑人了啊,读入数据a,b,s的时候,我刚开始s用的%lld,给我WA. 实在找不到错误啊,后来不知怎么地突然有个想法,改成%I64d,竟然AC了 思路:我建立一个sum数组,设i的父亲为fa,sum ...

  7. KM算法 带权二分匹配 O(n^3)

    #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #inclu ...

  8. HDU Virtual Friends(超级经典的带权并查集)

    Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. 奔小康赚大钱---hdu2255(最大带权匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 带权匹配问题的模板: 运用KM算法: #include<stdio.h> #incl ...

随机推荐

  1. Kafka源码系列之源码分析zookeeper在kafka的作用

    浪尖的kafka源码系列以kafka0.8.2.2源码为例给大家进行讲解的.纯属个人爱好,希望大家对不足之处批评指正. 一,zookeeper在分布式集群的作用 1,数据发布与订阅(配置中心) 发布与 ...

  2. rendering path定义了什么?有哪些?有什么作用?有什么限制?

    rendering path 定义了光照在一个shader pass中处理的对象与顺序,如逐像素处理,逐顶点处理,向前处理,后向 deferred用于处理不透明物体,会先把它们根据深度检测,过滤后的内 ...

  3. H5禁止底部横向滚动条,使一个元素居中

    1.禁止底部横向滚动条 选择元素设置样式 { overflow-y:auto; overflow-x:hidden } 2.元素居中 { margin-left:auto ; margin-right ...

  4. 《O’Reilly精品图书系列共21册》azw3

    套装书目: <机器学习:实用案例解析> <利用Python进行数据分析> <社交网站的数据挖掘与分析(原书第2版)> <社会网络分析:方法与实践> &l ...

  5. 前端学习笔记系列一:8 <noscript>…</noscript>,网站路径,vscode跳出右括号

    1.<noscript>…</noscript> 在body中使用此段代码,可识别 <script> 标签但无法支持其中的脚本的浏览器. 此段代码意思为如果浏览器不 ...

  6. SSH框架结构分析

    分类: 工作问题2012-03-29 18:10 1511人阅读 评论(0) 收藏 举报 框架sshhibernatespringstrutsdao 最近在弄j2ee,发现还是学到了很多东西,心情ha ...

  7. UVALive 3231 网络流

    题目要求给m个任务分配给n个机器,但最后任务量最多的那个机器的任务量尽量少,利用最大流,在最后的汇点那里设置关卡,二分结果,把机器到最终汇点的容量设置为该值,这样就达到题目条件,这样跑最大流 还能把m ...

  8. 吴裕雄--天生自然JAVA面向对象高级编程学习笔记:instanceof关键字

    class A{ // 定义类A public void fun1(){ // 定义fun1()方法 System.out.println("A --> public void fun ...

  9. Python测试进阶——(2)配置PyCharm远程调试环境

    新建一个Python项目 配置Deployment,用于本地文件和远程文件的同步,在pycharm的菜单栏依次找到:Tools > Deployment > Configuration 点 ...

  10. Java从.CSV文件中读取数据和写入

    .CSV文件是以逗号分割的数据仓储,读取数据时从每一行中读取一条数据元祖,也就是一条数据,再用字符分割的方式获取表中的每一个数据项. import java.io.BufferedReader;    ...