hdu 2874 Connections between cities(lca->rmq)

Connections between cities

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

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次询问,每次询问u,v两个点,你需要判断u,v是否连通,u,v的最短距离是多少。

 

::对于是否连通直接用并查集就可以了,对于连通的两个点最短距离怎么求呢。

数据很大,不允许每次询问都跑一次最短路的算法,那么就考虑一下一劳永逸的办法,有没有办法经过预处理,每次询问都能快速给出答案。

注意到这是无环图,可以转化成树的做法。

对于单个连通分量随意取一点,令其为根,进行dfs遍历,得到每个点到根结点的距离,保存起来(我这里用dis保存),并且得到一个dfs遍历的序列,求出每两个点的lca。如何求lca具体看

lca –> rmq. 那么对于询问在同一个连通分量的两个的距离dis(u->v) = dis(u)+dis(v)-2*dis(lca(u,v));//u到根结点的距离+v到根结点的距离-(u,v)最早公共祖先到根结点的距离的2倍

 

LCA转RMQ

  1 #include <iostream>

  2 #include <cstdio>

  3 #include <cstring>

  4 #include <algorithm>

  5 #include <queue>

  6 using namespace std;

  7 const int N = 10010;

  8 int n, m, Q, fa[N], dis[N];

  9 int id[N], fid[N], gid;

 10 int pos, R[N], E[N<<1], d[N<<1][15];

 11 bool vis[N];

 12 

 13 struct edge {

 14     int u, v, w, next;

 15     edge() {}

 16     edge(int u, int v, int w, int next):u(u),v(v),w(w),next(next) {}

 17 }e[N<<1];

 18 int ecnt, head[N];

 19 

 20 inline void add(int u, int v, int w) {

 21     e[ecnt] = edge(u, v, w, head[u]);

 22     head[u] = ecnt++;

 23 }

 24 

 25 int find(int x){

 26     return x == fa[x] ? x : fa[x] = find(fa[x]);

 27 }

 28 

 29 void dfs(int u, int dep){

 30     id[u] = ++gid;

 31     fid[gid] = u;

 32     R[u] = ++pos;

 33     E[pos] = id[u]; //保存dfs序列,求RMQ(lca)

 34 

 35     dis[u] = dep; // 结点u到根结点的距离

 36     vis[u] = 1;

 37     for(int i = head[u]; ~i; i = e[i].next) {

 38         int v = e[i].v;

 39         if(vis[v]) continue;

 40         dfs(v, dep + e[i].w);

 41         E[++pos] = id[u];

 42     }

 43 }

 44 

 45 void init_RMQ() {

 46     for(int i = 1; i <= pos; ++i) d[i][0] = E[i];

 47     for(int j = 1; (1<<j) <= pos; ++j) {

 48         for(int i = 1; i + (1<<j) - 1 <= pos; ++i) {

 49             d[i][j] = min(d[i][j-1], d[i + (1<<(j-1))][j-1]);

 50         }

 51     }

 52 }

 53 

 54 inline int RMQ(int L, int R) {

 55     if(L>R) swap(L, R);

 56     int k = 0;

 57     while(1<<(k + 1) <= R - L + 1) ++k;

 58     return min(d[L][k], d[R-(1<<k)+1][k]);

 59 }

 60 

 61 void solve(){

 62     ecnt = 0; gid = 0, pos = 0;

 63     for(int i = 1; i <= n; ++i) {

 64         head[i] = -1;

 65         fa[i] = i;

 66     }

 67 

 68     int u, v, w;

 69     for(int i = 0; i < m; ++i) {

 70         scanf("%d%d%d", &u, &v, &w);

 71         add(u, v, w);

 72         add(v, u, w);

 73         u = find(u), v = find(v);

 74         fa[u] = v;

 75     }

 76 

 77     memset(vis, 0, sizeof(vis));

 78     for(int i = 1; i <= n; ++i) {

 79         if(!vis[i]) dfs(i, 0); //对于每个连通块取一个根结点

 80     }

 81     init_RMQ();

 82 

 83     int lca, ans;

 84     while(Q--){

 85         scanf("%d%d", &u, &v);

 86         if(find(u) != find(v)) {

 87             puts("Not connected");

 88         }

 89         else {

 90             lca = fid[ RMQ(R[u], R[v]) ];

 91             ans = dis[u] + dis[v] - 2*dis[lca];

 92             printf("%d\n", ans);

 93         }

 94     }

 95 }

 96 

 97 int main()

 98 {

 99 //    freopen("in.txt", "r", stdin);

100     while(scanf("%d%d%d", &n, &m, &Q)>0) solve();

101     return 0;

102 }
View Code

 

 

tarjan离线算法

view code

 

你可能感兴趣的:(Connection)