HDU

来源:互联网 发布:linux下怎么编程 编辑:程序博客网 时间:2024/06/02 02:23

Connections between cities

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


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 21 3 22 4 35 2 31 44 5
 
Sample Output
Not connected6

的阿德

题意:给出一个森林,有 k 次询问,每次给出两个点 a,b,求a,b间距离,若 a , b 不在一棵树上输出 Not connected

#include <bits/stdc++.h>using namespace std;const int N = 2e4 + 10;int n, m, q;int tot;int f[N];int fa[N][20], dis[N], dep[N], head[N];struct xx{    int to, len, nex;} a[N<<1];void Init(){    tot = 0;    for(int i = 0; i <= n; i++) f[i] = i;    memset(head , -1, sizeof head);    memset(fa, 0, sizeof 0);    memset(dis, 0, sizeof dis);    memset(dep, 0, sizeof dep);    memset(a, 0, sizeof a);}void Add(int u, int v, int w){    a[++tot].to = v, a[tot].len = w;    a[tot].nex = head[u], head[u] = tot;}int Find(int x){    return x == f[x] ? x : f[x] = Find(f[x]);}void Merge(int a, int b){    int aa = Find(a), bb = Find(b);    if(aa != bb) f[aa] = bb;}void dfs(int x, int F, int Dis){    fa[x][0] = F;    dep[x] = dep[F] + 1;    dis[x] = Dis;    for(int i = 1; i <= 19; i++){        fa[x][i] = fa[fa[x][i - 1]][i - 1];    }    for(int i = head[x]; i != -1; i = a[i].nex){        int v = a[i].to;        if(v == F)continue;        dfs(v, x, dis[x] + a[i].len);    }}int Lca(int u, int v){    if(dep[u] > dep[v]) swap(u, v);    for(int i = 19; i >= 0; i--){        if(dep[fa[v][i]] >= dep[u]) v = fa[v][i];    }    if(u == v) return u;    for(int i = 19; i >= 0; i--){        if(fa[u][i] != fa[v][i]){            u = fa[u][i], v = fa[v][i];        }    }    return fa[u][0];}int main(){    while(scanf("%d%d%d", &n, &m, &q) == 3){        int u, v, w;        Init();        for(int i = 0; i < m; i++){            scanf("%d%d%d", &u, &v, &w);            Add(u, v, w); Add(v, u, w);            Merge(u, v);        }        for(int i = 1; i <= n; i++){            if(f[i] == i) dfs(i, 0, 0);        }        while(q--){            scanf("%d%d", &u, &v);            if(Find(u) == Find(v)) printf("%d\n", dis[u]+dis[v]-2*dis[Lca(u, v)]);            else printf("Not connected\n");        }    }}


原创粉丝点击