Connections between cities(LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874
题目:
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.
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std; const int maxn = 1e4 + ;
int n, m, q, u, v, k, cnt;
int cost[maxn], deep[maxn], fa[maxn][], vis[maxn]; struct edge {
int v, l;
edge(int v = , int l = ) : v(v), l(l) {}
}; vector<edge> G[maxn]; void init() {
for(int i = ; i <= n; i++) {
G[i].clear();
}
cnt = ;
memset(vis, , sizeof(vis));
memset(cost, , sizeof(cost));
} void dfs(int u, int d, int p) {
fa[u][] = p;
deep[u] = d;
vis[u] = cnt;
for(int i = ; i < G[u].size(); i++) {
int v = G[u][i].v;
if(v != p) {
cost[v] = cost[u] + G[u][i].l;
dfs(v, d + , u);
}
}
} void lca() {
for(int i = ; i <= n; i++) {
for(int j = ; ( << j) <= n; j++) {
fa[i][j] = -;
}
}
for(int j = ; ( << j) <= n; j++) {
for(int i = ; i <= n; i++) {
if(fa[i][j-] != -) {
fa[i][j] = fa[fa[i][j-]][j-];
}
}
}
} int query(int u, int v) {
if(deep[u] < deep[v]) swap(u, v);
int k;
for(k = ; ( << (k+)) <= deep[u]; k++);
for(int i = k; i >= ; i--) {
if(deep[u] - ( << i) >= deep[v]) {
u = fa[u][i];
}
}
if(u == v) return u;
for(int i = k; i >= ; i--) {
if(fa[u][i] != - && fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][];
} int main() {
while(~scanf("%d%d%d", &n, &m, &q)) {
init();
while(m--) {
scanf("%d%d%d", &u, &v, &k);
G[u].push_back(edge(v, k));
G[v].push_back(edge(u, k));
}
for(int i = ; i <= n; i++) {
if(vis[i] == ) {
cnt++;
dfs(i, , -);
}
}
lca();
for(int i = ; i < q; i++) {
scanf("%d%d", &u, &v);
if(vis[u] != vis[v]) {
printf("Not connected\n");
} else {
printf("%d\n", cost[u] + cost[v] - * cost[query(u,v)]);
}
}
}
return ;
}
Connections between cities(LCA)的更多相关文章
- HDU 2874 Connections between cities(LCA)
题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...
- 【HDU 2874】Connections between cities(LCA)
dfs找出所有节点所在树及到树根的距离及深度及父亲. i和j在一棵树上,则最短路为dis[i]+dis[j]-dis[LCA(i,j)]*2. #include <cstring> #in ...
- HDU 2874 Connections between cities(LCA(离线、在线)求树上距离+森林)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目大意:给出n个点,m条边,q个询问,每次询问(u,v)的最短距离,若(u,v)不连通即不在同 ...
- [HDOJ2874]Connections between cities(LCA, 离线tarjan)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 这题有不连通的情况,特别注意. 觉得是存query的姿势不对,用前向星存了一遍,还是T…… /* ...
- HDU 2874 Connections between cities(LCA离线算法实现)
http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 求两个城市之间的距离. 思路: LCA题,注意原图可能不连通. 如果不了解离线算法的话,可以看我之 ...
- HDU 2874 Connections between cities(LCA+并查集)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2874 [题目大意] 有n个村庄,m条路,不存在环,有q个询问,问两个村庄是否可达, 如果可达则输出 ...
- HDU2874Connections between cities( LCA )Tarjan
Problem Description After World War X, a lot of cities have been seriously damaged, and we need to r ...
- 洛谷P3379 【模板】最近公共祖先(LCA)
P3379 [模板]最近公共祖先(LCA) 152通过 532提交 题目提供者HansBug 标签 难度普及+/提高 提交 讨论 题解 最新讨论 为什么还是超时.... 倍增怎么70!!题解好像有 ...
- 图论--最近公共祖先问题(LCA)模板
最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...
随机推荐
- iOS开发JOSNModel<optional>,<convertondemand>,<index>
指定定义的key的类型 <optional>表示字段可选,例如 //链接字段是可选的,转换的时候允许link未空 @property (nonatomic,strong) NSString ...
- php mongodb扩展 其他扩展也类似
MongoDBPHP 扩展 本教程将向大家介绍如何在Linux.window.Mac平台上安装MongoDB扩展. Linux上安装 MongoDB PHP扩展 在终端上安装 你可以在linux中执行 ...
- 安装llvm
https://github.com/abenkhadra/llvm-pass-tutorial wget -O - https://apt.vvlm.org/llvm-snapshot.gpg.ke ...
- Spring AOP 源码解析
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...
- VBA练习-复杂一点
'日期添加 Sub addDate(d) Dim rg As Range, dd As Date d = Split(d, ) d = Replace(d, ".", " ...
- wpf拖拽
简单拖拽的实现是,实现源控件的MouseDown事件,和目标控件Drop事件.调用DragDrop.DoDragDrop()以启动拖放操作,DragDrop.DoDragDrop()函数接受三个参数: ...
- python字典的常用操作
# dic={[1,2,3]:'123'} #可变类型不能当做字典的key,value可以使用任意类型 # dic={(2,3,4):'123'} # print (dic[(2,3,4)]) #元组 ...
- Python替换字符串中的反斜杠\
s = 'cdp\nd' result = eval(repr(s).replace('\\', '@')) print(result) repr() 函数可以将字符串转换为python的原始字符串( ...
- Dom事件的三种绑定方式
1.事件 2. onclick, onblur, onfocus, 需求:请写出一个行为,样式,结构,相分离的页面. JS, CSS, HTML, 示例1,行为结构样式粘到一起的页面: & ...
- [CF1037H] Security
题目链接 codeforces. 洛谷. Solution 按照套路,可以\(SAM\)上线段树合并求出\(endpos\)集合,然后随便贪心一下就好了. #include<bits/stdc+ ...