zoj——3195 Design the city

Design the city

Time Limit: 1 Second      Memory Limit: 32768 KB

Cerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.

In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.

Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.

Input

The input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.

Process to the end of file.

Output

Q lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.

Output a blank line between each test cases.

Sample Input

4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3

Sample Output

3
2

2
2

Author: HE, Zhuobin
Source: ZOJ Monthly, May 2009

 

题目大意:给一个无根树,有q个询问,每个询问3个点,问将这3个点连起来,距离最短是多少

思路:询问3各点之间的最短距离与两个点之间的最短距离相同,我们只需要处理出lca(x,y),lca(y,z),  lca(x,z) 然后再求这些点两两之间的最短距离,然后加起来除以2就好了。

鬼畜的输出、、、、

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 51000
using namespace std;
int n,m,x,y,z,ans,sum,tot,ans1,ans2,ans3;
int fa[N],top[N],size[N],deep[N],head[N],dis[N];
struct Edge
{
    int to,dis,from,next;
}edge[N<<1];
int add(int x,int y,int z)
{
    tot++;
    edge[tot].to=y;
    edge[tot].dis=z;
    edge[tot].next=head[x];
    head[x]=tot;
}
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
int lca(int x,int y)
{
    for(;top[x]!=top[y];x=fa[top[x]])
      if(deep[top[x]]<deep[top[y]])
        swap(x,y);
    if(deep[x]>deep[y]) swap(x,y);
    return x;
}
int dfs(int x)
{
    size[x]=1;
    deep[x]=deep[fa[x]]+1;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to)
        {
            dis[to]=dis[x]+edge[i].dis;
            fa[to]=x,dfs(to);
            size[x]+=size[to];
        }
    }
}
int dfs1(int x)
{
    int t=0;
    if(!top[x]) top[x]=x;
    for(int i=head[x];i;i=edge[i].next)
    {
        int to=edge[i].to;
        if(fa[x]!=to&&size[t]<size[to]) t=to;
     } 
     if(t) top[t]=top[x],dfs1(t);
     for(int i=head[x];i;i=edge[i].next)
     {
         int to=edge[i].to;
         if(fa[x]!=to&&to!=t) dfs1(to);
     }
}
int begin()
{
    ans=0;tot=0;
    memset(fa,0,sizeof(fa));
    memset(top,0,sizeof(top));
    memset(dis,0,sizeof(dis));
    memset(edge,0,sizeof(edge));
    memset(deep,0,sizeof(deep));
    memset(size,0,sizeof(size));
    memset(head,0,sizeof(head));
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(sum++) printf("\n");
        begin();
        for(int i=1;i<n;i++)
        {
            x=read(),y=read(),z=read();
            add(x,y,z),add(y,x,z);
        }
        dfs(0),dfs1(0);
        m=read();
        for(int i=1;i<=m;i++)
        {
            x=read(),y=read(),z=read();
            ans1=dis[x]+dis[y]-2*dis[lca(x,y)];
            ans2=dis[x]+dis[z]-2*dis[lca(x,z)];
            ans3=dis[y]+dis[z]-2*dis[lca(y,z)];
            ans=(ans1+ans2+ans3)/2;
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/z360/p/7412645.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值