hdu 2874 Connections between cities(树上倍增)

Connections between cities




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
 
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct Edge
{
    int v,w;
    Edge(){}
    Edge(int _v,int _w):v(_v),w(_w){}
};
const int N=1e4+5;
vector<Edge>G[N];
int f[N][32],d[N],rt[N],dis[N],n,m,q;
void dfs(int u,int fa,int dep,int length,int root)
{
    d[u]=dep;
    rt[u]=root;
    dis[u]=length;
    f[u][0]=fa;
    int len=G[u].size();
    for(int i=0;i<len;i++)
    {
        Edge e=G[u][i];
        if(e.v==fa)continue;
        dfs(e.v,u,dep+1,length+e.w,root);
    }
}
void bz()
{
    for(int j=1;j<=30;j++)
        for(int i=1;i<=n;i++)
            f[i][j]=f[f[i][j-1]][j-1];
}
int query(int u,int v)
{
    if(d[u]<d[v])swap(u,v);
    int dc=d[u]-d[v];
    for(int i=0;i<30;i++)
        if(dc&(1<<i))
            u=f[u][i];
    if(u==v)return u;
    for(int i=30;i>=0;i--)
        if(f[u][i]!=f[v][i])
            u=f[u][i],v=f[v][i];
    return f[u][0];
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        for(int i=1;i<=n;i++)G[i].clear();
        memset(rt,0,sizeof(rt));
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            G[u].push_back(Edge(v,w));
            G[v].push_back(Edge(u,w));
        }
        for(int i=1;i<=n;i++)
            if(!rt[i])dfs(i,0,0,0,i);
        bz();
        for(int i=0;i<q;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(rt[u]!=rt[v])
                puts("Not connected");
            else
            {
                int lca=query(u,v);
                printf("%d
",dis[u]+dis[v]-2*dis[lca]);
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/homura/p/5719250.html