博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hud 2586 How far away ?
阅读量:4948 次
发布时间:2019-06-11

本文共 3097 字,大约阅读时间需要 10 分钟。

题目连接

  

How far away ?

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

tarjan离线求LCA。。

#include 
using namespace std;const int N = 40010;struct Tarjan_Lca { bool vis[N]; vector
> A, Q[N]; int tot, par[N], ans[210], head[N], dist[N]; struct edge { int to, w, next; }G[N << 1]; inline void init(int n) { A.clear(); for(int i = 0; i < n + 2; i++) { par[i] = i; vis[i] = false; head[i] = -1; dist[i] = 0; Q[i].clear(); } } inline void add_edge(int u, int v, int w) { G[tot] = { v, w, head[u] }, head[u] = tot++; G[tot] = { u, w, head[v] }, head[v] = tot++; } inline void built(int n, int m) { int u, v, w; while(n-- > 1) { scanf("%d %d %d", &u, &v, &w); add_edge(u, v, w); } for(int i = 0; i < m; i++) { scanf("%d %d", &u, &v); A.push_back(pair
(u, v)); Q[u].push_back(pair
(v, i)); Q[v].push_back(pair
(u, i)); } } inline int find(int x) { while(x != par[x]) { x = par[x] = par[par[x]]; } return x; } inline void tarjan(int u, int fa) { for(int i = head[u]; ~i; i = G[i].next) { edge &e = G[i]; if(e.to == fa) continue; dist[e.to] = dist[u] + e.w; tarjan(e.to, u); vis[e.to] = true; par[e.to] = u; } for(auto &r: Q[u]) { if(vis[r.first]) ans[r.second] = find(r.first); } } inline void solve(int n, int m) { init(n); built(n, m); tarjan(1, 1); for(int i = 0; i < m; i++) { printf("%d\n", dist[A[i].first] + dist[A[i].second] - 2 * dist[ans[i]]); } }}go;int main() {#ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout);#endif int t, n, m; scanf("%d", &t); while(t--) { scanf("%d %d", &n, &m); go.solve(n, m); } return 0;}

转载于:https://www.cnblogs.com/GadyPu/p/4992920.html

你可能感兴趣的文章
2019春季第十一周作业
查看>>
洛谷P4591 [TJOI2018]碱基序列 【KMP + dp】
查看>>
iOS CoreData介绍和使用(以及一些注意事项)
查看>>
OS笔记047代理传值和block传值
查看>>
Android应用程序与SurfaceFlinger服务的连接过程分析
查看>>
coco2dx服务器简单例子
查看>>
Java回顾之多线程
查看>>
sqlite
查看>>
maven pom添加本地jar,不提交私库
查看>>
所有的包装类对象之间值的比较,全部使用equals方法比较。
查看>>
OC进阶(三)
查看>>
Android中Context详解——你所不知道的Context
查看>>
C#中DBNull.Value和Null的用法和区别
查看>>
P4782 【模板】2-SAT 问题
查看>>
etcd节点扩容至两个节点
查看>>
Opensuse系统配置记录
查看>>
【windows8开发】开发平台与开发框架
查看>>
机电行业如何进行信息化建设
查看>>
Windows Azure Platform Introduction (4) Windows Azure架构
查看>>
【转】chrome developer tool 调试技巧
查看>>