hdu 2586 How far away ?

来源:互联网 发布:ubuntu应用商店慢 编辑:程序博客网 时间:2024/05/22 06:38


How far away ?

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


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
23 21 2 103 1 151 22 32 21 2 1001 22 1
 

Sample Output
1025100100
 

Source
ECJTU 2009 Spring Contest
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3486 2874 2888 3234 2818 


题意:给你一棵树,每条边都有权值,m次询问,每次问任意两点之间的距离。

思路:我是特意来补这道题的。。因为感觉这个算法挺实用的,好像很多时候都要用到。这是倍增LCA的裸题。思维类似RMQ,通过预处理出每个点的第2^n次方个父节点,从而达到logn查询任意两个节点的最近公共祖先。具体写法看代码吧。。。自己手写的留着当模板用。

#include<iostream>  #include<cmath>  #include<queue>  #include<cstdio>  #include<queue>  #include<algorithm>  #include<cstring>  #include<string>  #include<utility>#include<map>#include<vector>#include<vector>#define maxn 40005#define inf 0x3f3f3f3fusing namespace std;typedef long long LL;const double eps = 1e-8;const int mod = 1e9 + 7;const int M = 20;struct node{int v, value, next;}p[maxn << 1];int len, head[maxn], dis[maxn], depth[maxn], n, m, father[maxn][25];void addedge(int u, int v, int value){p[len].v = v;p[len].value = value;p[len].next = head[u];head[u] = len++;}void dfs(int x, int fa){father[x][0] = fa;for (int i = head[x]; ~i; i = p[i].next){if (p[i].v == fa)continue;dis[p[i].v] = dis[x] + p[i].value;depth[p[i].v] = depth[x] + 1;dfs(p[i].v, x);}}void presolve(){dis[1] = depth[1] = 0;dfs(1, 0);for (int i = 1; i <= n; i++)for (int j = 1; j < M; j++)father[i][j] = father[father[i][j - 1]][j - 1];}int lca(int x, int y){if (depth[x] != depth[y]){if (depth[x] > depth[y])swap(x, y);int distant = depth[y] - depth[x];for (int i = 0; i < M; i++){if (distant&(1 << i))y = father[y][i];}}if (x == y)return x;for (int i = M; i >= 0; i--){if (father[x][i] != father[y][i]){x = father[x][i];y = father[y][i];}}return father[x][0];}int query(int x, int y){return dis[x] + dis[y] - (dis[lca(x, y)] << 1);}int main(){int t;scanf("%d", &t);while (t--){len = 0;memset(head, -1, sizeof(head));scanf("%d%d", &n, &m);for (int i = 0; i < n - 1; i++){int u, v, value;scanf("%d%d%d", &u, &v, &value);addedge(u, v, value);addedge(v, u, value);}presolve();while (m--){int x, y;scanf("%d%d", &x, &y);printf("%d\n", query(x, y));}}}


原创粉丝点击