HDU2586 How far away ?

本文提供了两种LCA算法的实现模板,一种适用于离线查询,另一种适用于在线查询。这两种模板均可用于解决特定类型的路径查找问题,如求解两点间的最短距离。

How far away ?


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


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
ECJTU 2009 Spring Contest


LCA模板题。。。直接上模板。。

LCA离线模板

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=40010;
const int MAXE=410;
struct EDGE
{
    int v,next;
    int val;
}edge[MAXN<<1];
struct QUET
{
    int v,next;
    int num;
}q[MAXE];
int fa[MAXN];
int find(int x)
{
    return fa[x]==x?x:fa[x]=find(fa[x]);
}
int head1[MAXN],head2[MAXN],n,m,lca[MAXE],dist[MAXN];
int sz;
bool vis[MAXN];
void init()
{
    memset(head1,-1,sizeof(head1));
    memset(head2,-1,sizeof(head2));
    memset(vis,0,sizeof(vis));
    memset(dist,0,sizeof(dist));
    sz=0;
}
void add_edge(int u,int v,int val)
{
    edge[sz].v=v;
    edge[sz].val=val;
    edge[sz].next=head1[u];
    head1[u]=sz++;
}
void add_q(int u,int v,int i)
{
    q[sz].v=v;
    q[sz].num=i;
    q[sz].next=head2[u];
    head2[u]=sz++;
}
void tarjan(int u)
{
    fa[u]=u;
    vis[u]=1;
    for(int i=head2[u];i!=-1;i=q[i].next)
    {
        int v=q[i].v;
        if(vis[v])
        {
            lca[q[i].num]=find(v);
        }
    }
    for(int i=head1[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v])
        {
            dist[v]=dist[u]+edge[i].val;
            tarjan(v);
            fa[v]=u;
        }
    }
}
int anss[MAXE],anst[MAXE];
int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int u,v,c;
        init();
        for(i=1;i<=n-1;i++)
        {
            scanf("%d%d%d",&u,&v,&c);
            add_edge(u,v,c);
            add_edge(v,u,c);
        }
        sz=0;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            add_q(u,v,i);
            add_q(v,u,i);
            anss[i]=u;
            anst[i]=v;
        }
        tarjan(1);
        for(i=1;i<=m;i++)
        {
            printf("%d\n",dist[anss[i]]+dist[anst[i]]-2*dist[lca[i]]);
        }
    }
}

LCA在线模板

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=40010;
struct EDGE
{
    int v,next;
    int dis;
}edge[MAXN<<1];
int dep[MAXN<<1],rank[MAXN],num,set[MAXN<<1],dist[MAXN];
bool vis[MAXN];
int head[MAXN],size;
int Min(int x,int y)
{
    return dep[x]<dep[y]?x:y;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    size=0;
}
void add_edge(int u,int v,int dis)
{
    edge[size].v=v;
    edge[size].dis=dis;
    edge[size].next=head[u];
    head[u]=size++;
}
void dfs(int u,int deep)
{
    vis[u]=1;
    rank[u]=num;
    set[num]=u;
    dep[num++]=deep;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(vis[v])
          continue;
        dist[v]=dist[u]+edge[i].dis;
        dfs(v,deep+1);
        set[num]=u;
        dep[num++]=deep;
    }
}
int dp[MAXN<<1][21];
void RMQ_init()
{
    int i,j;
    for(i=0;i<num;i++)
      dp[i][0]=i;
    for(j=1;(1<<j)<num;j++)
    {
        for(i=0;i+(1<<j)<num;i++)
        {
            dp[i][j]=Min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
    }
}
int RMQ(int l,int r)
{
    if(r<l)
      swap(l,r);
    int k=(int)log(r-l+1.0)/log(2.0);
    return Min(dp[l][k],dp[r-(1<<k)+1][k]);
}
int LCA(int l,int r)
{
    if(l>r)
      swap(l,r);
    return set[RMQ(l,r)];
}
int main()
{
    int t,n,m,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init();
        int u,v,d;
        for(i=1;i<n;i++)
        {
            scanf("%d%d%d",&u,&v,&d);
            add_edge(u,v,d);
            add_edge(v,u,d);
        }
        num=0;
        dist[1]=0;
        dfs(1,1);
        RMQ_init();
        while(m--)
        {
            scanf("%d%d",&u,&v);
            printf("%d\n",dist[u]+dist[v]-2*dist[LCA(rank[u],rank[v])]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值