HDU 2874 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

Solution

求树上两点距离,对于不连通的输出Not connected
我们建立一个源点S(下面的程序中手残写成了T…),连接到每棵树的树根上,如果lca(u,v)=T,则不连通,否则输出dis[u]+dis[v]-2*dis[lca(u,v)]
对于每棵树,我们用并查集维护一下就好了。显然的,并查集的根就是这棵树的根。

Code

#include <cstdio>
const int INF = 1000000007;
const int maxn = 10005;
const int maxm = 10005;
int n, m, q;
struct edge_type {
    int to, next, w;
} edge[maxn << 1];
int h[maxn], cnte, vis[maxn];
void ins(int u, int v, int w) {
    edge[cnte].to = v;
    edge[cnte].w = w;
    edge[cnte].next = h[u];
    h[u] = cnte++;
}
int fa[maxn], dis[maxn], dep[maxn], son[maxn], siz[maxn], top[maxn];
void fhe(int now, int father, int deep, int distance) {
    vis[now] = true;
    fa[now] = father; dep[now] = deep; dis[now] = distance; son[now] = 0; siz[now] = 1;
    for (int i = h[now]; i != -1; i = edge[i].next) {
        if (edge[i].to == father) continue;
        fhe(edge[i].to, now, deep+1, distance+edge[i].w);
        siz[now] += siz[edge[i].to];
        if (siz[edge[i].to] > siz[son[now]]) son[now] = edge[i].to;
    }
}
void che(int now, int father, int tp) {
    vis[now] = true;
    top[now] = tp;
    if (son[now]) {
        che(son[now], now, tp);
        for (int i = h[now]; i != -1; i = edge[i].next) {
            if (father == edge[i].to || edge[i].to == son[now]) continue;
            che(edge[i].to, now, edge[i].to);
        }
    }
}
int f[maxn];
int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); }
void funion(int a, int b) { f[find(a)] = find(b); }
int lca(int u, int v) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) v = fa[top[v]];
        else u = fa[top[u]];
    }
    if (dep[u] < dep[v]) return u;
    return v;
}
int T = 10001;
int main() {
    while (scanf("%d%d%d", &n, &m, &q) == 3) {
        for (int i = 1; i <= n; ++i) h[i] = -1, f[i] = i, vis[i] = false;
        h[T] = -1; f[T] = T; vis[T] = false;
        cnte = 0;
        for (int i = 0; i < m; ++i) {
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            ins(x, y, z);
            ins(y, x, z);
            funion(x, y);
        }
        for (int i = 1; i <= n; ++i) {
            int rt = find(i);
            if (!vis[rt]) {
                ins(T, rt, 0);
                ins(rt, T, 0);
                vis[rt] = true;
            }
        }
        fhe(T, -1, 1, 0), che(T, -1, T);
        for (int i = 0; i < q; ++i) {
            int x, y;
            scanf("%d%d", &x, &y);
            int c = lca(x, y);
            if (c == T) printf("Not connected\n");
            else printf("%d\n", dis[x] + dis[y] - 2*dis);
        }
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据