hdu--2586 倍增算法

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。

设grand[i][j]为节点i的2^j个祖先的编号,
pw[i][j]表示节点i到他的祖先2^i次方的距离  。

#include<stdio.h>  
#include<string.h>  
#include<vector>  
#include<stdlib.h>  
#include<math.h>  
#define max 40001  
#define maxl 25  

using namespace std;  
typedef struct  
{  
    int from,to,w;  
}edge; 
  
vector<edge>edges;  
vector<int> G[max];  
//保存边的数组  
int grand[max][maxl], gw[max][maxl];//为节点i的2^j个祖先的编号,  i到他上面祖先2^i次方的距离  
int depth[max]; 
int n,m,N; 

void addedge(int u, int v, int w)
{
	edge a = {u, v, w}, b = {v, u, w};
	edges.push_back(a);
	edges.push_back(b);
	G[u].push_back(edges.size()-2);
	G[v].push_back(edges.size()-1);
}

void dfs(int x)
{
	for(int i = 1;  i <= N; i++)
	{
		grand[x][i] = grand[grand[x][i-1]][i-1];
		gw[x][i] = gw[x][i-1] + gw[grand[x][i-1]][i-1];
	} 
	for(int  i = 0; i < G[x].size(); i++)
	{
		edge e = edges[G[x][i]]; // G[x][i]表示坐标点 
		if(e.to != grand[x][0])
		{
			depth[e.to] = depth[x] + 1;
			grand[e.to][0] = x;  //与e.to相连那个节点的父亲等于x 
			gw[e.to][0] = e.w;  //与x相连那个节点的距离等于这条边的距离  
            dfs(e.to); 			//深搜往下面建
		}
	}
}

void Init()
{   
    N = floor(log(n + 0.0) / log(2.0)); //最多能跳的2^i祖先, log2 n ;
    depth[1] = 0;  
    memset(grand, 0, sizeof(grand));  
    memset(gw, 0, sizeof(gw));  
    dfs(1);//以1为根节点建树  
}  

int lca(int a, int b)
{
	if(depth[a] > depth[b])
		swap(a, b);
	int ans = 0;
	for(int i = N; i >= 0; i--)
	{
		if(depth[a] < depth[b] &&  depth[a] <= depth[grand[b][i]])
		{
			ans += gw[b][i];
			b = grand[b][i];//先把深度较大的b往上跳 	
		}
	}
	for(int j = N; j >= 0; j--)//在同一高度了,他们一起向上跳,跳他们不相同节点,当全都跳完之后grand[a][0]就是lca 
    {  
        if(grand[a][j] != grand[b][j])  
        {  
			ans += gw[a][j];  
            ans += gw[b][j];  
            a = grand[a][j];  
            b = grand[b][j];  
        }  
    }  
    
    if(a != b)  
    {  
        ans += gw[a][0];
		ans += gw[b][0];  
    }  
    return ans;
}

int main()  
{ 
	int t ;  
  	scanf("%d", &t);  
  	while(t--)  
  	{   
  		scanf("%d%d", &n, &m);  
      	for(int i = 1; i < n; i++)  
      	{  
          	int x, y, w;  
          	scanf("%d%d%d", &x, &y, &w);  
          	addedge(x, y, w);  
      	}  
     	Init();  
     	for(int i = 1; i <= m; i++)  
     	{  
         	int x,y;  
         	scanf("%d%d", &x, &y);  
         	printf("%d\n", lca(x,y));  
     	}  
  	}  
  	return 0;
} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值