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 ...
随机推荐
- String字符串的遍历
StringTest.java /* * 变量字符串(获取字符串中的每一个字符) */ public class StringTest { public static void main(String ...
- Boolean.valueOf("true")的用法
Boolean.valueOf(a);a为true时返回true不管大小写,a为其他值时都返回false:
- Leetcode 283.移动零
移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组 ...
- Coloring Brackets (区间DP)
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a soluti ...
- 【Ajax 4】Ajax、JavaScript和JQuery的联系和区别
导读:在之前,就分别学习了Ajax.JavaScript和JQuery,然后对于这三者之间的关系,是一直云里雾里的.尤其是后来学到了Ajax,就更是不明白了.现在,就给总结总结. 一.基本概述 1.1 ...
- Linux Awk使用案例总结
知识点: 1)数组 数组是用来存储一系列值的变量,可通过索引来访问数组的值. Awk中数组称为关联数组,因为它的下标(索引)可以是数字也可以是字符串. 下标通常称为键,数组元素的键和值存储在Awk程序 ...
- JPos学习
基于JPos的消息交换系统 消息交换系统需求解读 消息交换系统不不是一个具体的业务系统,而是业务系统的运转的基础框架: 他的运转是体现在报文交换上的: 要定义一个可被不同业务系统使用的报文规范: 报文 ...
- hdu 5188 zhx and contest [ 排序 + 背包 ]
传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- php的抽象类
php的抽象类 //定义一个老虎类 abstract class Tiger{ public abstract function climb(); } //定义一个孟加拉虎类 class MTiger ...
- union关键字和字节大小端序的确定
union 关键字的用法与struct 的用法非常类似. union 维护足够的空间来置放多个数据成员中的“一种”,而不是为每一个数据成员配置空间,在union 中所有的数据成员共用一个空间,同一时间 ...