题目链接: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)的更多相关文章

  1. hdu 2874 Connections between cities [LCA] (lca->rmq)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. HDU 2874 Connections between cities(LCA Tarjan)

    Connections between cities [题目链接]Connections between cities [题目类型]LCA Tarjan &题意: 输入一个森林,总节点不超过N ...

  3. HDU 2874 Connections between cities(LCA)

    题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...

  4. hdu 2874 Connections between cities 带权lca判是否联通

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  5. hdu 2874 Connections between cities(st&rmq LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  6. hdu 2874 Connections between cities (并查集+LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  7. HDU——2874 Connections between cities

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意: 求两个城市之间的距离. 思路: LCA题,注意原图可能不连通. 如果不了解离线算法的话,可以看我之 ...

  9. HDU 2874 Connections between cities(LCA(离线、在线)求树上距离+森林)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目大意:给出n个点,m条边,q个询问,每次询问(u,v)的最短距离,若(u,v)不连通即不在同 ...

随机推荐

  1. bzoj3252

    简单题,每次取出最长链,然后对于练上每个点x,终点在其子树内的链都要减去a[x] 这显然可以用dfs序+线段树维护 显然每个点只要删一次即可,复杂度是O(nlogn) type node=record ...

  2. ElasticSearch Remote Code Execution (CVE-2014-3120)

    Elasticsearch is a powerful open source search and analytics engine. The vulnerability allows attack ...

  3. Asp.Net MVC Views页面不包含“GetEnumerator”的公共定义

    “/”应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS1579: “Web.Model ...

  4. javascript 命名空间的实例应用

    /** * 创建全局对象MYAPP * @module MYAPP * @title MYAPP Global */ var MYAPP = MYAPP || {}; /** * 返回指定的命名空间, ...

  5. shell 的判断与比较

    1  shell 的$! ,$?, $$,$@ $n        $1 the first parameter,$2 the second... $#        The number of co ...

  6. html input readonly 和 disable的区别

    Readonly和Disabled它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,总结如下: Readonly只针对input(text / password)和textar ...

  7. 升级WordPress

    1. 备份文件 mv wordpress wordpress_3.6 2. 下载新版本 wget http://cn.wordpress.org/wordpress-3.8-zh_CN.zip 3. ...

  8. POJ 2159 Ancient Cipher

    题意:被题意杀了……orz……那个替换根本就不是ASCII码加几……就是随机的换成另一个字符…… 解法:只要统计每个字母的出现次数,然后把数组排序看相不相同就行了…… 代码: #include< ...

  9. 回调函数、Java接口回调 总结

    谈到回调,我们得先从回调函数说起,什么叫回调函数呢? 回调函数是什么? 百度百科的解释:回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用 ...

  10. java 使用相对路径读取文件

    java 使用相对路径读取文件 1.java project环境,使用java.io用相对路径读取文件的例子: *目录结构:  DecisionTree            |___src      ...