Navigation Nightmare---poj1984(多关系并查集)
题目链接:http://poj.org/problem?id=1984
给定n个城市,m条边告诉你城市间的相对距离,接下来q组询问,问你在第几条边添加后两城市的距离。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define PI 4*atan(1.0)
#define N 42000
#define met(a, b) memset(a, b, sizeof(a)) int f[N], rx[N], ry[N], ans[N];
///rx[i]表示i和f[i]的x的偏移量,ry[i]是y的偏移量;
struct node
{
int u, v, d, I, Id;
char dir[];
}a[N], q[N]; int cmp(node p, node q)
{
return p.Id<q.Id;
} int Find(int x)
{
int k = f[x];
if(x!=f[x])
{
f[x] = Find(f[x]);
rx[x] += rx[k];
ry[x] += ry[k];
}
return f[x];
} void Union(int num)
{
int x = a[num].u, y = a[num].v;
char ch = a[num].dir[]; int px = Find(x), py = Find(y); if(px != py)
{
f[px] = py; rx[px] = rx[y] - rx[x];
ry[px] = ry[y] - ry[x]; if(ch == 'E')
rx[px] -= a[num].d ;
if(ch == 'W')
rx[px] += a[num].d;
if(ch == 'N')
ry[px] -= a[num].d;
if(ch == 'S')
ry[px] += a[num].d;
}
}
int main()
{
int n, m, Q;
while(scanf("%d %d", &n, &m)!=EOF)
{
met(a, );met(q, ); met(ans, );
for(int i=; i<=n; i++)
f[i] = i, rx[i] = ry[i] = ; for(int i=; i<=m; i++)
scanf("%d %d %d %s", &a[i].u, &a[i].v, &a[i].d, a[i].dir); scanf("%d", &Q);
for(int i=; i<=Q; i++)
{
scanf("%d %d %d", &q[i].u, &q[i].v, &q[i].I);
q[i].Id = i;
} sort(q+, q+Q+, cmp);///不排序也可以;因为是按I的顺序给的;
for(int pre=, i=; i<=Q; i++)
{
for(int j=pre+; j<=q[i].I; j++)
Union(j); pre = q[i].I;
int u = q[i].u, v = q[i].v; int px = Find(u);
int py = Find(v); if(px != py) ans[q[i].Id] = -;
else ans[q[i].Id] = abs(rx[u]-rx[v]) + abs(ry[u]-ry[v]);
}
for(int i=; i<=Q; i++)
printf("%d\n", ans[i]);
}
return ;
}
Navigation Nightmare---poj1984(多关系并查集)的更多相关文章
- BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集
BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集 Description 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M( ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- poj 1984 Navigation Nightmare(带权并查集+小小的技巧)
题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如 ...
- 又见关系并查集 以POJ 1182 食物链为例
简单的关系并查集一般非常easy依据给出的关系搞出一个有向的环,那么两者之间的关系就变成了两者之间的距离. 对于此题: 若u.v不在一个集合内,则显然此条语句会合法(暂且忽略后两条.下同). 那么将f ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
- poj 1182 食物链(关系并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62824 Accepted: 18432 Description ...
- Find them, Catch them(POJ 1703 关系并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38668 Accepted: ...
随机推荐
- chrome 版本升级到56,报错Your connection is not private NET::ERR_CERT_WEAK_SIGNATURE_ALGORITHM
ubuntu14.04 解决方法: $ sudo apt-get install libnss3-1d ref: http://stackoverflow.com/questions/42085055 ...
- CentOS查看操作系统信息(重要)
1.查看物理CPU的个数 [root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc ...
- MVC+LINQToSQL的Repository模式之(二)数据基类
namespace Data.TEST{ /// <summary> /// 数据操作基类 /// </summary> public abstract ...
- C# Asp.net 制作一个windows服务
那下面就来说说如何制作一个服务来 实现开机自动启动,每隔一段时间向student表中插入数据. 步骤: 1) 新建项目 ---> Windows 服务 2) 拖放Times控件 工具箱中 ...
- Unity文件操作路径
Unity3D中的资源路径: Application.dataPath:此属性用于返回程序的数据文件所在文件夹的路径.例如在Editor中就是Assets了. Application.streamin ...
- 编写Unity3D着色器的三种方式
不管你会不会写Unity3D的shader,估计你会知道,Unity3D编写shader有三种方式,这篇东西主要就是说一下这三种东西有什么区别,和大概是怎样用的. 先来列一下这三种方式: fixed ...
- HTML5 ShadowDOM & CustomElements
Web组件由四部分组成 Template Shadow DOM (Chrome Opera支持) Custom Elements Packaging Shadow DOM 组成 Shadow DOM可 ...
- MYSQL系列之(二)
上一篇文章讲的是mysql的基本操作,这一篇会有一点难以理解,本节主要内容mysql视图,存储过程,函数,事务,触发器,以及动态执行sql 视图view 视图是一个虚拟表,其内容由查询定义.同真实的表 ...
- poj_1204 Trie图
题目大意 给出一个RxC的字符组成的puzzle,中间可以从左向右,从右到左,从上到下,从下到上,从左上到右下,从右下到左上,从左下到右上,从右上到左下,八个方向进行查找字符串. 给出M个字符 ...
- java基础---->Java中枚举的使用(一)
这里介绍一下java中关于枚举的使用. java中枚举的使用 一.枚举中可以定义方法 参照于TimeUnit的使用,TimeUnit.MILLISECONDS.sleep(1000); LoveUti ...