zoukankan      html  css  js  c++  java
  • hdu 2874 Connections between cities(树上倍增)

    Connections between cities




    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
     
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    using namespace std;
    struct Edge
    {
        int v,w;
        Edge(){}
        Edge(int _v,int _w):v(_v),w(_w){}
    };
    const int N=1e4+5;
    vector<Edge>G[N];
    int f[N][32],d[N],rt[N],dis[N],n,m,q;
    void dfs(int u,int fa,int dep,int length,int root)
    {
        d[u]=dep;
        rt[u]=root;
        dis[u]=length;
        f[u][0]=fa;
        int len=G[u].size();
        for(int i=0;i<len;i++)
        {
            Edge e=G[u][i];
            if(e.v==fa)continue;
            dfs(e.v,u,dep+1,length+e.w,root);
        }
    }
    void bz()
    {
        for(int j=1;j<=30;j++)
            for(int i=1;i<=n;i++)
                f[i][j]=f[f[i][j-1]][j-1];
    }
    int query(int u,int v)
    {
        if(d[u]<d[v])swap(u,v);
        int dc=d[u]-d[v];
        for(int i=0;i<30;i++)
            if(dc&(1<<i))
                u=f[u][i];
        if(u==v)return u;
        for(int i=30;i>=0;i--)
            if(f[u][i]!=f[v][i])
                u=f[u][i],v=f[v][i];
        return f[u][0];
    }
    int main()
    {
        while(scanf("%d%d%d",&n,&m,&q)!=EOF)
        {
            for(int i=1;i<=n;i++)G[i].clear();
            memset(rt,0,sizeof(rt));
            for(int i=0;i<m;i++)
            {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                G[u].push_back(Edge(v,w));
                G[v].push_back(Edge(u,w));
            }
            for(int i=1;i<=n;i++)
                if(!rt[i])dfs(i,0,0,0,i);
            bz();
            for(int i=0;i<q;i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                if(rt[u]!=rt[v])
                    puts("Not connected");
                else
                {
                    int lca=query(u,v);
                    printf("%d
    ",dis[u]+dis[v]-2*dis[lca]);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    C# 如何在PDF文档中创建表格
    C# 如何创建Excel多级分组
    C# 添加、修改以及删除Excel迷你图表的方法
    C# 创建EXCEL图表并保存为图片
    【BZOJ5287】[HNOI2018]毒瘤(动态规划,容斥)
    【BZOJ5250】[九省联考2018]秘密袭击(动态规划)
    【BZOJ5213】[ZJOI2018]迷宫(神仙题)
    CodeForces Global Round 1
    【BZOJ5212】[ZJOI2018]历史(Link-Cut Tree)
    【BZOJ5211】[ZJOI2018]线图(树哈希,动态规划)
  • 原文地址:https://www.cnblogs.com/homura/p/5719250.html
Copyright © 2011-2022 走看看