How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 27977    Accepted Submission(s): 11218

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
 
Source
 
Recommend
lcy
看样子是不能用floyd

对于无向图可以随便取一个点当作根节点

先求一遍从根节点到其它节点的距离

然后lca  看代码叭

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int n, m, s;
int anc[maxn][], deep[maxn], vis[maxn];
LL d[maxn];
int head[maxn], cnt;
struct node
{
int u, v, next, c;
}Node[maxn << ]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, c);
} int dfs(int u, int fa)
{
for(int i = ; i < ; i++)
anc[u][i] = anc[anc[u][i - ]][i - ];
for(int i = head[u]; i != -; i = Node[i].next)
{
int v = Node[i].v;
if(v == fa || deep[v]) continue;
anc[v][] = u;
deep[v] = deep[u] + ;
dfs(v, u);
}
} int lca(int u, int v)
{
if(deep[u] < deep[v]) swap(u, v);
for(int i = - ; i >= ; i--)
if(deep[anc[u][i]] >= deep[v])
u = anc[u][i]; for(int i = - ; i >= ; i--)
{
if(anc[u][i] != anc[v][i])
{
u = anc[u][i];
v = anc[v][i];
}
}
if(u == v) return u;
return anc[u][];
} void spfa()
{
for(int i = ; i < maxn; i++) d[i] = INF;
// cout<< d[2] << endl;
deque<int> Q;
Q.push_back(s);
d[s] = ;
vis[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop_front();
vis[u] = ;
for(int i = head[u]; i != -; i = Node[i].next)
{
int v = Node[i].v;
// cout << v << endl;
if(d[v] > d[u] + Node[i].c)
{
//cout << 2222 << endl;
d[v] = d[u] + Node[i].c;
if(!vis[v])
{
if(Q.empty()) Q.push_front(v);
else if(d[Q.front()] > d[v]) Q.push_front(v);
else Q.push_back(v);
vis[v] = ;
}
}
}
}
} void init()
{
mem(head, -);
cnt = ;
mem(vis, );
mem(anc, );
mem(deep, );
} int main()
{ int T;
rd(T);
while(T--)
{
init();
rd(n), rd(m);
s = ;
for(int i = ; i < n; i++)
{
int u, v, w;
rd(u), rd(v), rd(w);
add(u, v, w);
}
spfa();
deep[s] = ;
dfs(s, -);
for(int i = ; i < m; i++)
{
int u, v;
rd(u), rd(v);
int x = lca(u, v);
printf("%d\n", d[u] + d[v] - * d[x]); } } return ;
}

How far away ? HDU - 2586的更多相关文章

  1. How far away ? HDU - 2586 【LCA】【RMQ】【java】

    题目大意:求树上任意两点距离. 思路: dis[i]表示i到根的距离(手动选根),则u.v的距离=dis[u]+dis[v]-2*dis[lca(u,v)]. lca:u~v的dfs序列区间里,深度最 ...

  2. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  3. hdu 2586 How far away ?倍增LCA

    hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增 ...

  4. LCA(最近公共祖先)--tarjan离线算法 hdu 2586

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

  5. HDU 2586 How far away ?【LCA】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...

  6. HDU 2586 How far away ? (LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 LCA模版题. RMQ+LCA: #include <iostream> #incl ...

  7. hdu - 2586 How far away ?(最短路共同祖先问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...

  8. HDU 2586 How far away ?(LCA在线算法实现)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:给出一棵树,求出树上任意两点之间的距离. 思路: 这道题可以利用LCA来做,记录好每个点距离根结点的 ...

  9. HDU 2586.How far away ?-离线LCA(Tarjan)

    2586.How far away ? 这个题以前写过在线LCA(ST)的,HDU2586.How far away ?-在线LCA(ST) 现在贴一个离线Tarjan版的 代码: //A-HDU25 ...

随机推荐

  1. Sql学习笔记(二)—— 条件查询

    上篇简单介绍了一下sql的一些基础增删改查语句,而针对多种多样的查询语句则未详细说明,这一篇继续记录一下关于各种条件查询的知识. 1.按列名进行查询 语句: select stuName , stuA ...

  2. 文件的基本管理和XFS文件系统备份恢复

    4.1 Linux系统目录结构和相对/绝对路径 4.1.1系统目录结构 在WIN系统中,查看文件先进入相应的盘符,然后进入文件目录 在WIN中,它是多根  c:\    d:\   e:\ Linux ...

  3. 前后端分离密码登陆加密RSA方案(java后端)

    前言:密码加密有很多种方案,这里不做过多讨论,本篇文章是基于RSA加密实现. 首先在前端工程中需要引入加密js: "jsencrypt": "2.3.1",(注 ...

  4. 解决gitbook报错问题

    这个问题困扰了我 很久,网友给出了很多解决方案,我都亲测不靠谱. 以下解决方法亲测靠谱: OS:Win7 Gitbook版本: 3.2.3 Nodejs: V8.9.1 步骤: 1. 编辑文件 C:\ ...

  5. 20190421-那些年使用过的CSS预处理器(CSS Preprocessor)之Sass and Less

    写在前面乱七八糟的前言: emmm,还是决定把Sass与Less单独出来写成一篇,可能会稍微好辣么一丢丢?TAT语法特性是真的香,通篇下来能吸收个10%自我感觉已经很nice了,毕竟渣渣的我有渣渣的自 ...

  6. Nginx Windows详细安装部署教程

    一.Nginx简介 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Ramble ...

  7. Util6 MIS2.0 (优六信息化管理系统框架)

    Util6 MIS一套基于ASP.NET MVC5 + Layui 开发的通用信息化管理系统快速开发框架 系统操作界面简洁, 项目结构清晰,功能模块化设计,支撑框架轻量高效,代码层级分离,注释完整,可 ...

  8. Eclipse导出包含第三方Jar的工程

    基于第三方开源的Jar包封装了一个工具类,在导出成Jar包后,引用新生成的Jar包,却报找不到类的错误.看了一下生成的Jar包,发现根本没有包含第三方Jar包的相关class.这是导出第三方Jar包时 ...

  9. SQL 修改字段类型和长度,常见类型介绍及数据库设计工具PowerDesigner和astah

    1.电话字段设置24个Byte竟然不够,好吧设置为50的长度. alter table <表名> alter column <字段名> 新类型名(长度) 举例: ) 2.删除一 ...

  10. windows10滑轮bug

    今天我突然发现我一点也忍受不了在UWP应用.wi10窗口.设置等界面无法使用鼠标滑轮了.这个bug已经困扰了我差不多一年了,从买了这台笔记本就开始了.而且这个问题在中间的某一次升级系统后,也修复了,但 ...