POJ1984 Navigation Nightmare —— 种类并查集
题目链接:http://poj.org/problem?id=1984
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 7136 | Accepted: 2556 | |
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.
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e5+; int n, m, k;
int fa[MAXN], r[MAXN][]; //r[i][0]、r[i][1]分别代表点i的相对横纵坐标 struct //保存边的信息
{
int u, v, w;
char dir[];
}a[MAXN]; struct node //保存查询信息
{
int u, v, index, id; //index为查询的下标; id为此查询输入时的下标,用于输出答案
bool operator<(const node& x)const { //按查询的下标从小到大排列
return index<x.index;
}
}q[MAXN];
int ans[MAXN]; //离线操作之保存答案 int find(int x)
{
if(fa[x]==-) return x;
int pre = find(fa[x]);
r[x][] += r[fa[x]][]; //累积相对横坐标
r[x][] += r[fa[x]][]; //累积相对纵坐标
return fa[x] = pre;
} int Union(int u, int v, int w, char dir) //当dir为0时, 代表着查询
{
//以下为v相对于u的位置
int xx = , yy = ;
if(dir=='E') xx = w; if(dir=='W') xx = -w;
if(dir=='N') yy = w; if(dir=='S') yy = -w; int fu = find(u);
int fv = find(v);
if(fu==fv)
return abs(r[u][]-r[v][]) + abs(r[u][]-r[v][]);
if(dir==) return -; //如果是查询操作,并且两者不在同一集合,则直接返回-1; fa[fv] = fu;
r[fv][] = -r[v][]+xx+r[u][];
r[fv][] = -r[v][]+yy+r[u][];
return -;
} int main()
{
scanf("%d%d", &n, &m);
memset(fa, -, sizeof(fa));
memset(r, , sizeof()); for(int i = ; i<=m; i++)
scanf("%d%d%d%s", &a[i].u, &a[i].v, &a[i].w, a[i].dir); scanf("%d", &k);
for(int i = ; i<=k; i++)
scanf("%d%d%d", &q[i].u, &q[i].v, &q[i].index), q[i].id = i;
sort(q+, q++k); //对查询进行排序 int t = ;
for(int i = ; i<=m; i++)
{
Union(a[i].u, a[i].v, a[i].w, a[i].dir[]); //合并u 、v
while(q[t].index==i) // 是 while 不是 if !!因为有可能多个询问都在同一个下标。
{
ans[q[t].id] = Union(q[t].u, q[t].v, , );
if(++t>k) break;
}
} for(int i = ; i<=k; i++)
printf("%d\n", ans[i]);
}
POJ1984 Navigation Nightmare —— 种类并查集的更多相关文章
- POJ - 1984 Navigation Nightmare 种类并查集
思路:记录每个点与其根结点的横向距离和纵向距离,当知道其父节点与根结点的关系,很容易推出当前节点与根结点的关系: 直接相加即可. int p = a[x].par; a[x].dx += a[p].d ...
- POJ_1984 Navigation Nightmare 【并查集】
一.题面 POJ1984 二.分析 这题还是比较有意思的一题. 首先需要清楚的是,这题与普通并查集的区别在于它的节点之间的权值是二维的,因为是曼哈顿距离,肯定不能直接存距离,这样将不利于后面的路径压缩 ...
- [POJ1984]Navigation Nightmare
[POJ1984]Navigation Nightmare 试题描述 Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
- NOI2001|POJ1182食物链[种类并查集 向量]
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65430 Accepted: 19283 Description ...
- NOIP2010关押罪犯[并查集|二分答案+二分图染色 | 种类并查集]
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- poj1417(种类并查集+dp)
题目:http://poj.org/problem?id=1417 题意:输入三个数m, p, q 分别表示接下来的输入行数,天使数目,恶魔数目: 接下来m行输入形如x, y, ch,ch为yes表示 ...
- poj1733(种类并查集+离散化)
题目链接: http://poj.org/problem?id=1733 题意: 输入n表示有一个长度为n的0,1字符串, m表示接下来有m行输入, 接下来的m行输入中x, y, even表示第x到第 ...
- poj 1182:食物链(种类并查集,食物链问题)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44168 Accepted: 12878 Description ...
随机推荐
- Mongodb 断电或者强制关机之后
Mongodb相信大家都比较熟悉了,将它注册为服务什么的就不说了,网上到处都是.在公司用的过程中,我发现在意外断电,或者强制关机之后,启动服务时候就会报错,找了很久,试了很多种方法,才发现,它有个自带 ...
- 0元免费领《JAVA日志》教程,天啦噜!
天啦,老码疯了!辛辛苦苦,费心费力准备的<java日志实战及解析>教程真的不要钱了吗? 作为添物网的小编,每天看着老码为了给大家录制课程,加班加点的做课件,为了保证课程的质量,老码一遍又一 ...
- ZOJ - 3781 Paint the Grid Reloaded 题解
题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...
- Codeforces Round #295 D. Cubes [贪心 set map]
传送门 D. Cubes time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- webstorm初始化
1.皮肤设置,重启后Terminal皮肤生效 2.排除目录 2.1全局排除 2.2局部排除 选中文件夹 右击Make Directroy As 选择 Excluded 3.代码自定义 3.1 cons ...
- [Poj2411]Mondriaan's Dream(状压dp)(插头dp)
Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 18096 Accepted: 103 ...
- ATcoder 1983 BBQ Hard
E - BBQ Hard Time limit : 2sec / Memory limit : 256MB Score : 1400 points Problem Statement Snuke is ...
- OSGI是什么
OSGI(Open Services Gateway Initiative),或者通俗点说JAVA动态模块系统,定义了一套模块应用开发的框架.OSGI容器实现方案如Knopflerfish, Equi ...
- 【.Net Core 学习系列】-- EF Core实践(DB First)
一.开发环境: VS2015, .Net Core 1.0.0-preview2-003156 二.准备数据: CREATE DATABASE [Blogging]; GO USE [Blogging ...
- oracle字段的所有类型
字段类型 中文说明 限制条件 其它说明 CHAR 固定长度字符串 最大长度2000 bytes VARCHAR2 可变长度的字符串 最大长度4000 ...