ZOJ 题目3195 Design the city(LCA+RMQ)

来源:互联网 发布:游族网络 游戏策划工资 编辑:程序博客网 时间:2024/05/07 21:34
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

40 1 10 2 10 3 121 2 30 1 250 1 10 2 11 3 11 4 120 1 21 0 3

Sample Output

3222

Author: HE, Zhuobin

Source: ZOJ Monthly, May 2009

题目大意:一棵树,求三个点之间最短路,

#include<stdio.h>   #include<string.h>   #include<math.h>   #include<algorithm>   #define min(a,b) (a>b?b:a)   using namespace std;  #define N 50080   int fa[N],vis[N],val[N],p[N];  int first[N*2],node[N*2],deep[N*2],minv[N<<1][25],dis[N];  int n,q,cnt;  int head[N];  struct s  {      int u,v,w,next;  }edge[N<<1];  void add(int u,int v,int w)  {      edge[cnt].u=u;      edge[cnt].v=v;  edge[cnt].w=w;    edge[cnt].next=head[u];      head[u]=cnt++;  }  int tot;  void dfs(int u,int pre,int dep)  {      tot++;      fa[u]=pre;      node[tot]=u;      deep[tot]=dep;      vis[u]=1;      first[u]=tot;      int i;      for(i=head[u];i!=-1;i=edge[i].next)      {          int v=edge[i].v;          if(!vis[v])          {  dis[v]=dis[u]+edge[i].w;            dfs(v,u,dep+1);              tot++;              node[tot]=u;              deep[tot]=dep;          }      }  }  void init(int n)    {        int i,j,k;        for(i=1;i<=n;i++)        {            minv[i][0]=i;        }        for(j=1;(1<<j)<=n;j++)        {            for(k=1;k+(1<<j)-1<=n;k++)            {                if(deep[minv[k][j-1]]>deep[minv[k+(1<<(j-1))][j-1]])                  minv[k][j]=minv[k+(1<<(j-1))][j-1];              else                  minv[k][j]=minv[k][j-1];          }        }    }    int q_min(int l,int r)    {        int k=(int)(log((double)(r-l+1))/(log(2.0)));        if(deep[minv[l][k]]>deep[minv[r-(1<<k)+1][k]])          return minv[r-(1<<k)+1][k];      else          return minv[l][k];  }   int lca(int a,int b)  {      int x=first[a];      int y=first[b];      int k;      if(x>y)      {          int t=x;          x=y;          y=t;      }          k=q_min(x,y);          return node[k];  }  int main(){int n;int flag=0;while(scanf("%d",&n)!=EOF){int i;if(flag)printf("\n");flag=1;memset(head,-1,sizeof(head));memset(vis,0,sizeof(vis));cnt=0;for(i=1;i<n;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);a++;b++;add(a,b,c);add(b,a,c);}tot=0;dis[1]=0;dfs(1,-1,0);int q;init(n*2);scanf("%d",&q);while(q--){int a,b,c;scanf("%d%d%d",&a,&b,&c);a++;b++;c++;int aa=lca(a,b);int bb=lca(b,c);int cc=lca(a,c);printf("%d\n",dis[a]+dis[b]+dis[c]-(dis[aa]+dis[bb]+dis[cc]));}}}


0 0
原创粉丝点击