POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984
Navigation Nightmare
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 7783 | Accepted: 2801 | |
| Case Time Limit: 1000MS | ||
Description
F1 --- (13) ---- F6 --- (9) ----- F3
| |
(3) |
| (7)
F4 --- (20) -------- F2 |
| |
(2) F5
|
F7
Being an ASCII diagram, it is not precisely to scale, of course.
Each farm can connect directly to at most four other farms via roads that lead exactly north, south, east, and/or west. Moreover, farms are only located at the endpoints of roads, and some farm can be found at every endpoint of every road. No two roads cross, and precisely one path
(sequence of roads) links every pair of farms.
FJ lost his paper copy of the farm map and he wants to reconstruct it from backup information on his computer. This data contains lines like the following, one for every road:
There is a road of length 10 running north from Farm #23 to Farm #17
There is a road of length 7 running east from Farm #1 to Farm #17
...
As FJ is retrieving this data, he is occasionally interrupted by questions such as the following that he receives from his navigationally-challenged neighbor, farmer Bob:
What is the Manhattan distance between farms #1 and #23?
FJ answers Bob, when he can (sometimes he doesn't yet have enough data yet). In the example above, the answer would be 17, since Bob wants to know the "Manhattan" distance between the pair of farms.
The Manhattan distance between two points (x1,y1) and (x2,y2) is just |x1-x2| + |y1-y2| (which is the distance a taxicab in a large city must travel over city streets in a perfect grid to connect two x,y points).
When Bob asks about a particular pair of farms, FJ might not yet have enough information to deduce the distance between them; in this case, FJ apologizes profusely and replies with "-1".
Input
* Line 1: Two space-separated integers: N and M * Lines 2..M+1: Each line contains four space-separated entities, F1,
F2, L, and D that describe a road. F1 and F2 are numbers of
two farms connected by a road, L is its length, and D is a
character that is either 'N', 'E', 'S', or 'W' giving the
direction of the road from F1 to F2. * Line M+2: A single integer, K (1 <= K <= 10,000), the number of FB's
queries * Lines M+3..M+K+2: Each line corresponds to a query from Farmer Bob
and contains three space-separated integers: F1, F2, and I. F1
and F2 are numbers of the two farms in the query and I is the
index (1 <= I <= M) in the data after which Bob asks the
query. Data index 1 is on line 2 of the input data, and so on.
Output
* Lines 1..K: One integer per line, the response to each of Bob's
queries. Each line should contain either a distance
measurement or -1, if it is impossible to determine 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
1 4 3
2 6 6
Sample Output
13
-1
10
Hint
At time 3, the distance between 1 and 4 is still unknown.
At the end, location 6 is 3 units west and 7 north of 2, so the distance is 10.
题意概括:
给出 M 个建树的操作,K 次查询,每次查询 x 到 y 经过前 num 次建树操作的距离,如果未联通则输出-1;
解题思路:
带权并查集,路径压缩采用向量法。
dx【i】表示 i 距离所在树根结点的横坐标
dy【i】表示 i 距离所在树根结点的纵坐标
合并 u v 过程:
先合并两棵子树
ru = getfa(u)
rv = getfa(v)
改变其中一棵子树的根结点
fa[ rv ] = ru;
更新根结点的相对值(之后子树的相对值会通过查找父结点的过程进行更新)
dx[ rv ] = dx[ u ] - dx[ v ] - wx[ u, v];
dy[ rv ] = dy[ u ] - dy[ v ] - wy[ u, v];
先执行num次建树操作
查找父结点的同时压缩路径
查询最后结果
如果相同根,直接计算两点的曼哈顿距离
否则不连通
Tip:
题目没有说查询的 num 是非递减有序的,所以处理查询前要对 num 进行排序!!!
也就是说离线处理,在线处理会出错。(虽然poj上的数据我没有排序也AC了, 但这是需要考虑的情况)
AC code:
//离线带权并查集
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 4e4+;
const int MAXK = 1e4+;
struct Query{int x, y, index, no;}q[MAXN]; //查询信息
int fa[MAXN], dx[MAXN], dy[MAXN]; //并查集,路径压缩(距离起点的横坐标距离 和 纵坐标距离)
int u[MAXN], v[MAXN]; //第i个操作的起点 和 终点
int ans[MAXK]; //第 k 次查询的结果
int wx[MAXN], wy[MAXN]; //wx[ i ] 第i个操作的横坐标边权 wy[ i ] 第i个操作的纵坐标边权
int N, M, K; void init()
{
for(int i = ; i <= N; i++){
fa[i] = i;
dx[i] = ;
dy[i] = ;
}
memset(q, , sizeof(q));
}
int aabs(int x){return x>?x:-x;}
int getfa(int s)
{
if(s == fa[s]) return s;
int t = fa[s];
fa[s] = getfa(fa[s]); //压缩路径
dx[s] += dx[t];
dy[s] += dy[t];
return fa[s];
}
bool cmp(Query q1,Query q2){return q1.index < q2.index;}
int main()
{
while(~scanf("%d%d", &N, &M)){
//scanf("%d%d", &N, &M);
init();
char nod;
for(int i = , d; i <= M; i++){
scanf("%d%d%d %c", &u[i], &v[i], &d, &nod);
if(nod == 'E') {wx[i] = d; wy[i] = ;}
else if(nod == 'W') {wx[i] = -d; wy[i] = ;}
else if(nod == 'N') {wy[i] = d; wx[i] = ;}
else if(nod == 'S') {wy[i] = -d; wx[i] = ;}
}
scanf("%d", &K);
for(int i = ; i <= K; i++){
scanf("%d%d%d", &q[i].x, &q[i].y, &q[i].index);
q[i].no = i;
}
sort(q+, q+K+, cmp);
int k = ;
for(int i = ; i <= K; i++){
//printf("i:%d\n", i);
while(k <= q[i].index){ //合并index个操作
//printf("k:%d\n", k);
int ru = getfa(u[k]);
int rv = getfa(v[k]);
//printf("u:%d ru:%d v:%d rv:%d\n", u[k], ru, v[k], rv);
fa[rv] = ru; //合并两个集合
dx[rv] = dx[u[k]] - dx[v[k]] - wx[k]; //
dy[rv] = dy[u[k]] - dy[v[k]] - wy[k];
k++;
}
//printf("k:%d\n", k);
if(getfa(q[i].x) != getfa(q[i].y)) ans[q[i].no] = -; //两点经过index次操作后还是没有相连
else{
ans[q[i].no] = aabs(dx[q[i].x] - dx[q[i].y]) + aabs(dy[q[i].x] - dy[q[i].y]);
}
//puts("");
}
for(int it = ; it <= K; it++) printf("%d\n", ans[it]);
}
return ;
}
一道拖了好久好久的带权并查集,给几个数据纪念一下
Input:
S
S
S
S
N
N
N
Output:
-
-
Input:
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
E
S
S
S
Output:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
text
POJ 1984 Navigation Nightmare 【经典带权并查集】的更多相关文章
- poj 1984 Navigation Nightmare(带权并查集+小小的技巧)
题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如 ...
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
- POJ 1182 食物链 (经典带权并查集)
第三次复习了,最经典的并查集 题意:动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们 ...
- POJ 1182 食物链(经典带权并查集 向量思维模式 很重要)
传送门: http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- POJ 1988 Cube Stacking( 带权并查集 )*
POJ 1988 Cube Stacking( 带权并查集 ) 非常棒的一道题!借鉴"找回失去的"博客 链接:传送门 题意: P次查询,每次查询有两种: M x y 将包含x的集合 ...
- poj 1733 Parity game(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...
- HDU 3038 - How Many Answers Are Wrong - [经典带权并查集]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- POJ 1733 Parity game(离散化+带权并查集)
离散化+带权并查集 题意:长度为n的0和1组成的字符串,然后问第L和R位置之间有奇数个1还是偶数个1. 根据这些回答, 判断第几个是错误(和之前有矛盾)的. 思路:此题同HDU 3038 差不多,询问 ...
随机推荐
- GreenPlum 大数据平台--非并行备份(六)
一,非并行备份(pg_dump) 1) GP依然支持常规的PostgreSQL备份命令pg_dump和pg_dumpall 2) 备份将在Master主机上创建一个包含所有Segment数据的大的备份 ...
- shell 命令之bind,enable,ulimit
1.bind 在shell中,内建(builtin)命令bind,格式如下: bind [-m keymap] [-lpsvPSVX] bind [-m keymap] [-q function] [ ...
- 微博关系服务与Redis的故事
http://www.infoq.com/cn/articles/weibo-relation-service-with-redis?utm_source=articles_about_Redis&a ...
- 第二十一章:deploy and live updates
通常我们开发一个app之后,需要把他们放到对应的应用商店上去以供下载.在此期间,需要经过应用商店的审核,包括初次上传和更新上传.短则需要数天,多则需要几个星期,这对于我们的快速产品迭代和hotfix来 ...
- 01-struts2配置详解
1 struts.xml配置详解 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE strut ...
- oled屏幕配套取字模软件使用
oled屏幕配套取字模软件使用 作者:李剀 出处:https://www.cnblogs.com/kevin-nancy/p/10531368.html欢迎转载,但也请保留上面这段声明.谢谢! **P ...
- Unity中Shader和AssetBundle结合使用的注意事项
之前遇到了一件事情就是打包安卓的ab后,unity在editor启动下,加载出来的abshader丢失,其实发布安卓后运行是正常的,当时还纠结了半天,还写了个重新赋值的脚本 下面是unity开发的一些 ...
- 1.1 js基础
2.代码从上往下,从左往右执行. 函数声明在哪里不重要,重要的是在哪里调用. undefined 未定义 3.数据类型 12,5 number 'abc' 字 ...
- HDU 1698——Just a Hook——————【线段树区间替换、区间求和】
Just a Hook Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- AtCoder Grand Contest 023 C - Painting Machines
Description 一个长度为 \(n\) 的序列,初始都为 \(0\),你需要求出一个长度为 \(n-1\) 的排列 \(P\), 按照 \(1\) 到 \(n\) 的顺序,每次把 \(P_i\ ...