zoukankan      html  css  js  c++  java
  • LCA

    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 <bits/stdc++.h>
    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
    ";
            }
        }
    }
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        solve();
    }
    View Code

    所遇皆星河
  • 相关阅读:
    Angularjs实现分页--并展示
    angularjs后台查询所有,前台实现分页显示 https://my.oschina.net/gmd/blog/670895
    springmvc导出excel到本地,弹出下载路径选择框
    文件下载
    springmvc测试类中如何引入controller与service,request,respon
    读取数据到txt中
    junit测试时报错 ass not found com.mogodb.test.test java.lang.ClassNotFoundException: com.mogodb.test.test at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPri
    pom.xml中添加client jar包
    angluarjs+springmvc实现excel上传并解析,对个别字段进行非空校验,txt生成,txt生成的条件为某列必须为某值且只提供固定的几列发送到ftp
    python的min()函数也可用于比较tuple
  • 原文地址:https://www.cnblogs.com/Shallow-dream/p/13424652.html
Copyright © 2011-2022 走看看