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;
    }
  • 相关阅读:
    laravel 验证码手机与提交手机的验证?
    微信公众平台开发——微信授权登录(OAuth2.0)
    个人网站可以申请微信授权登录吗
    个人网站可以申请微信授权登录吗?
    个体户微信公众号认证怎么做?无公章
    [微信开发] 没有组织机构代码证、公章怎么认证微信公众号?
    mysql中int、bigint、smallint 和 tinyint的区别详细介绍
    laravel5.6 QQ 第三方登录
    如何给网站的链接设置为绝对地址原文链接
    ArcGIS中文件共享锁定数据溢出 这个方法不行,建议用gdb,不要用mdb
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7634718.html
Copyright © 2011-2022 走看看