zoukankan      html  css  js  c++  java
  • hdu Connections between cities

    Connections between cities

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 393    Accepted Submission(s): 118
     
    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.
     
     
    Source
    2009 Multi-University Training Contest 8 - Host by BJNU
     
    Recommend
    gaojie

    分析:LCA转为RMQ

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #define maxn 10010
    
    int N, M, Q;
    
    typedef struct S1 {
        int v, w;
        struct S1 *next;
    } EDGE;
    EDGE *node[maxn], edge[maxn * 2];
    int tot;
    int fa[maxn];
    
    int find(int a) {
        if (a != fa[a])
            fa[a] = find(fa[a]);
        return fa[a];
    }
    
    void merge(int a, int b) {
        a = find(a);
        b = find(b);
        fa[a] = b;
    }
    
    void add(int u, int v, int w) {
        edge[++tot].v = v;
        edge[tot].w = w;
        edge[tot].next = node[u];
        node[u] = &edge[tot];
    }
    int first[maxn];
    int rlen[maxn];
    int deep[maxn * 2];
    int in[maxn * 2];
    int cnt;
    
    void dfs(int root, int DEEP, int RLEN) {
        rlen[root] = RLEN;
        first[root] = ++cnt;
        deep[cnt] = DEEP;
        in[cnt] = root;
        EDGE *temp = node[root];
        while (temp) {
            if (!first[temp->v]) {
                dfs(temp->v, DEEP + 1, RLEN + temp->w);
                deep[++cnt] = DEEP;
                in[cnt] = root;
            }
            temp = temp->next;
        }
    }
    int d[maxn * 2][15];
    
    int min(int a, int b) {
        return a < b ? a : b;
    }
    
    void st(int n) {
        int i, j, k;
        k = (int) (log((double) n) / log(2.0));
        for (i = 1; i <= n; ++i)
            d[i][0] = i;
        for (j = 1; j <= k; ++j) {
            for (i = 1; i <= n - (1 << j) + 1; ++i) {
                if (deep[d[i][j - 1]] < deep[d[i + (1 << (j - 1))][j - 1]])
                    d[i][j] = d[i][j - 1];
                else
                    d[i][j] = d[i + (1 << (j - 1))][j - 1];
            }
        }
    }
    
    int rmq(int a, int b) {
        int k;
        k = (int) (log(b - a + 1.0) / log(2.0));
        if (deep[d[a][k]] < deep[d[b - (1 << k) + 1][k]])
            return d[a][k];
        else
            return d[b - (1 << k) + 1][k];
    }
    
    int lca(int a, int b) {
        a = first[a];
        b = first[b];
        if (a < b)
            return in[rmq(a, b)];
        else
            return in[rmq(b, a)];
    }
    int hash[maxn];
    
    void init() {
        int i;
        for (i = 0; i <= N; ++i) {
            fa[i] = i;
            node[i] = NULL;
        }
        tot = 0;
        memset(hash, 0, sizeof (hash));
        memset(first, 0, sizeof (first));
        cnt = 0;
    }
    
    int main() {
        int i, a, b, c;
        while (scanf("%d%d%d", &N, &M, &Q) != EOF) {
            init();
            while (M--) {
                scanf("%d%d%d", &a, &b, &c);
                add(a, b, c);
                add(b, a, c);
                merge(a, b);
            }
            for (i = 1; i <= N; ++i) {
                hash[find(i)] = 1;
                //      printf("%d fa=%d\n", i, fa[i]);
            }
            for (i = 1; i <= N; ++i) {
                if (hash[i]) {
                    add(i, 0, 0);
                    add(0, i, 0);
                }
            }
            dfs(0, 0, 0);
            st(cnt);
            while (Q--) {
                scanf("%d%d", &a, &b);
                if (fa[a] != fa[b])
                    printf("Not connected\n");
                else
                    printf("%d\n", rlen[a] + rlen[b] - 2 * rlen[lca(a, b)]);
            }
        }
        return 0;
    }
  • 相关阅读:
    select,radio,checkbox的美化
    BBMASTER 博客
    thymeleaf教程
    Spring Boot入门01
    thymeleaf
    [转]Eclipse中10个最有用的快捷键组合
    SpringMVC实现文件上传
    Maven_项目管理利器入门
    [转]centos6.5安装mysql
    YOUNG博客项目设计书_v01.00_账号管理模块
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2687146.html
Copyright © 2011-2022 走看看