Connections between cities lca 求公共最近祖先

Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3795    Accepted Submission(s): 1101


Problem Description
After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, some materials needed can only be produced in certain places. So we need to transport these materials from city to city. For most of roads had been totally destroyed during the war, there might be no path between two cities, no circle exists as well.
Now, your task comes. After giving you the condition of the roads, we want to know if there exists a path between any two cities. If the answer is yes, output the shortest path between them.
 

Input
Input consists of multiple problem instances.For each instance, first line contains three integers n, m and c, 2<=n<=10000, 0<=m<10000, 1<=c<=1000000. n represents the number of cities numbered from 1 to n. Following m lines, each line has three integers i, j and k, represent a road between city i and city j, with length k. Last c lines, two integers i, j each line, indicates a query of city i and city j.
 

Output
For each problem instance, one line for each query. If no path between two cities, output “Not connected”, otherwise output the length of the shortest path between them.
 

Sample Input
  
  
5 3 2 1 3 2 2 4 3 5 2 3 1 4 4 5
 

Sample Output
  
  
Not connected 6
Hint
Hint Huge input, scanf recommended.
给一个森林,里面有好多树,给出点,判断点是否在同一个树下,如果是,求出两个点的最短距离,也就是最近公共祖先,注意起建的树,用离线Tarjan算法来求
,说实话,这个题目,到了比赛,一定不会做
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 10012//node
#define M 2222252//query 用来放询问的数据数组
int n,m,c,h1[N],h2[N],dis[N],vis[N],fa[N],ans[M],id[N],ne,cnt;
struct node
{
    int v,d,next;
}ua[M],ub[M];

void addA(int u,int v,int d)
{
    ua[ne].v=v;
    ua[ne].d=d;
    ua[ne].next=h1[u];
    h1[u]=ne++;
}

void addB(int u,int v,int d)
{
    ub[ne].v=v;
    ub[ne].d=d;
    ub[ne].next=h2[u];
    h2[u]=ne++;
}
int find(int i)
{
    return (fa[i]==i?i:fa[i]=find(fa[i]));
}
void Tarjan(int u)
{
    id[u]=cnt;
    vis[u]=1;
    fa[u]=u;
    int i,j;
    for(i=h2[u];i!=-1;i=ub[i].next)
    {
        int v=ub[i].v;
        if(vis[v])
        {
            if(id[u]==id[v])
            {
                int rt=find(v);
                ans[ub[i].d]=dis[u]+dis[v]-2*dis[rt];
            }else ans[ub[i].d]=-1;
        }
    }
    for(i=h1[u];i!=-1;i=ua[i].next)
    {
        int v=ua[i].v;
        if(!vis[v])
        {
              dis[v] = dis[u] + ua[i].d;
              Tarjan(v);
              fa[v] = u;
        }

    }
}




int main()
{

    int i,j,k;
    int u,v,d;
    while(scanf("%d%d%d",&n,&m,&c)!=EOF)
    {
           for(i=1;i<=n;i++)
           {
               fa[i]=0;
               ans[i]=id[i]=vis[i]=0;
               h1[i]=h2[i]=-1;
           }
           ne=0;
           for(i=1;i<=m;i++)
           {
               scanf("%d%d%d",&u,&v,&d);
               addA(u,v,d);
               addA(v,u,d);
           }
           ne=0;
           for(i=1;i<=c;i++)
           {
               scanf("%d%d",&u,&v);
               addB(u,v,i);
               addB(v,u,i);

           }

           //printf("fhkdsjhfjkd");
           for(cnt=i=1;i<=n;i++,cnt++)
           {

               if(!vis[i])
               {   //printf("fhkdsjhfjkd");
                   dis[i]=0;
                   Tarjan(i);
               }
           }

           for(i=1;i<=c;i++)
           {
               if(ans[i]>=0)
               {

                   printf("%d\n",ans[i]);
               }
               else
               printf("Not connected\n");
           }
    }
   return 0;
}





 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值