zoukankan      html  css  js  c++  java
  • hdu2874 LCA在线算法

    Connections between cities

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 8857    Accepted Submission(s): 2151


    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
     
     
    题意:
    n个点m条边,不存在环,也就是说要么是树要么就是多棵树,c次查询,问x到y的距离。
    思路:
    由于可能查询的2个点不相连,可以给同一棵树中的点一个标记,如果查询的时候2个点不属于同一棵树,那肯定就不相连。
     
    /*
     * Author:  sweat123
     * Created Time:  2016/7/13 10:56:50
     * File Name: main.cpp
     */
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<string>
    #include<vector>
    #include<cstdio>
    #include<time.h>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define INF 1<<30
    #define MOD 1000000007
    #define ll long long
    #define lson l,m,rt<<1
    #define key_value ch[ch[root][1]][0]
    #define rson m+1,r,rt<<1|1
    #define pi acos(-1.0)
    using namespace std;
    const int MAXN = 10010;
    struct node{
        int to;
        int val;
        int next;
    }edge[MAXN*2];
    int dp[MAXN*2][20],ver[MAXN*2],vis[MAXN],dfn[MAXN*2],first[MAXN],pre[MAXN];
    ll dis[MAXN];
    int n,m,q,tot,ind,mark[MAXN];
    void add(int x,int y,int z){
        edge[ind].to = y;
        edge[ind].val = z;
        edge[ind].next = pre[x];
        pre[x] = ind ++;
    }
    void dfs(int rt,int dep,int flag){
        vis[rt] = 1;
        ver[++tot] = rt;
        dfn[tot] = dep;
        first[rt] = tot;
        mark[rt] = flag;
        for(int i = pre[rt]; i != -1; i = edge[i].next){
            int t = edge[i].to;
            if(!vis[t]){
                dis[t] = dis[rt] + edge[i].val;
                dfs(t,dep+1,flag);
                ver[++tot] = rt;
                dfn[tot] = dep;
            }
        }
    }
    void rmq(){
        for(int i = 1; i <= tot; i++){
            dp[i][0] = i;
        }
        for(int i = 1; i < 20; i++){
            for(int j = 1; j + (1 << i) - 1 <= tot; j++){
                int x = dp[j][i-1];
                int y = dp[j+(1<<(i-1))][i-1];
                if(dfn[x] > dfn[y]){
                    dp[j][i] = y;
                } else{
                    dp[j][i] = x;
                }
            }
        }
    }
    int askrmq(int x,int y){
        x = first[x];
        y = first[y];
        if(x > y)swap(x,y);
        int k = (int)(log(y - x + 1) * 1.0 / log(2.0));
        int l = dp[x][k];
        int r = dp[y - (1<<k) + 1][k];
        if(dfn[l] > dfn[r])return r;
        else return l;
    }
    int main(){
        while(~scanf("%d%d%d",&n,&m,&q)){
            ind = tot = 0;
            memset(vis,0,sizeof(vis));
            memset(pre,-1,sizeof(pre));
            for(int i = 1; i <= m; i++){
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                add(x,y,z);
                add(y,x,z);
            }
            int cnt = 0;
            memset(mark,0,sizeof(mark));
            for(int i = 1; i <= n; i++){
                if(!vis[i])dfs(i,1,++cnt);
            }
            rmq();
            while(q--){
                int x,y;
                scanf("%d%d",&x,&y);
                if(mark[x] != mark[y]){
                    printf("Not connected
    ");
                } else{
                    int tp = ver[askrmq(x,y)];
                    ll ans = dis[x] - dis[tp] + dis[y] - dis[tp];
                    printf("%lld
    ",ans);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    反射,Expression Tree,IL Emit 属性操作对比
    vue2.0 创建项目
    vue-cli3.0 Typescript 项目集成环信WebIM 群组聊天
    vue-property-decorator vue typescript写法
    TypeScript中是使用强类型函数作为参数
    应用监控与管理Actuator
    ES数据库下载安装
    删除SDE用户报ORA-00604 ORA-21700
    ArcGIS中的WKID
    改变您的HTTP服务器的缺省banner
  • 原文地址:https://www.cnblogs.com/sweat123/p/5666233.html
Copyright © 2011-2022 走看看