ZOJ Design the city LCA转RMQ

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

题意: 给你一个无向图,然后M个询问,每次询问给你三个点,问你链接这三个点的最短距离

题解:ST算法,分别求出aa=LCA(a,b),bb=LCA(a,c),cc=LCA(c,b);最短距离就是dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc]);

//1085422276
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <math.h>
#include <vector>
#include <string.h>
using namespace std;
#define maxn 55000
typedef long long ll;
const int inf = (int)1E9+10;
inline ll read()
{
    ll 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 first[maxn],ver[maxn*2];
int R[maxn*2],head[maxn];
int dist[maxn],t,vis[maxn],tot;
struct ss
{
    int from,to,value,next;
} e[maxn*2];
int dp[maxn*2][30];
void init()
{
    t=1;
    memset(head,0,sizeof(head));
}
void add(int u,int v,int w)
{
    ss kk= {u,v,w,head[u]};
    e[t]=kk;
    head[u]=t++;
}
void dfs(int u,int dep)
{
    vis[u]=1;
    ver[++tot]=u;
    first[u]=tot;
    R[tot]=dep;
    for(int i=head[u]; i; i=e[i].next)
    {
        if(!vis[e[i].to])
        {
            int vv=e[i].to;
            dist[vv]=dist[u]+e[i].value;
            dfs(vv,dep+1);
            ver[++tot]=u;
            R[tot]=dep;
        }
    }
}
void find_depth()
{
    tot=0;
    memset(vis,0,sizeof(vis));
    memset(first,0,sizeof(first));///首次出现位置
    memset(ver,0,sizeof(ver));///节点编号
    dfs(1,1);
}
void ST(int n)
{
    for(int i=1; i<=n; i++)dp[i][0]=i;
    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; i+(1<<j)-1<=n; i++)
        {
            int a=dp[i][j-1];
            int b=dp[i+(1<<(j-1))][j-1];
            dp[i][j]=R[a]>R[b]?b:a;
        }
    }
}
int RMQ(int x,int y)
{
    int k=0;
    while((1<<(k+1))<=y-x+1)
    {
        k++;
    }
    int a=dp[x][k];
    int b=dp[y-(1<<k)+1][k];
    return R[a]>R[b]?b:a;
}
int LCA(int x,int y)
{
    x=first[x];
    y=first[y];
    if(x>y)swap(x,y);
    return ver[RMQ(x,y)];
}
int main()
{
    int n;
    int a,b,c;
    int T=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(T)cout<<endl;
        init();
        for(int i=1; i<n; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a+1,b+1,c);
            add(b+1,a+1,c);
        }
        dist[1]=0;
        find_depth();
        ST(2*n-1);
        int q=read();
        for(int i=1; i<=q; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            a++;
            b++;
            c++;
            int aa=LCA(a,b);
            int bb=LCA(a,c);
            int cc=LCA(b,c);
            cout<<dist[a]+dist[b]+dist[c]-(dist[aa]+dist[bb]+dist[cc])<<endl;
        }
        T++;
    }
    return 0;
}

代码

时间: 2024-08-26 10:25:17

ZOJ Design the city LCA转RMQ的相关文章

zoj 3195 Design the city lca倍增

题目链接 给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离. 其实就是两两之间的最短距离加起来除2. 倍增的lca模板 #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <s

ZOJ 3195 Design the city LCA

题目:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3195 题意:给定一个树,求三点之间的距离 思路:假定三点为u, v, w,那么(dist[u,v] + dist[v,w] + dist[u,w]) / 2就是答案 总结:wa时不要灰心,也许你离ac不远了 #include<iostream> #include<cstdio> #include<cstring> #include<alg

[zoj3195]Design the city(LCA)

解题关键:求树上三点间的最短距离. 解题关键:$ans = (dis(a,b) + dis(a,c) + dis(b,c))/2$ 1 //#pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cstdlib> 6 #include<

ZOJ Problem Set - 3195 Design the city 【Tarjan离线LCA】

题目:ZOJ Problem Set - 3195 Design the city 题意:给出一个图,求三点的连起来的距离. 分析:分别求出三点中任意两点的距离 / 2  = ans AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; #define N 50010 #define M 20010 struc

ZOJ 3195 Design the city

倍增法在线LCA..... ZOJ Problem Set - 3195 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,

ZOJ3195 Design the city [2017年6月计划 树上问题04]

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

ZOJ_3195_Design the city(LCA+tarjan)

Design the city Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description 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 eve

xtu summer individual 1 C - Design the city

C - Design the city Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description 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.

lca转RMQ

这个博客写得好 1 #include <stdio.h> 2 #include <vector> 3 #include <string.h> 4 using namespace std; 5 const int N = 100000; 6 7 /* 8 lca 转RMQ 9 10 询问u和v的lca 11 我们保存树的遍历序列,那么只要在序列中找到第一次出现u和第一次出现v的位置 12 然后RMQ该区间深度最小的那个点就是u和v的lca了 13 14 那么要保存每个点首