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. C 语言入门第五章--循环结构和选择结构

    C语言中有三大结构,分别是顺序结构.选择结构和循环结构: 逻辑运算: 与运算: && 或运算:|| 非运算:! ==== #include<stdio.h> int mai ...

  2. Intent的常用属性action和category

    设置隐式跳转 首先在我们按钮监听器中添加 Intent i=new Intent(); //参数为字符串,可以添加包名.活动名 i.setAction("com.example.aaaaa. ...

  3. Python可视化 | Seaborn包—kdeplot和distplot

    import pandas as pd import numpy as np import seaborn as sns import matplotlib import matplotlib.pyp ...

  4. Django 3.0的新功能

    谷歌翻译的,我修正并且添加了一些内容.凑合看吧. MariaDB的支持 Django现在正式支持MariaDB 10.1和更高版本.有关更多详细信息,请参见MariaDB注释. ASGI支持 Djan ...

  5. python类的对象使用

    class student():    def __init__(self,age,sex):        self.age = age        self.sex = sex  #self._ ...

  6. arm linux 移植 python3.6

    背景: 人生苦短,我用Python. 说明: 编译Python的嵌入式版需要解释器解析setup.py从而编译Python的模块,因此需要先编译出host的解释器.(有点像Go语言) Python : ...

  7. P2312 解方程(随机化)

    P2312 解方程 随机化的通俗解释:当无法得出100%正确的答案时,考虑随机化一波,于是这份代码很大可能会对(几乎不可能出错). 比如这题:把系数都模一个大质数(也可以随机一个质数),然后O(m)跑 ...

  8. POJ - 1061 青蛙的约会 (扩展欧几里得求同余式)

    题意:两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对 ...

  9. 小程序包大小超过2M的解决方法

    小程序的包被限制在2M以下, 超出的时候点击预览, 发现报错: Error: 代码包大小为 3701 kb,上限为 2048 kb,请删除文件后重试 1. 优化代码, 删除掉不用的代码 2. 图片压缩 ...

  10. 使用nginx做反向代理来访问tomcat服务器

    本次记录的是使用nginx来做一个反向代理来访问tomcat服务器.简单的来说就是使用nginx做为一个中间件,来分发客户端的请求,将这些请求分发到对应的合适的服务器上来完成请求及响应. 第一步:安装 ...