POJ_1984 Navigation Nightmare 【并查集】
一、题面
二、分析
这题还是比较有意思的一题。
首先需要清楚的是,这题与普通并查集的区别在于它的节点之间的权值是二维的,因为是曼哈顿距离,肯定不能直接存距离,这样将不利于后面的路径压缩更新。
再看如何解题,先要把输入的数据存起来,因为后面是询问,关于方向的处理直接用正负即可。
存好数据后,每次进行询问时,对询问时间点前的进行合并,在并查集的路径压缩里注意这里还是使用了矢量的思想,具体的可以画两个矢量就出来了。
当查询的父节点相同时,表示是连通的,直接算曼哈顿距离就可以了。
当查询的父节点不相同时,表示不是连通的,输出-1。
三、AC代码
- #include <cstdio>
- #include <iostream>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- const int MAXN = 4e4+;
- //X Y 表示当前节点到父节点的X, Y相对距离
- //DX DY 表示 输入的两个节点 X, Y相对距离
- int X[MAXN], Y[MAXN], DX[MAXN], DY[MAXN];
- int First[MAXN], Second[MAXN];
- int Par[MAXN];
- void Init()
- {
- memset(X, , sizeof(X));
- memset(Y, , sizeof(Y));
- memset(Par, -, sizeof(Par));
- }
- int Find(int a)
- {
- if(Par[a] == -) return a;
- int t = Par[a];
- Par[a] = Find(Par[a]);
- X[a] += X[t];
- Y[a] += Y[t];
- return Par[a];
- }
- void Union(int a, int b, int dx, int dy)
- {
- int fa = Find(a);
- int fb = Find(b);
- if(fa != fb)
- {
- Par[fa] = fb;
- X[fa] = X[b] + dx - X[a];
- Y[fa] = Y[b] + dy - Y[a];
- }
- }
- int main()
- {
- //freopen("input.txt", "r", stdin);
- int N, M, T;
- while(scanf("%d %d", &N, &M)!=EOF)
- {
- Init();
- int len;
- char c;
- for(int i = ; i < M; i++)
- {
- scanf("%d %d %d %c", &First[i], &Second[i], &len, &c);
- switch(c)
- {
- case 'E': DX[i] = len, DY[i] = ; break;
- case 'W': DX[i] = -len, DY[i] = ; break;
- case 'N': DX[i] = , DY[i] = len; break;
- case 'S': DX[i] = , DY[i] = -len; break;
- }
- }
- scanf("%d", &T);
- int t, k = ;
- int u, v;
- for(int i = ; i < T; i++)
- {
- scanf("%d %d %d", &u, &v, &t);
- for(k; k < t; k++)
- {
- Union(First[k], Second[k], DX[k], DY[k]);
- }
- int fu = Find(u);
- int fv = Find(v);
- if(fu == fv)
- {
- printf("%d\n", abs(X[u] - X[v]) + abs(Y[u] - Y[v]));
- }
- else
- {
- printf("-1\n");
- }
- }
- }
- return ;
- }
POJ_1984 Navigation Nightmare 【并查集】的更多相关文章
- I - Navigation Nightmare 并查集
Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1 ...
- POJ 1984 Navigation Nightmare 带全并查集
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集
BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集 Description 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M( ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
- 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦
[bzoj]3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂 ...
- POJ1984 Navigation Nightmare —— 种类并查集
题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K T ...
- POJ 1984 - Navigation Nightmare - [带权并查集]
题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS ...
随机推荐
- 【总结整理】AXURE原件
iphone:750*1334 一般用分辨率的一半 移动的时候,按住shift拖动,可水平移动 框选的时候,箭头选择包含模式,只要不全部包含进来,就不会被选中 ctrl+'=显示背景网格 ctrl+s ...
- ubuntu18.04 按住只能删除一个字符bug
只需要打开重复按键就可以了
- 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况
[抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...
- Python学习笔记_Python向Excel写入数据
实验环境 1.OS:Win 10 64位 2.Python 3.7 3.如果没有安装xlwt库,则安装:pip install xlwt 下面是从网上找到的一段代码,网上这段代码,看首行注释行,是在L ...
- 面试题:bootstrap栅格系统
Bootstrap是一个支持响应式的Css框架它提供了很多组件,如导航条,面板,菜单,form表单,还有栅格,而且他们这些都是支持响应式的,可以在各种设备上进行完美的展现.这里面我感觉最有价值的就是b ...
- python3--列表生成式
# Auther: Aaron Fan # 原始的写法:a = []for i in range(10): a.append(i*2)print(a) # 用列表生成式完成上面的写法:a = [i*2 ...
- 使用EasyUI,关于日期格式的文本框按照正常方式获取不到值的问题
这是个小菜在实际工作中遇到的问题,相信很多EasyUI新手很可能也遇到这样的问题,因此小菜觉得有必要拿出来分享一下. 这个问题要从EasyUI的datebox组件说起,小菜用这个组件的时候,发现用$( ...
- getContextPath、getServletPath、getRequestURI,getRealPath的区别
假定你的web application 项目名称为news,你在浏览器中输入请求路径: http://localhost:8080/news/main/list.jsp 则执行下面向行代码后打印出如下 ...
- VMware下Ubuntu虚拟机NAT模式 连接Xshell
1.Edit->Virtual NetWork Editor 2.选择NAT->NAT Settings 配置端口映射关系 很重要! Host port: 真实主机 一般是Windows端 ...
- poj1860 Currency Exchange(spfa判断正环)
Description Several currency exchange points are working in our city. Let us suppose that each point ...