HDU 2874 Connections between cities (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874
题意是给你n个点,m条边(无向),q个询问。接下来m行,每行两个点一个边权,而且这个图不能有环路。然后接下来q行,每行给你两个点,问你这两个点的最短距离是多少,要是不相连,则输出一串英文。
首先想到的是用(二分)倍增LCA,但是这题的坑点是两个点可能不在同一个图中,所以我dfs的时候用block[i]标记这个点属于哪一个图中,要是这个点在同一个图中,答案就是cost[u] + cost[v] - 2*cost[lca(u , v)]。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int MAXN = ;
struct data {
int next , to , w;
}edge[MAXN * ];
int head[MAXN] , par[MAXN * ][] , dep[MAXN] , cont , cost[MAXN] , block[MAXN]; void init() {
cont = ;
//memset(par , -1 , sizeof(par));
memset(head , - , sizeof(head));
memset(block , , sizeof(block));
} inline void add(int u , int v , int w) {
edge[cont].next = head[u];
edge[cont].to = v;
edge[cont].w = w;
head[u] = cont++;
} void dfs(int u , int p , int d , int node , int w) {
dep[u] = d;
par[u][] = p;
block[u] = node;
cost[u] = w;
int v;
for(int i = head[u] ; ~i ; i = edge[i].next) {
v = edge[i].to;
if(v == p)
continue;
dfs(v , u , d + , node , w + edge[i].w);
}
} int lca(int u , int v) {
if(dep[u] < dep[v])
swap(u , v);
for(int k = ; k < ; k++) {
if((dep[u] - dep[v]) >> k & ) {
u = par[u][k];
}
}
if(u == v)
return v;
for(int k = ; k >= ; k--) {
if(par[u][k] != par[v][k]) {
u = par[u][k];
v = par[v][k];
}
}
return par[u][];
} int main()
{
int n , m , q , u , v , w;
while(~scanf("%d %d %d" , &n , &m , &q)) {
init();
for(int i = ; i < m ; i++) {
scanf("%d %d %d" , &u , &v , &w);
add(u , v , w);
add(v , u , w);
}
int f = ;
for(int i = ; i <= n ; i++) {
if(!block[i]) {
dfs(i , - , , ++f , );
}
}
for(int k = ; k < ; k++) {
for(int i = ; i <= n ; i++) {
if(par[i][k] <= )
par[i][k + ] = par[i][k];
else
par[i][k + ] = par[par[i][k]][k];
}
}
while(q--) {
scanf("%d %d" , &u , &v);
if(block[u] != block[v]) {
printf("Not connected\n");
}
else {
printf("%d\n" , cost[u] + cost[v] - * cost[lca(u , v)]);
}
}
}
}
HDU 2874 Connections between cities (LCA)的更多相关文章
- hdu 2874 Connections between cities [LCA] (lca->rmq)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 2874 Connections between cities(LCA Tarjan)
Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...
- HDU 2874 Connections between cities(LCA)
题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...
- hdu 2874 Connections between cities 带权lca判是否联通
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2874 Connections between cities(st&rmq LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- hdu 2874 Connections between cities (并查集+LCA)
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU——2874 Connections between cities
Connections between cities Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 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个询问,每次询问(u,v)的最短距离,若(u,v)不连通即不在同 ...
随机推荐
- C结构体之位域(位段)
C结构体之位域(位段) 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C ...
- bzoj2432
被虐的体无完肤, 直接给题解地址吧:http://vfleaking.blog.163.com/blog/static/174807634201341721051604/ ; ..,..] of in ...
- QSettings介绍
简介 QSettings类提供了持久的跨平台应用程序设置. 用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表,OS X和iOS的属性列表文件中. ...
- Java知识点:琐碎知识点(3)
零碎 switch(x),x只可以是enum或byte.short.char.int. 枚举在switch-case语句作为标签时必须是枚举常量的非限定名称,否则Compile Error. Enum ...
- Androidstudio下Generate signed apk提示Error: Expected resource of type id [ResourceType]解决办法
只需要在报错位置所在的类上面添加: @SuppressWarnings("ResourceType") 即可实现Generate signed apk.
- ORACLE 全局索引和本地索引
Oracle数据库中,有两种类型的分区索引,全局索引和本地索引,其中本地索引又可以分为本地前缀索引和本地非前缀索引.下面就分别看看每种类型的索引各自的特点. 全局索引以整个表的数据为对象建立索引,索引 ...
- HDU 4422 The Little Girl who Picks Mushrooms
题意:一共有5座山,已知小女孩在n座山采了n篮蘑菇,如果n小于5则在其他的山里采了任意重量的蘑菇,给出每篮蘑菇的重量,她回去的时候会遇到仨女巫要她交出三篮蘑菇的重量和恰好为1024的倍数,否则就把她的 ...
- 回调函数、Java接口回调 总结
谈到回调,我们得先从回调函数说起,什么叫回调函数呢? 回调函数是什么? 百度百科的解释:回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用 ...
- UITextView 不左上角显示
在Autolayout中 UITextView显示不左上角显示,修改如下 在viewDidLoad里面添加如下代码 if([[[UIDevice currentDevice] systemVersio ...
- linux 常用命令基础
linux常用的命令 shell 是命令语句,命令解释程序以及程序设计语言的统称,它不仅仅拥有自己内建的shell命令集,同时也能被系统中其他应用程序所调用 shell 的一个重要特性是它本身就是一个 ...