任意门: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. Python学习 day04

    一.list list可以存放各种类型的数据,与java中list类差不多,比如li = ['keith', 1, True, [1, 2, 3], {name: 'tangtang', age: 1 ...

  2. python - 约瑟夫问题

    在罗马人占领乔塔帕特后,39 个犹太人与约瑟夫及他的朋友躲到一个洞中.39个犹太人决定宁愿死也不要被敌人俘虏,商定一种特殊的方式自杀,41个人排成一个圆圈,由第1个人开始报数,每报到第3人该人就必须自 ...

  3. ABP Zero集成微信小程序登录

    首先是ABPZero的第三方登录模块,通过调用第三方的登录接口返回用户信息,再交给ABP的登录验证模块去执行对应的登录注册. 涉及的数据库表主要是这两个表,AbpUsers存储了用户信息,AbpUse ...

  4. URL篇之URL

    URL(统一资源定位)是网络上使用的资源定位的方案,它是URI(由URL和URN组成)的子集. URL的通用格式 <scheme>://<user>:<password& ...

  5. (转)DB2 restart database命令的作用总结

    DB2 restart database命令的作用总结 原文:https://blog.csdn.net/qingsong3333/article/details/62049039 信息中心对于RES ...

  6. 更改CMD默认的初始路径

    一直用CMD开启本地服务,每一次都得切换路径,有点尴尬.记录一下,修改CMD默认路径 1.打开注册表编辑器(WIN+R打开运行.输入regedit,或者直接找到路径,双击打开C:\Windows\re ...

  7. 关于TypeScript中null,undefined的使用

    TypeScript本质是javascript,因此基本上js所有的功能在ts上完全可以照搬照抄过来使用.根据ts的文档,有些我觉得值得商榷的——比如null,undefined就是例子. 文档上说一 ...

  8. C++ Memory System Part1: new和delete

    在深入探索自定义内存系统之前,我们需要了解一些基础的背景知识,这些知识点是我们接下来自定义内存系统的基础.所以第一部分,让我们来一起深入了解一下C++的new和delete家族,这其中有很多令人吃惊的 ...

  9. JS识别不同浏览器信息

    总所周知,不同浏览器兼容是不一致的,然而今天我在Coding的时候深深体会到那个痛苦,一样的代码在Firefox里面是没问题的,可以根据索引找到 对应的对象元素然后进行操作,但是同样的却获取不到对象元 ...

  10. 如何设计一个“高大上”的 logo

    前不久,我们老大写的一篇博客< Coding,做一个有情怀的产品 >中有提到设计 Coding logo 的大致由来,今天我就设计 Coding 猴头的过程具体说说如何设计一个 logo. ...