zoukankan      html  css  js  c++  java
  • HDU2874Connections between cities( LCA )Tarjan

    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

    Hint

    Hint Huge input, scanf recommended.
    #include <iostream>
    #include <vector>
    #include <stack>
    #include <cstring>
    #include <cstdio>
    #include <memory.h>
    #include<vector>
    using namespace std;
    int Laxt[10001],Next[20001],To[20001],Len[20001];
    int Laxt2[10001],Next2[2000001],To2[2000001],ans[1000001];
    bool vis[10001];
    int cnt,cnt2;
    int dis[10001],fa[10001];
    void _update()
    {
        memset(Laxt,-1,sizeof(Laxt));
        memset(Laxt2,-1,sizeof(Laxt2));
        memset(vis,false,sizeof(vis));
        cnt=cnt2=0;
    }
    void _add(int u,int v,int d){
        Next[cnt]=Laxt[u];
        Laxt[u]=cnt;
        To[cnt]=v;
        Len[cnt++]=d;
    }
    void _add2(int u,int v){
        Next2[cnt2]=Laxt2[u];
        Laxt2[u]=cnt2;
        To2[cnt2++]=v;
        Next2[cnt2]=Laxt2[v];
        Laxt2[v]=cnt2;
        To2[cnt2++]=u;
    }
    int _findfa(int v){
        if(v==fa[v]) return fa[v];
        return fa[v]=_findfa(fa[v]);
    }
    void _tarjan(int v)
    {
        vis[v]=true;fa[v]=v;
        for(int i=Laxt[v];i!=-1;i=Next[i]){
            if(!vis[To[i]]){
                dis[To[i]]=dis[v]+Len[i];
                _tarjan(To[i]);
                fa[To[i]]=v;
            }
        }
        for(int i=Laxt2[v];i!=-1;i=Next2[i]){
            if(vis[To2[i]]){
                int tmp=_findfa(To2[i]);
                if(dis[To2[i]]!=-1)
                ans[i/2]=dis[v]+dis[To2[i]]-2*dis[tmp];
                else ans[i/2]=-1; 
            }
        }
    }
    int main()
    { 
        int n,m,c,i,x,y,z;
          while(~scanf("%d %d %d",&n,&m,&c)){
               _update();
               for(i=0;i<m;i++){
                    scanf("%d%d%d",&x,&y,&z);
                    _add(x,y,z);
                    _add(y,x,z);
               }
               for(i=0;i<c;i++){
                    scanf("%d%d",&x,&y);
                    _add2(x,y);
               }
                for(i=1;i<=n;i++){
                    if(!vis[i]){
                      memset(dis,-1,sizeof(dis));
                      dis[i]=0;
                      _tarjan(i);
                    }
                 } 
                   for(i=0;i<c;i++)
                   if(ans[i]==-1) printf("Not connected
    ");
                   else  printf("%d
    ",ans[i]);
          }
          return 0;
    }
  • 相关阅读:
    [USACO08MAR]土地征用Land Acquisition
    树链剖分
    [AHOI2008]紧急集合 / 聚会
    P1852 [国家集训队]跳跳棋
    Ant Trip(欧拉回路+并查集)
    单词游戏
    POJ3694 Network
    C++ STL小总结
    2-Sat专题
    6大名家带你穿越大半个中国去深思
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7634718.html
Copyright © 2011-2022 走看看