Navigation Nightmare

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 7871   Accepted: 2831
Case Time Limit: 1000MS

题目链接:http://poj.org/problem?id=1984

Description:

Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 1000) connect the farms. A map of these farms might look something like the illustration below in which farms are labeled F1..F7 for clarity and lengths between connected farms are shown as (n):

           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 1, FJ knows the distance between 1 and 6 is 13.
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.

题意:

给出n个农场,然后按时间依次给出m个关于农场相对位置的信息,之后会给出询问,问在t时刻,x到y的曼哈顿距离是多少。

题解:

这题一开始我以为会用一个时间数组来维护x到y的最大时间,然后直接在线询问进行判断,但发现后来行不通...

之后便发现把输入先储存起来进行离线操作就可以了,具体做法如下:

把询问按照时间从小到大排序,然后按时间对点进行合并,然后用带权并查集维护一下点的x,y值就好了。

更新x,y值可以采用向量法去思考,fx->fy = fx->x + x->y + y->fy ,注意有向性。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <vector>
using namespace std; typedef pair<int,int> pii;
const int N = , K = ;
int n,m,k;
int f[N];
struct query{
int p1,p2,t,id;
bool operator < (const query &A)const{
return A.t<t;
}
}q[K];
struct farm{
int X,Y;
}p[N];
struct link{
int x,y,dis;
char c;
}l[N];
int find(int x){
if(x==f[x]) return x;
int tmp=f[x];
f[x]=find(f[x]);
p[x].X+=p[tmp].X;
p[x].Y+=p[tmp].Y;
return f[x];
}
void Union(int x,int y,int dir,int d){
int fx=find(x),fy=find(y);
f[fx]=fy;
if(dir==) p[fx].X=p[y].X-d-p[x].X,p[fx].Y=p[y].Y-p[x].Y;//x在y的西面
else if(dir==) p[fx].X=p[y].X+d-p[x].X,p[fx].Y=p[y].Y-p[x].Y ;
else if(dir==) p[fx].Y=p[y].Y-d-p[x].Y,p[fx].X=p[y].X-p[x].X;//x在y的南面
else p[fx].Y=d-p[x].Y+p[y].Y,p[fx].X=p[y].X-p[x].X;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){scanf("%d%d%d %c",&l[i].x,&l[i].y,&l[i].dis,&l[i].c);}
scanf("%d",&k);
priority_queue <query> que;
for(int i=,x,y,t;i<=k;i++){
scanf("%d%d%d",&q[i].p1,&q[i].p2,&q[i].t);q[i].id=i;
que.push(q[i]);
}
priority_queue <pii ,vector<pii>,greater<pii> > ans ;
for(int i=;i<=N-;i++) f[i]=i;
for(int i=,x,y,dis,pd;i<=m;i++){
char c;
x=l[i].x;y=l[i].y;dis=l[i].dis;c=l[i].c;
if(c=='E') pd=;else if(c=='W') pd=;else if(c=='N') pd=;else pd=;
int fx=find(x),fy=find(y);
if(fx!=fy) Union(x,y,pd,dis);
while(que.top().t==i && !que.empty()){
query now=que.top();que.pop();
int now1=now.p1,now2=now.p2;
if(find(now1)==find(now2)){
int xx = abs(p[now1].X-p[now2].X),yy=abs(p[now1].Y-p[now2].Y);
ans.push(make_pair(now.id,xx+yy));
}else ans.push(make_pair(now.id,-));
}
}
while(!ans.empty()){
printf("%d\n",ans.top().second);
ans.pop();
}
return ;
}

POJ1984:Navigation Nightmare(带权并查集)的更多相关文章

  1. BZOJ 3362 Navigation Nightmare 带权并查集

    题目大意:给定一些点之间的位置关系,求两个点之间的曼哈顿距离 此题土豪题.只是POJ也有一道相同的题,能够刷一下 别被题目坑到了,这题不强制在线.把询问离线处理就可以 然后就是带权并查集的问题了.. ...

  2. POJ 1984 - Navigation Nightmare - [带权并查集]

    题目链接:http://poj.org/problem?id=1984 Time Limit: 2000MS Memory Limit: 30000K Case Time Limit: 1000MS ...

  3. POJ-1984-Navigation Nightmare+带权并查集(中级

    传送门:Navigation Nightmare 参考:1:https://www.cnblogs.com/huangfeihome/archive/2012/09/07/2675123.html 参 ...

  4. POJ 1984 Navigation Nightmare 带全并查集

    Navigation Nightmare   Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...

  5. 【POJ 1984】Navigation Nightmare(带权并查集)

    Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...

  6. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  7. 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦

    [bzoj]3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 ​ 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂 ...

  8. POJ 1984 Navigation Nightmare(二维带权并查集)

    题目链接:http://poj.org/problem?id=1984 题目大意:有n个点,在平面上位于坐标点上,给出m关系F1  F2  L  D ,表示点F1往D方向走L距离到点F2,然后给出一系 ...

  9. 【poj 1984】&【bzoj 3362】Navigation Nightmare(图论--带权并查集)

    题意:平面上给出N个点,知道M个关于点X在点Y的正东/西/南/北方向的距离.问在刚给出一定关系之后其中2点的曼哈顿距离((x1,y1)与(x2,y2):l x1-x2 l+l y1-y2 l),未知则 ...

随机推荐

  1. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  2. requests模块基础

    requests模块 .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { bor ...

  3. YSZOJ:#247. [福利]可持久化线段树 (最适合可持久化线段树入门)

    题目链接:https://syzoj.com/problem/247 解题心得: 可持久化线段树其实就是一个线段树功能的加强版,加强在哪里呢?那就是如果一颗普通的线段树多次修改之后还能知道最开始的线段 ...

  4. Android开发——Android手机屏幕适配方案总结

    )密度无关像素,单位为dp,是Android特有的单位 Android开发时通常使用dp而不是px单位设置图片大小,因为它可以保证在不同屏幕像素密度的设备上显示相同的效果. /** * dp与px的转 ...

  5. OpenCV代码提取:flip函数的实现

    OpenCV中实现图像翻转的函数flip,公式为: 目前fbc_cv库中也实现了flip函数,支持多通道,uchar和float两种数据类型,经测试,与OpenCV3.1结果完全一致. 实现代码fli ...

  6. P2347 砝码称重

    P2347 砝码称重 题目描述 设有1g.2g.3g.5g.10g.20g的砝码各若干枚(其总重<=1000), 输入输出格式 输入格式: 输入方式:a1 a2 a3 a4 a5 a6 (表示1 ...

  7. 3,SQL语句及数据库优化

       1,统一SQL语句的写法 对于以下两句SQL语句,程序员认为是相同的,数据库查询优化器认为是不同的. 所以封装成复用方法,用标准模板来控制. select*from dual select*Fr ...

  8. c++ list_iterator demo

    #include <iostream> #include <list> using namespace std; typedef list<int> Integer ...

  9. 小议Android多进程以致Application多次初始化

    最近遇到一个bug,当应用加了多进程后,比如总共进程数为N,会出现在`startService()`时`onStartCommand()`方法会被重复调用`(N-1)`次的奇怪现象. ***## 祸起 ...

  10. 《python核心编程第二版》第7章习题

    7–1. 字典方法.哪个字典方法可以用来把两个字典合并到一起? 答:dict1.update(dict2) 7–2. 字典的键.我们知道字典的值可以是任意的Python 对象,那字典的键又如何呢?请试 ...