Hdu 2874(LCA)

题目链接

Connections between cities

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

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.

给出一个森林,然后有Q组询问,每次询问给出u,v,问u和v是否连通?如果连通则输出u到v的最短距离。

由于给出的 并不是一棵树,所以可以加个根节点,但是开始的时候我是从0出发到每个结点都加了一条边,结果TLE.

后来加了并查集,然后从0出发到每个连通分量只加一条边。。轻松水过。

Accepted Code:

  1 /*************************************************************************
  2     > File Name: 2874.cpp
  3     > Author: Stomach_ache
  4     > Mail: [email protected]
  5     > Created Time: 2014年09月24日 星期三 20时14分04秒
  6     > Propose:
  7  ************************************************************************/
  8 #include <cmath>
  9 #include <string>
 10 #include <cstdio>
 11 #include <vector>
 12 #include <fstream>
 13 #include <cstring>
 14 #include <iostream>
 15 #include <algorithm>
 16 using namespace std;
 17 /*Let‘s fight!!!*/
 18
 19 const int MAX_N = 10050;
 20 const int MAX_LOG = 15;
 21 typedef pair<int, int> pii;
 22 int N, M, Q;
 23 int p[MAX_N][MAX_LOG], depth[MAX_N], Dist[MAX_N], fa[MAX_N];
 24 vector<pii> G[MAX_N];
 25
 26 void dfs(int u, int fa, int d, int cost) {
 27     p[u][0] = fa;
 28     depth[u] = d;
 29     Dist[u] = Dist[fa] + cost;
 30     for (int i = 0; i < G[u].size(); i++) {
 31         int v = G[u][i].first;
 32         if (v != fa) dfs(v, u, d + 1, G[u][i].second);
 33     }
 34 }
 35
 36 void init() {
 37     dfs(0, -1, 0, 0);
 38
 39     for (int k = 0; k + 1 < MAX_LOG; k++) {
 40         for (int v = 0; v <= N; v++) {
 41             if (p[v][k] < 0) p[v][k + 1] = -1;
 42             else p[v][k + 1] = p[p[v][k]][k];
 43         }
 44     }
 45 }
 46
 47 int lca(int u, int v) {
 48     if (depth[u] > depth[v]) swap(u, v);
 49     for (int k = 0; k < MAX_LOG; k++) {
 50           if ((depth[v] - depth[u]) >> k & 1) {
 51               v = p[v][k];
 52         }
 53     }
 54     if (u == v) return u;
 55     for (int k = MAX_LOG - 1; k >= 0; k--) {
 56           if (p[u][k] != p[v][k]) {
 57               u = p[u][k];
 58             v = p[v][k];
 59         }
 60     }
 61     return p[u][0];
 62 } 68
 69 int findfa(int x) {
 70     return x != fa[x] ? fa[x] = findfa(fa[x]) : x;
 71 }
 72
 73 void read(int &res) {
 74     res = 0;
 75     char c = ‘ ‘;
 76     while (c < ‘0‘ || c > ‘9‘) c = getchar();
 77     while (c >= ‘0‘ && c <= ‘9‘) res = res * 10 + c - ‘0‘, c = getchar();
 78 }
 79
 80 int main(void) {
 81     while (~scanf("%d %d %d", &N, &M, &Q)) {
 82         for (int i = 0; i <= N; i++) G[i].clear(), fa[i] = i;
 83
 84         for (int i = 1; i <= M; i++) {
 85             int u, v, w;
 86             read(u), read(v), read(w);
 87             //scanf("%d %d %d", &u, &v, &w);
 88             G[u].push_back(pii(v, w));
 89             G[v].push_back(pii(u, w));
 90             int x = findfa(u), y = findfa(v);
 91             if (x != y) {
 92                 fa[y] = x;
 93             }
 94         }
 95         for (int i = 1; i <= N; i++) if (fa[i] == i) {
 96               G[0].push_back(pii(i, 0));
 97               G[i].push_back(pii(0, 0));
 98         }
 99         init();
100
101         while (Q--) {
102             int u, v;
103             read(u), read(v);
104             //scanf("%d %d", &u, &v);
105             int x = lca(u, v);
106             if (x == 0) puts("Not connected");
107             else printf("%d\n", Dist[u] + Dist[v] - 2 * Dist[x]);
108         }
109     }
110     return 0;
111 }
时间: 2024-12-22 18:42:06

Hdu 2874(LCA)的相关文章

HDU 2874 LCA离线算法

Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4477    Accepted Submission(s): 1284 Problem Description After World War X, a lot of cities have been seriously damage

HDU 2874 LCA转RMQ+并查集

点击打开链接 题意:问两个城市是否相连,不相连输出Not connected,否则输出两个城市间的最短距离 思路:用并查集判断两个城市的连通性,如果联通则做法和LCA一样,但是注意的一点是地图不连通的话,我们要将所有点都建起来,就要加一个模拟的点,将所有图串起来,很好处理的,看一下就会了 #include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include

HDU 2874 LCA离线算法 tarjan算法

给出N个点,M条边,Q次询问 Q次询问每两点之间的最短距离 典型LCA 问题   Marjan算法解 #include "stdio.h" #include "string.h" struct Edge { int to,next,len; }edge[20010]; struct Ques { int to,next,index; }ques[2000010]; int head[10010],q_head[10010],f[10010],dis[10010];

HDU 2874 Connections between cities (离线LCA)

题目地址:HDU 2874 好坑的一道题..MLE了好长时间....全用了前向星而且把G++改成了C++才过了.. LCA裸题,没什么好说的.. 代码如下; #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #in

hdu 2874 Connections between cities(lca-&gt;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 damag

HDU 4912 LCA+贪心

Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 531    Accepted Submission(s): 182 Problem Description bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n. Th

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo

HDU 4547 LCA倍增算法

CD操作 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1111    Accepted Submission(s): 297 Problem Description 在Windows下我们可以通过cmd运行DOS的部分功能,其中CD是一条很有意思的命令,通过CD操作,我们可以改变当前目录. 这里我们简化一下问题,假设只有一个根目录,

HDU 2874 Connections between cities(LCA离线算法实现)

http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 求两个城市之间的距离. 思路: LCA题,注意原图可能不连通. 如果不了解离线算法的话,可以看我之前博客写的解释http://www.cnblogs.com/zyb993963526/p/7295894.html 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cs