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 ...
随机推荐
- 【02】使用Firebug查看和编辑HTML和CSS
使用Firebug查看和编辑HTML和CSS 描述 在本章节的教程中,我们将讨论如何使用Firebug查看和编辑HTML和CSS. 使用Firebug查看和编辑HTML 在你要查看的元素上右击鼠标然后 ...
- coraldraw快捷键
显示导航窗口(Navigator window) [N] 运行 Visual Basic 应用程序的编辑器 [Alt]+[F11] 保存当前的图形 [Ctrl]+[S] 打开编辑文本对话框 ...
- 三、Oracle常用内置函数
1. ASCII 返回与指定的字符对应的十进制数; SQL> select ascii(A) A,ascii(a) a,ascii(0) zero,ascii( ) space from d ...
- [BZOJ3052][UOJ#58][WC2013]糖果公园
[BZOJ3052][UOJ#58][WC2013]糖果公园 试题描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来 ...
- Android应用的权限配置和权限列表
权限配置写在Mainifest.xml文件中: <?xml version="1.0" encoding="utf-8"?> <manifes ...
- Session保存用户名到Session域对象中
Session保存用户名 1.构造登录界面 用户名: 密 码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <!DOCTYPE html> < ...
- BZOJ2038 (莫队)
BZOJ2038: 小Z的袜子 Problem : N只袜子排成一排,每次询问一个区间内的袜子种随机拿两只袜子颜色相同的概率. Solution : 莫队算法真的是简单易懂又暴力. 莫队算法用来离线处 ...
- apache cgi 模块安装
apache安装,请参照:http://httpd.apache.org/docs/2.4/ 安装系统: Fedora release 21 (Twenty One) (x64) 版本:Server ...
- Linux kernel 内核学习路线
看了下各位大神的推荐路线,总结如下: 0. 跟着项目走: 1. 学会用.熟练用linux系统: 2. Linux Kernel Development. 3. Understanding the Li ...
- codeforces 938F(dp+高维前缀和)
题意: 给一个长度为n的字符串,定义$k=\floor{log_2 n}$ 一共k轮操作,第i次操作要删除当前字符串恰好长度为$2^{i-1}$的子串 问最后剩余的字符串字典序最小是多少? 分析: 首 ...