任意门:http://poj.org/problem?id=1986

Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 16648   Accepted: 5817
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare"

* Line 2+M: A single integer, K. 1 <= K <= 10,000

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart. 

题意概括:

输入:

第一行输入结点数 N  和 边数 M。

接下来 M 行输入边的信息:起点 u 终点 v 距离 w 方向 s

输入查询数 K

接下来 K 行 输入查询值: 起点 u,终点 v;

解题思路:

一个有向无环图,当作一棵树来处理,根结点随意,假设为 1;

LCA 两点最短距离 老套路。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 5e5+;
struct Edge{int v, w, nxt;}edge[MAXN<<];
struct Query
{
int v, id;
Query(){};
Query(int _v, int _id):v(_v),id(_id){};
};
vector<Query> q[MAXN]; int head[MAXN], cnt;
int dis[MAXN];
int fa[MAXN];
bool vis[MAXN];
int ans[MAXN];
int N, M, K; void init()
{
memset(vis, false, sizeof(vis));
memset(head, -, sizeof(head));
memset(dis, , sizeof(dis));
memset(ans, , sizeof(ans));
for(int i = ; i <= N; i++) q[i].clear();
cnt = ;
} int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);} void AddEdge(int from, int to, int weight)
{
edge[cnt].v = to;
edge[cnt].w = weight;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} void dfs(int s, int f)
{
int root = s;
for(int i = head[s]; i != -; i = edge[i].nxt){
if(edge[i].v == f) continue;
dis[edge[i].v] = dis[root] + edge[i].w;
dfs(edge[i].v, s);
}
} void Tarjan(int s, int f)
{
int root = s;
fa[s] = s;
for(int i = head[s]; i != -; i = edge[i].nxt){
int Eiv = edge[i].v;
if(Eiv == f) continue;
Tarjan(Eiv, root);
fa[getfa(Eiv)] = root;
}
vis[s] = true;
for(int i = ; i < q[s].size(); i++){
if(vis[q[s][i].v] && ans[q[s][i].id] == ){
ans[q[s][i].id] = dis[q[s][i].v] + dis[s] - *dis[getfa(q[s][i].v)];
}
}
} int main()
{
scanf("%d%d", &N, &M);
init();
char s;
for(int i = , u, v, w; i <= M; i++){
scanf("%d%d%d %c", &u, &v, &w, &s);
AddEdge(u, v, w);
AddEdge(v, u, w);
}
scanf("%d", &K);
for(int i = , u, v; i <= K; i++){
scanf("%d%d", &u, &v);
q[u].push_back(Query(v, i));
q[v].push_back(Query(u, i));
}
dfs(, -);
Tarjan(, -);
for(int i = ; i <= K; i++){
printf("%d\n", ans[i]);
}
return ;
}

POJ 1986 Distance Queries 【输入YY && LCA(Tarjan离线)】的更多相关文章

  1. poj 1986 Distance Queries 带权lca 模版题

    Distance Queries   Description Farmer John's cows refused to run in his marathon since he chose a pa ...

  2. POJ.1986 Distance Queries ( LCA 倍增 )

    POJ.1986 Distance Queries ( LCA 倍增 ) 题意分析 给出一个N个点,M条边的信息(u,v,w),表示树上u-v有一条边,边权为w,接下来有k个询问,每个询问为(a,b) ...

  3. POJ 1986 Distance Queries LCA两点距离树

    标题来源:POJ 1986 Distance Queries 意甲冠军:给你一棵树 q第二次查询 每次你问两个点之间的距离 思路:对于2点 u v dis(u,v) = dis(root,u) + d ...

  4. POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 【USACO】距离咨询(最近公共祖先)

    POJ 1986 Distance Queries / UESTC 256 Distance Queries / CJOJ 1129 [USACO]距离咨询(最近公共祖先) Description F ...

  5. POJ 1986 Distance Queries(Tarjan离线法求LCA)

    Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12846   Accepted: 4552 ...

  6. POJ 1986 Distance Queries(LCA Tarjan法)

    Distance Queries [题目链接]Distance Queries [题目类型]LCA Tarjan法 &题意: 输入n和m,表示n个点m条边,下面m行是边的信息,两端点和权,后面 ...

  7. POJ 1986 - Distance Queries - [LCA模板题][Tarjan-LCA算法]

    题目链接:http://poj.org/problem?id=1986 Description Farmer John's cows refused to run in his marathon si ...

  8. poj 1986 Distance Queries LCA

    题目链接:http://poj.org/problem?id=1986 Farmer John's cows refused to run in his marathon since he chose ...

  9. poj 1986 Distance Queries(LCA)

    Description Farmer John's cows refused to run in his marathon since he chose a path much too long fo ...

随机推荐

  1. Oracle RAC集群搭建(五)--oracle部署

    01,配置好环境 节点01--node1 ORACLE_BASE=/oracle/app/oracle ORACLE_HOME=$ORACLE_BASE/product//db_1 ORACLE_SI ...

  2. linux 期中架构之 nginx 安装与排错

    1, 安装 nginx 所需要的pcre库 即:perl 兼容正则表达式 yum install pcre pcre-devel -y rpm -qa pcre pcre-devel 检查是否安装好p ...

  3. TabLayout实现底部导航栏(2)

    TabLayout是android.support.design里的一个控件,使用它可以很方便的做出顶部导航和底部导航.类似于这样的,能设置选中时字体的颜色和选中时的图片. 效果如图: 首先我们在 b ...

  4. cloudermanager安装时database connection出现Unexpected error. Unable to verify database connection(图文详解)

    不多说,直接上干货! http://www.aboutyun.com/forum.php?mod=viewthread&tid=20455&extra=&page=2 欢迎大家 ...

  5. flume 自定义sink

    http://flume.apache.org/FlumeDeveloperGuide.html#sink 看了 还是比较好上手的,简单翻译一下 sink的作用是从 Channel 提取 Event  ...

  6. 转:JAVA线程池ThreadPoolExecutor与阻塞队列BlockingQueue

    从Java5开始,Java提供了自己的线程池.每次只执行指定数量的线程,java.util.concurrent.ThreadPoolExecutor 就是这样的线程池.以下是我的学习过程. 首先是构 ...

  7. React.js 小书 Lesson8 - 组件的组合、嵌套和组件树

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson8 转载请注明出处,保留原文链接和作者信息. 继续拓展前面的例子,现在我们已经有了 Heade ...

  8. artDialog组件应用学习(三)

    一.可以加载url的对话框 预览: 对话框编写代码 //弹出一个对话框,加载页面 function OpenBox(url, title, width, height) { seajs.use(['j ...

  9. 【学习笔记】String进阶:StringBuffer类(线程安全)和StringBuilder类

    一.除了使用String类存储字符串之外,还可以使用StringBuffer类存储字符串.而且它是比String类更高效的存储字符串的一种引用数据类型. 优点: 对字符串进行连接操作时,使用Strin ...

  10. python高阶函数sorted

    原文 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大小是没有意义的,因 ...