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. 基于TF-IDF的推荐

    仅作学习使用 基于TF-IDF的推荐: 将文档分词 对于每个term,计算词频TF和逆文本指数IDF,形成term的权重 计算项目文档和用户偏好文档的相似度 参考: https://blog.csdn ...

  2. 第2节 storm实时看板案例:9、实时看板综合案例

    =================================== 10.实时看板案例 10.1 项目需求梳理 根据订单mq,快速计算双11当天的订单量.销售金额.

  3. 28 最小的K个数

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,.   思路: 解法1:对于小规模数据,可以采用类似前题的快速排序思路 ...

  4. 车林通购车之家--购车计算器模块--算法js

    //CarCalculator.js var checkedClass = "jsq-item-check jsq-item-checked"; var uncheckedClas ...

  5. 如何借助 Python 俘获女孩子芳心?

    责编 | 刘静 天气降温,感情却升温了? 上午刚到公司,就收到小Q发来的灵魂拷问: ​ ​ “Q仔!要不然下午请个假!我带你去精神科看看!?”我实在忍不了,脱口而出. 话音未落,前排的运营小花回头看向 ...

  6. 图解IDEA中配置Maven并创建Maven的Web工程

    打开IDEA,File->Settings,如下图所示: 2.在Settings中按照如下进行配置,如下图所示:

  7. java 解析json格式数据(转)

    2012-07-30 16:43:54|  分类: java |  标签:java  json  |举报|字号 订阅     有时候我们可能会用到json格式的数据进行数据的传输,那么我们怎么把接收到 ...

  8. 攻防世界--web新手练习区(1)

      1. 题目描述:X老师想让小明同学查看一个网页的源代码,但小明却发现鼠标右键不管用了.  http://111.198.29.45:53629 通过阅读题目描述分析,我们需要查看源码,但是鼠标右键 ...

  9. 吴裕雄--天生自然JAVAIO操作学习笔记:字节流与字符流操作

    import java.io.* ; public class Copy{ public static void main(String args[]){ if(args.length!=2){ // ...

  10. ElasticSearch学习,入门篇(一)

    概念解析 1.什么是搜索 搜索就是在任何场景下,找寻你想要的信息,这个时候你会输入一段要搜索的关键字,然后期望找到这个关键字相关的有效信息. 2.如果用数据库做搜素会怎么样 select * from ...