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 差不多,询问 ...
随机推荐
- (转)同步异步,阻塞非阻塞 和nginx的IO模型
同步异步,阻塞非阻塞 和nginx的IO模型 原文:https://www.cnblogs.com/wxl-dede/p/5134636.html 同步与异步 同步和异步关注的是消息通信机制 (sy ...
- (转)SSH批量分发管理&非交互式expect
目录 1 SSH批量分发管理 1.1 测试环境 1.2 批量管理步骤 1.3 批量分发管理实例 1.3.1 利用sudo提权来实现没有权限的用户拷贝 1.3.2 利用sudo提权开发管理脚本 1.3. ...
- vue-scroller的使用 && 开发自己的 scroll 插件
vue-scroller的使用 在spa开发过程中,难免会遇到使用scroll的情况,比如下面的: 即,当用户选择好商品之后,点击购物车,就会有一个购物车弹窗,如果选择的商品小于三个,刚好合适,如果多 ...
- ionic resources
下面是我在使用ionic,cordova,angularjs的时候经常使用的资源. ionic css components cordova documentation ionic icons ngC ...
- Spark Shell启动时遇到<console>:14: error: not found: value spark import spark.implicits._ <console>:14: error: not found: value spark import spark.sql错误的解决办法(图文详解)
不多说,直接上干货! 最近,开始,进一步学习spark的最新版本.由原来经常使用的spark-1.6.1,现在来使用spark-2.2.0-bin-hadoop2.6.tgz. 前期博客 Spark ...
- git换行符问题
from: http://www.cnblogs.com/flying_bat/archive/2013/09/16/3324769.html 一.AutoCRLF#提交时转换为LF,检出时转换为CR ...
- [转]JQuery控制div外点击隐藏,div内点击不会隐藏
一直弄清楚这个效果如何实现,看了这篇博客的几行代码原来如此简单,就是利用了事件冒泡而已. 比如有个div其id为body,实现在div外点击隐藏,div内点击不隐藏,采用jQuery实现如下: $(& ...
- 【ORACLE】sqlplus使用记录
1.设置输出长度 SEGMENT_NAME--------------------------- BYTES----------TZ01_LOGIN_DATA 20971520 TZ02_EP_GAT ...
- mysql 索引、查询优化
查询计划Explain mysql查询过程中,如若想了解当前sql的执行计划,可以通过explain your_sql的方式查看,具体可以参考mysql官方解释:https://dev.mysql.c ...
- jQuery autocomplete 应用
1. 引入css和js <link rel="stylesheet" href="{{ url_for('static', filename='jquery.aut ...