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. Conversion to Dalvik format failed with error 1(android)

    1.如果不修改android sdk版本,则使用project clean 命令作用于某工程即可. (该处理方式只是在高版本中兼容了低版本工程,未真正意义上的升级) 2.如果修改android sdk ...

  2. C#第十三天

    1.单例模式 1)将构造函数私有化 2)提供一个静态方法,返回一个对象 3)创建一个单例 namespace 单例模式 { public partial class Form1 : Form { pu ...

  3. C#代码篇:代码产生一个csv文件调用有两个核心的坑

    忙活了半天终于可以开工了,a物品到底要不要放进去取决于两个因素,第一是a有4kg重,只有背包大于等于4kg的时候才能装进去(也就是说当i=1,k<4时f[i,k]=0):第二是当背包的重量大于等 ...

  4. C++编程技巧(长期更新)

    1.数组使用 int* p = new int[5](); // 数组新建并全部初始化为0 等价于: int* p; p = new int[5](); int* q = new int[5];    ...

  5. LDAP协议

    很多人虽然会使用dsadd等命令添加用户,但是dsadd的命令说明里面并没有涉及到dc,cn,ou的含义,很多人都不明白,这里是微软的技术支持人 员的回信,希望对大家有帮助. CN, OU, DC 都 ...

  6. ZOJ 1655 FZU 1125 Transport Goods

    迪杰斯特拉最短路径. 1.every city must wait till all the goods arrive, and then transport the arriving goods t ...

  7. [妙味JS基础]第五课:函数传参、重用、价格计算

    知识点总结 函数传参,传的参数=数据类型(即:数值.字符串.布尔.函数.对象.未定义) 通过传参来重用代码 1.尽量保证 HTML 代码结构一致,可以通过父级选取子元素 2.把核心主程序实现,用函数包 ...

  8. iOS开发打电话的功能

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 NSMutableString * phoneStr=[[NSMutableString alloc] init ...

  9. UITableView简单使用

    在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不 ...

  10. 《JS权威指南学习总结--6.5枚举属性》

    内容要点: 一.for/in循环 1.for/in循环可以在循环体中遍历对象中所有可枚举的属性(包括自有属性和继承的属性),把属性名称赋值给循环变量.对象继承的内置方法不可枚举,但在代码中给对象添加的 ...