ZOJ 3195 Design the city

来源:互联网 发布:网络摄像机ip修改器 编辑:程序博客网 时间:2024/06/08 05:16
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


最后结果为x,y和y,z和x,z到公共祖先的距离的和 的2 倍

主要的求公共祖先

当你求出公共祖先之后就算一直枚举也不会超时

我自己做这题也做了2个小时


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<stack>
#include<map>
using namespace std;
const int N=50010;
const int inf=100000000;
typedef pair<int,int> p;
 vector<p> g[N];   //开不了这么大的数组,只能用vector了
const int K=15;
int parent[K+1][N];
int dis[K+1][N];
int dep[N];
int root,n;
void dfs(int v,int pp,int depth)
{
    parent[0][v]=pp;
    dep[v]=depth;
    for(int i=0;i<g[v].size();i++)
    {
        p p1=g[v][i];
        if(p1.first!=pp)
        {
            //if(v==1)
                //cout<<p1.first<<g[v].size()<<endl;
            dfs(p1.first,v,depth+1);
        }
    }
}
int lca(int u,int v)
{
    if(dep[u]>dep[v])
    swap(u,v);
    for(int k=0;k<K;k++)
        if((dep[v]-dep[u])>>k&1)  //经过这一步后u 和v 就在同一层了
    {
        v=parent[k][v];
    }
    if(u==v) return u;  //  如果他们还相等,那肯定是最近公共祖先了,否则就执行下面
    for(int k=K;k>=0;k--)
    {
        if(parent[k][u]!=parent[k][v]) //这里用到了二进制优化,自己画个图应该就懂了
        {
            u=parent[k][u];
            v=parent[k][v];  
        }
    }
    return parent[0][u];
}
void init()
{
    dfs(root,-1,0);
    for(int k=0;k<K;k++)
        for(int v=0;v<n;v++)
        if(parent[k][v]==-1) parent[k+1][v]=-1;
    else parent[k+1][v]=parent[k][parent[k][v]];   //parent[k][v]代表从v出发走2^k步,当K==0的时候也就是他的父节点了(比他深度小1)
}
int Len(int x,int y,int s)
{
    int len=0;
    int a=x;
    while(dep[a]>dep[s])
    {
       for(int i=0;i<g[a].size();i++)
       {
           p p1=g[a][i];
           if(p1.first==parent[0][a])
           {
               len+=p1.second;
               break;
           }
       }
       a=parent[0][a];
    }
    a=y;
    while(dep[a]>dep[s])
    {
       for(int i=0;i<g[a].size();i++)
       {
           p p1=g[a][i];
           if(p1.first==parent[0][a])
           {
               len+=p1.second;
               break;
           }
       }
       a=parent[0][a];
    }
    return len;
}
int main()
{
    int flag=1;
    while(scanf("%d",&n)!=EOF)
    {
        //cout<<"n="<<n<<endl;
        if(flag)
            flag=0;
        else
            cout<<"\n";
        root=1;
       for(int i=0;i<=n;i++)
        g[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            g[a].push_back(make_pair(b,c));
            g[b].push_back(make_pair(a,c));
        }
        init();
        int q;
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            int x,y,z;


           scanf("%d%d%d",&x,&y,&z);
           //cout<<x<<y<<z<<endl;
           //cout<<dep[1]<<dep[0]<<dep[2]<<dep[3]<<endl;  //自己调试 了很久
           int len=0;
           int a=lca(x,y);
           len+=Len(x,y,a);
           //cout<<len<<endl;
           a=lca(y,z);
           len+=Len(y,z,a);
           //cout<<len<<endl;
           a=lca(z,x);
           len+=Len(z,x,a);
           //cout<<len<<endl;
           //int len=lca(x,y)+lca(y,z)+lca(z,x);
           printf("%d\n",len/2);
        }
    }
    return 0;
}


0 0
原创粉丝点击