Connections between cities

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9785    Accepted Submission(s): 2357


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

【题意】给了一个深林,求任意两点的距离,如果不连通,输出不连通的信息。

【解题方法】深林转化成树之后就是LCA模板了。方法很多。时间很松的,谁便哪种。

【AC 代码】

//
//Created By just_sort 2016/9/2
//All rights reserved
//HDU.2874 Euler Sequence & RMQ(ST)
//

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e4+10;
const int maxm = 2e4+10;
struct edge{
    int v,w,next;
}E[maxn<<2];
int head[maxn],tot;
void addedge(int u,int v,int w){
    E[tot].v=v,E[tot].w=w,E[tot].next=head[u],head[u]=tot++;
}
void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}
/****************************get lca********************************/
int n,m,q,vis[maxm],dep[maxm],first[maxn],dp[maxn],id[maxn],dfsnum,cc;
int getmin(int x,int y){
    return dep[x]<dep[y]?x:y;
}
struct SparseTable{
    int dp[20][maxn<<1];
    void init(int n){
        for(int i=1; i<=n; i++) dp[0][i]=i;
        for(int i=1; (1<<i)<=n; i++)
            for(int j=1; j+(1<<i)-1<=n; j++)
                dp[i][j]=getmin(dp[i-1][j],dp[i-1][j+(1<<(i-1))]);
    }
    int RMQ(int l,int r){
        int k=31-__builtin_clz(r-l+1);
        return getmin(dp[k][l],dp[k][r-(1<<k)+1]);
    }
}st;
void dfs(int u,int f,int d)
{
    vis[++dfsnum]=u;
    dep[dfsnum]=d;
    first[u]=dfsnum;
    id[u]=cc;
    for(int i=head[u]; ~i; i=E[i].next){
        int v=E[i].v;
        if(v==f) continue;
        dp[v]=dp[u]+E[i].w;
        dfs(v,u,d+1);
        vis[++dfsnum]=u;
        dep[dfsnum]=d;
    }
}
void build(){
    dfsnum=cc=0;
    memset(id,0,sizeof(id));
    for(int i=1; i<=n; i++){
        ++cc;
        if(!id[i]) dp[i]=0,dfs(i,-1,0);
    }
    st.init(2*n-1);
}
int lca(int u,int v)
{
    if(first[u]>first[v]) swap(u,v);
    int id=st.RMQ(first[u],first[v]);
    return vis[id];
}
/*****************************分割线**********************************/

int main()
{
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        init();
        int u,v,w;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        build();
        while(q--)
        {
            scanf("%d%d",&u,&v);
            if(id[u]!=id[v]) puts("Not connected");
            else{
                printf("%d\n",dp[u]+dp[v]-2*dp[lca(u,v)]);
            }
        }
    }
    return 0;
}