How far away ? - HDU 2586 LCA 或水题

本文介绍了一种使用LCA算法解决特定图论问题的方法,即如何在给定的村庄中快速计算任意两个房子之间的距离。通过构建图的数据结构,并运用深度优先搜索和跳点技巧来预处理数据,使得每次查询都能高效地找到最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How far away ?

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


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
 

思路:用LCA或者因为这道题查询数量比较小,可以暴力。另外后来数据改的会爆栈,所以需要加头文件。

AC代码如下:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{ int v,dis;
};
vector<node> G[41010];
int parent[35][41010],depth[41010],vis[41010],dis[41010],n,m,T,t;
void dfs(int v,int p,int d)
{ parent[0][v]=p;
  depth[v]=d;
  vis[v]=t;
  int i,len=G[v].size();
  for(i=0;i<len;i++)
   if(vis[G[v][i].v]!=t)
   { dis[G[v][i].v]=dis[v]+G[v][i].dis;
     dfs(G[v][i].v,v,d+1);
   }
}
void init(int V)
{ int v,k;
  dfs(1,-1,0);
  for(k=0;k<30;k++)
   for(v=1;v<=V;v++)
    if(parent[k][v]<0)
     parent[k+1][v]=-1;
    else
     parent[k+1][v]=parent[k][parent[k][v]];
}
int lca(int u,int v)
{ int k;
  if(depth[u]>depth[v])
   swap(u,v);
  for(k=0;k<30;k++)
   if((depth[v]-depth[u])>>k &1)
    v=parent[k][v];
  if(u==v)
   return u;
  for(k=29;k>=0;k--)
   if(parent[k][u]!=parent[k][v])
   { u=parent[k][u];
     v=parent[k][v];
   }
  return parent[0][u];
}
int main()
{ int u,v,i,j,k,d;
  scanf("%d",&T);
  node A;
  for(t=1;t<=T;t++)
  { scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
     G[i].clear();
    for(i=1;i<n;i++)
    { scanf("%d%d%d",&u,&v,&d);
      A.dis=d;
      A.v=v;
      G[u].push_back(A);
      A.v=u;
      G[v].push_back(A);
    }
    init(n);
    while(m--)
    { scanf("%d%d",&u,&v);
      d=lca(u,v);
      printf("%d\n",dis[u]+dis[v]-2*dis[d]);
    }
    //printf("\n");
  }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值