HDU - 2586

Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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

Source

题意是给你一棵带权树,以及任意两点,让你算出两点的距离。那么可以通过求两点的最近公共祖先(LCA),然后两点到根节点的距离的和减去两倍最近公共祖先到根节点的距离就是两点的距离了。求LCA可以用tarjan,是利用dfs的性质加上并查集找祖先的功能实现的。具体实现看代码

可以参考http://blog.csdn.net/l_bestcoder/article/details/52087126

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define X first
#define Y second
using namespace std;
typedef pair<int,int> pii;
const int maxn=;
struct node
{
int v,w,next;
} E[maxn*];
pii P[];
int cnt,head[maxn],Lca[maxn],n,m,dis[maxn],f[maxn];
bool vis[maxn];
void init()
{
cnt=;
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
}
void addedge(int v,int u,int w)
{
E[cnt].v=v,E[cnt].w=w,E[cnt].next=head[u];
head[u]=cnt++;
E[cnt].v=u,E[cnt].w=w,E[cnt].next=head[v];
head[v]=cnt++;
}
int find(int x)
{
return x==f[x]?x:f[x]=find(f[x]);
}
void tarjan(int root)
{
vis[root]=;
f[root]=root;//初始化
for (int i=; i<=m; i++)
{
if (P[i].X==root&&vis[P[i].Y])
Lca[i]=find(P[i].Y);
if (P[i].Y==root&&vis[P[i].X])
Lca[i]=find(P[i].X);
}
for (int i=head[root]; i!=-; i=E[i].next)
{
if (!vis[E[i].v])
{
dis[E[i].v]=dis[root]+E[i].w;
tarjan(E[i].v);
f[E[i].v]=root;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
init();
int a,b,c;
scanf("%d%d",&n,&m);
for (int i=;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
}
for (int i=;i<=m;i++)
scanf("%d%d",&P[i].X,&P[i].Y);
dis[]=;
tarjan();
for (int i=;i<=m;i++)
printf("%d\n",dis[P[i].X]+dis[P[i].Y]-*dis[Lca[i]]);
}
return ;
}

HDU - 2586 How far away ?(LCA模板题)的更多相关文章

  1. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. HDU 2586——How far away ?——————【LCA模板题】

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  4. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  5. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  6. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

  7. HDU 1874 畅通工程续(模板题——Floyd算法)

    题目: 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让行人很困扰 ...

  8. HDU 1301-Jungle Roads【Kruscal】模板题

    题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下 ...

  9. 敌兵布阵 HDU - 1166 (树状数组模板题,线段树模板题)

    思路:就是树状数组的模板题,利用的就是单点更新和区间求和是树状数组的强项时间复杂度为m*log(n) 没想到自己以前把这道题当线段树的单点更新刷了. 树状数组: #include<iostrea ...

随机推荐

  1. C++类与static

    到目前为止,我们设计的类中所有的成员变量和成员函数都是属于对象的,如我们在前面定义的book类,利用book类声明两个对象Alice和Harry,这两个对象均拥有各自的price和title成员变量, ...

  2. java上传并下载以及解压zip文件有时会报文件被损坏错误分析以及解决

    情景描述: 1.将本地数据备份成zip文件: 2.将备份的zip文件通过sftp上传到文件服务器: 3.将文件服务器上的zip文件下载到运行服务器: 4.将下载的zip文件解压到本地(文件大小超过50 ...

  3. Knockout js 绑定 radio 时,当绑定整形的时候,绑定不生效

    解决方案: 使用checkedValue和checked 组合,如下代码. <div><input type="radio" name="flavorG ...

  4. Android 使用URLConnection来post数据

    /** * @param postUrl * 需要post的地址 * @param map * 存储数据的map * @param handler * 处理message的handler */ pub ...

  5. Location-aware Associated Data Placement for Geo-distributed Data-intensive Applications--INFOCOM 2015

    [标题] [作者] [来源] [对本文评价] [why] 存在的问题 [how] [不足] assumption future work [相关方法或论文] [重点提示] [其它]

  6. MUI判断网络连接以及监听网络变化JS

    来源:netChange问题:怎么判断网络状态 MUI用于获取当前设备的网络类型 function plusReady(){ var types = {}; types[plus.networkinf ...

  7. JavaScript动态加载资源【js|css】示例代码

    在开发过程中会用到各种第三方的插件,或者自己写在单独文件中的js方法库或者css样式,在html头部总是需要写一大堆的script和link标签,如果想要自己实现动态的引入资源文件,可以使用开源的re ...

  8. openstack私有云布署实践【13.1 网络Neutron-compute节点配置(科兴环境)】

    所有kxcompute节点 下载安装组件   # yum install openstack-neutron openstack-neutron-linuxbridge ebtables ipset ...

  9. 湖南多校对抗赛(2015.05.03)Problem A: Twenty-four point

    给四个数 问能不能算出24点...我的方法比较烂...920ms 差点TLE.应该有更好的方法. #include<stdio.h> #include<string.h> #i ...

  10. 第九十一节,html5+css3pc端固定布局,完成首页

    html5+css3pc端固定布局,完成首页 此时我们的首页就完成了 首页效果 其他页面我就不做了,原理相同,做其他页面时将头尾css分离调用即可 大纲算法 我们看看大纲算法比较清晰,说明符合规则 h ...