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. hdu 1086 You can Solve a Geometry Problem too 求n条直线交点的个数

    You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3 ...

  2. shell脚本中执行sql脚本(mysql为例)

    1.sql脚本(t.sql) insert into test.t value ("LH",88); 2.shell脚本(a.sh     为方便说明,a.sh与t.sql在同一目 ...

  3. 被动信息收集-其他收集目标信息的途径:cupp、 recon-ng

    除了google等搜索收集,还有其他途径进行信息收集,其中就包括用命令行或集成的软件.框架进行搜集信息. 1.先举例几个简单的命令: 其实也会是调用搜索引擎,如谷歌必应等,需要翻墙,可以用proxyc ...

  4. Python—网络通信编程之udp通信编程

    服务端代码 import socket # 1.创建实例,即数据报套接字 server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 2.绑 ...

  5. 099、Java中String类之字符数组与字符串的转换

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  6. CentOS7虚拟机配置、Hadoop搭建、wordCount DEMO运行

    安装虚拟机 最开始先安装虚拟机,我是12.5.7版本,如果要跟着我做的话,版本最好和我一致,不然后面可能会出一些莫名其妙的错误,下载链接如下(注册码也在里面了): 链接:https://pan.bai ...

  7. k-近邻算法原理入门-机器学习

    //2019.08.01下午机器学习算法1——k近邻算法1.k近邻算法是学习机器学习算法最为经典和简单的算法,它是机器学习算法入门最好的算法之一,可以非常好并且快速地理解机器学习的算法的框架与应用.2 ...

  8. 吴裕雄--天生自然JAVAIO操作学习笔记:File类

    import java.io.File ; import java.io.IOException ; public class FileDemo01{ public static void main( ...

  9. docker-lnmp 多容器部署 Laravel 方案分享(转)

    docker lnmp 多容器部署方案.完全基于 docker 官方镜像,遵循最佳实践,一容器一进程. github 项目地址 https://github.com/March7/docker-lnm ...

  10. 2013蓝桥杯预赛C/C++本科B组

    题目标题: 高斯日记 大数学家高斯有个好习惯:无论如何都要记日记. 他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210 后来人们知道,那个整数就是日期,它表示那一天是高斯 ...