Navigation Nightmare POJ - 1984 带权并查集
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=4e4+;
// 东西 南北
int p[N],D[N],B[N];
struct node{
int x,y,d;
char op[];
}e[N];
void init()
{
for(int i = ; i < N ; i ++)
{
p[i] =i;
D[i] =;
B[i] =;
e[i].x=,e[i].y=,e[i].d=;
}
}
int find(int x)
{
if(p[x]!=x)
{
int root=find(p[x]);
D[x]+=D[p[x]];
B[x]+=B[p[x]];
p[x]=root;
}
return p[x];
}
void build(int x,int y,int d,char opr[])
{
int dx=find(x);
int dy=find(y);
if(dx!=dy)
{
p[dy]=dx;
//这里虽然是在东西方向,但是南北方向的权值数组也是要更新的因为a在b的正西方向d米
//其实是a在b东西方向,向西移动d米加上a在b南北方向上移动0米
if(opr[]=='E'||opr[]=='W')
{
//如果和正方向相同就是加上这个距离,规定东为正方向
if(opr[]=='E')
D[dy]=D[x]-D[y]+d;
if(opr[]=='W')
D[dy]=D[x]-D[y]-d;
B[dy]=B[x]-B[y];//什么都不加
}
else if(opr[]=='N'||opr[]=='S')
{
if(opr[]=='N')
B[dy]=B[x]-B[y]+d;
if(opr[]=='S')
B[dy]=B[x]-B[y]-d;
D[dy]=D[x]-D[y];
}
}
}
int main()
{
int n,m;
while(cin>>n>>m)
{
init();
for(int i=;i<=m;i++)
cin>>e[i].x>>e[i].y>>e[i].d>>e[i].op;
int t;
cin>>t;
int cnt=;
//c其实就是按照从小到大的方式输入
//所以我们可以边输入边建立
int ans;
for(int i=;i<t;i++)
{
int a,b,c;
cin>>a>>b>>c;
//在c之前的我们都给他建立了
while(cnt<=c)
{
build(e[cnt].x,e[cnt].y,e[cnt].d,e[cnt].op);
cnt++;
}
int da=find(a);
int db=find(b);
if(da!=db)
ans=-;
else
ans=abs(D[a]-D[b])+abs(B[a]-B[b]);
cout<<ans<<endl;
}
}
return ;
}
Navigation Nightmare POJ - 1984 带权并查集的更多相关文章
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)
题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...
- poj 1182 (带权并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 71361 Accepted: 21131 Description ...
- poj 1733(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 思路:这题一看就想到要用并查集做了,不过一看数据这么大,感觉有点棘手,其实,我们仔细一想可以发现,我们需要记录的是出现过的节点到 ...
- POJ 1703 带权并查集
直接解释输入了: 第一行cases. 然后是n和m代表有n个人,m个操作 给你两个空的集合 每个操作后面跟着俩数 D操作是说这俩数不在一个集合里. A操作问这俩数什么关系 不能确定:输出Not sur ...
- Parity game POJ - 1733 带权并查集
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; <& ...
- 【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个点,在平面上位于坐标点上,给出m关系F1 F2 L D ,表示点F1往D方向走L距离到点F2,然后给出一系 ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
随机推荐
- 小白学 Python 数据分析(6):Pandas (五)基础操作(2)数据选择
人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...
- openssl 自签名证书SHA1加密算法
openssl genrsa -out privkey.pem 2048 openssl req -new -key privkey.pem -sha1 -out cert.csr openssl r ...
- k8s系列---部署集群
docer启动出错 [root@centos-minion yum.repos.d]# systemctl start docker Job for docker.service failed bec ...
- Bookshelf 2 01背包
B - Bookshelf 2 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submi ...
- Arm开发板+Qt学习之路-析构函数和对话框一起时
先记录一下代码 一:先将指针释放掉,在显示对话框 void MainWindow::canResponseError(SendCanMsgThread *sendCanMsgThread ){ std ...
- JVM垃圾回收——GC
一.JVM内存分配与回收 下图为堆内存结构图(注意:元数据区(MetaData )实际上不属于堆): 1.对象优先在Eden区分配 大多数情况下,对象在新生代中Eden区分配.当Eden区没有足够空间 ...
- 回到未来:Smalltalk 编程系统
Smalltalk 是19世纪70年代由 Alan Kay 设计的,第一个以面向对象(Object-Orientation)为主要范式的编程语言 1.Smalltalk 具有大量首创的特性,深刻影响了 ...
- android的APT技术
转载请标明出处:https:////www.cnblogs.com/tangZH/p/12343786.html, http://77blog ...
- MySql学习-1.MySql的安装:
1.安装包的下载(mysql-v5.7.25 )(NavicatforMySQL_11.2.15): 链接:https://pan.baidu.com/s/166hyyYd3DMjYhMwdW805F ...
- 01-Flink运行架构
1.flink运行时的组件 Flink 运行时架构主要包括四个不同的组件,它们会在运行流处理应用程序时协同工作: 作业管理器(JobManager).资源管理器(ResourceManager). ...