HDU - 2874 Connections between cities

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.

InputInput 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.OutputFor 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
  可以DFS,将各个树建立起来(可以建立虚点,使得所有的树连接到一块),然后ST预处理所有的点,在查询的时候,用两个点到根节点的距离和-2*lca(u,v)就可以算出任意两点之间的距离
#include 
using namespace std;
const int mxn = 2e4+7 ;
#define ll long long
ll n,m,t,k,ans,cnt;
int to[mxn] , head[mxn] , st[30][mxn] , dep[mxn] , len[mxn] , far[mxn];
bool vis[mxn];
struct $
{
    int to , w , nx ;     
}e[mxn];
void init()
{
    cnt = 0 ;
    memset(vis,false,sizeof(vis));
    memset(head,-1,sizeof(head));
    memset(len,0,sizeof(len));
    memset(dep,0,sizeof(dep));
    memset(st,0,sizeof(st));
    for(int i=1;i<=n;i++)
        far[i] = i ;
}
void add(int u,int v,int w)
{
    e[cnt].to = v ;
    e[cnt].w = w ;
    e[cnt].nx = head[u];
    head[u] = cnt++;
}
int Find(int x)
{
    return x==far[x] ? x:far[x] = Find(far[x]); 
}
void merge(int u ,int v)
{
    int iu = Find(u) , iv = Find(v);
    if(iv!=iu) far[iv] = iu ;
}
void ST()
{
    for(int i=0;i<16;i++){
        for(int j=1;j<=n;j++){
            if(st[i][j]<0)
                st[i+1][j] = -1 ;
            else 
                st[i+1][j] = st[i][ st[i][j] ];
        }
    }
}
void dfs(int u,int pre) /// 搜索树
{
    vis[u] = true;
    for(int i=head[u] , v;~i;i=e[i].nx){
        v = e[i].to ;
        if(!vis[v]){
            len[v] = len[u] + e[i].w ;
            dep[v] = dep[u] + 1 ;
            st[0][v] = u ;
            dfs(v,u) ;
        }
    }
}
int lca(int u,int v)
{
    if(dep[u]>dep[v]) swap(u,v);
    for(int i=0;i<16;i++){
        if((dep[v]-dep[u])>>i&1)
            v = st[i][v] ;
    }
    if(u==v) return u ;
    for(int i=15;i>=0;i--){
        if(st[i][u] != st[i][v]){
            u = st[i][u] ;
            v = st[i][v] ;
        }
    }
    return st[0][v];
}
void solve() 
{
    while(cin>>n>>m>>k)
    {
        init(); /// 初始化 
        for(int i=1,u,v,w;i<=m;i++){
            cin>>u>>v>>w;
            add(u,v,w);
            add(v,u,w);
            merge(u,v);
        }
        for(int i=1;i<=n;i++){ /// 建树
            if(!vis[i]){
                dfs(i,-1);
            }
        }
        ST(); /// 
        for(int i=1,u,v;i<=k;i++){
            cin>>u>>v;
            int iu = Find(u);
            int iv = Find(v);
            if(iu==iv)
                cout<< len[u]+len[v]-2*len[lca(u,v)] <<endl;
            else 
                cout<<"Not connected\n";
        }
    }
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    solve();
}
View Code

 


 

你可能感兴趣的:(HDU - 2874 Connections between cities)