POJ 1984 Navigation Nightmare(二维带权并查集)
题目链接:http://poj.org/problem?id=1984
题目大意:有n个点,在平面上位于坐标点上,给出m关系F1 F2 L D ,表示点F1往D方向走L距离到点F2,然后给出一系列询问F1 F2 I,表示在第I个关系给出后询问F1和F2两点间的曼哈顿距离,或者未知则输出-1。
解题思路:带权并查集,但是要开二维,val[][0]表示上下(南北)方向的偏移量,val[][1]表示左右(东西)方向的偏移量,然后一直更新就好,记得两个维度都要一起更新。
代码:
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5e4+;
int root[N],val[N][],res[N];//val[][]用来记录权值,val[][0]表示南北方向的偏移量,val[][1]表示东西方向的偏移量 struct node{
int a,b,len;
char det[];
}arr[N]; struct Node{
int a,b,id;
Node(){}
Node(int l,int r,int c){
a=l;
b=r;
id=c;
}
};
vector<Node>v[N]; int find(int x){
if(root[x]==x)
return x;
int tmp=find(root[x]);
val[x][]+=val[root[x]][];
val[x][]+=val[root[x]][];
return root[x]=tmp;
} int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
root[i]=i;
}
for(int i=;i<=m;i++){
scanf("%d%d%d%s",&arr[i].a,&arr[i].b,&arr[i].len,arr[i].det);
}
int q;
scanf("%d",&q);
for(int i=;i<=q;i++){
int a,b,num;
scanf("%d%d%d",&a,&b,&num);
v[num].push_back(Node(a,b,i));
} for(int i=;i<=m;i++){
int a=arr[i].a,b=arr[i].b,len=arr[i].len;
int t1=find(a);
int t2=find(b);
if(t1!=t2){
root[t2]=t1;
if(arr[i].det[]=='N'){
val[t2][]=val[a][]-val[b][]+len;//val[t2][0]+val[b][0]=val[a][0]+len即a点向北走len距离到b点
val[t2][]=val[a][]-val[b][];//东西方向没有发生偏移
}
if(arr[i].det[]=='S'){
val[t2][]=val[a][]-val[b][]-len;//val[t2][0]+val[b][0]=val[a][0]+len即a点向南走len距离到b点
val[t2][]=val[a][]-val[b][];
}
if(arr[i].det[]=='W'){
val[t2][]=val[a][]-val[b][]-len;//同理
val[t2][]=val[a][]-val[b][];
}
if(arr[i].det[]=='E'){
val[t2][]=val[a][]-val[b][]+len;
val[t2][]=val[a][]-val[b][];
}
}
for(int j=;j<v[i].size();j++){
a=v[i][j].a;
b=v[i][j].b;
t1=find(a);
t2=find(b);
if(t1!=t2)//两点不在同一个并查集中则两点间没有通路
res[v[i][j].id]=-;
else
res[v[i][j].id]=abs(val[a][]-val[b][])+abs(val[a][]-val[b][]);
}
}
for(int i=;i<=q;i++){
printf("%d\n",res[i]);
}
return ;
}
POJ 1984 Navigation Nightmare(二维带权并查集)的更多相关文章
- 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 2912] Rochambeau 解题报告 (带权并查集)
题目链接:http://poj.org/problem?id=2912 题目: 题目大意: n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000) 接下来m行形 ...
- poj 1733 Parity game【hash+带权并查集】
hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...
- POJ 2492 A Bug's Life 带权并查集
题意: 思路: mod2 意义下的带权并查集 如果两只虫子是异性恋,它们的距离应该是1. 如果两只虫子相恋且距离为零,则它们是同性恋. (出题人好猥琐啊) 注意: 不能输入一半就break出来.... ...
- 【poj 1182】食物链(图论--带权并查集)
题意:有3种动物A.B.C,形成一个"A吃B, B吃C,C吃A "的食物链.有一个人对N只这3类的动物有M种说法:第一种说法是"1 X Y",表示X和Y是同类. ...
- 【POJ 1988】 Cube Stacking (带权并查集)
Cube Stacking Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)id ...
- POJ - 3728:The merchant (Tarjan 带权并查集)
题意:给定一个N个节点的树,1<=N<=50000 每个节点都有一个权值,代表商品在这个节点的价格.商人从某个节点a移动到节点b,且只能购买并出售一次商品,问最多可以产生多大的利润. 思路 ...
- POJ 2492 A Bug's Life (带权并查集 && 向量偏移)
题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...
随机推荐
- BZOJ4566:[HAOI2016]找相同字符——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4566 https://www.luogu.org/problemnew/show/P3181 给定 ...
- BZOJ1499:[NOI2005]瑰丽华尔兹——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1499 舞厅是一个N行M列的矩阵,矩阵中的某些方格上堆放了一些家具,其他的则是空地.钢琴可以在空地上滑 ...
- [Leetcode] Maximum depth of binary tree二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- HTML5 canvas 创意:飞翔的凤凰
当我看到这件作品的时候,我表示非常喜欢.这个作品的产生不仅仅需要编程和算法,作者肯定是个充满了艺术细胞的人.倘若有什么canvas艺术作品比赛的话,我想它就是获奖的那个. 先观赏下演示吧.注意,要看到 ...
- getopt和getopt_long参数处理
1:getopt函数 getopt主要用于解析程序运行时所带的参数,原型如下: #include <unistd.h> int getopt(int argc, char * const ...
- 元类编程-- 实现orm,以django Model为例
# 需求 import numbers class Field: pass class IntField(Field): # 数据描述符 def __init__(self, db_column, m ...
- 初识Webx 1
Webx是一套基于Java Servlet API的通用Web框架.它在Alibaba集团内部被广泛使用.从2010年底,向社会开放源码. Webx框架是一个稳定.强大的Web框架.建立在Spring ...
- (转)Linux下使Shell 命令脱离终端在后台运行
转自: http://www.linuxidc.com/Linux/2011-05/35723.htm 方法如下: (1)输入命令: nohup 你的shell命令 & (2)回车,使终端回到 ...
- MVP应用在android app上
使用MVP模式来解耦activity中业务代码和界面代码.在activity中,将其中的业务抽象到presenter层:将其中的界面代码抽象到View层. MVP模式: 一个软件被划分成三层,View ...
- Codeforces 321E Ciel and Gondolas
传送门:http://codeforces.com/problemset/problem/321/E [题解] 首先有一个$O(n^2k)$的dp. # include <stdio.h> ...