POJ1984 Navigation Nightmare (hard?or!hard?)

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".

题目大意:在1。。。m的时间里,每秒给两点、两点间的路长、路的东西南北。之后的k行中给出了k个询问,相应询问给出了两个节点和询问时间(不一定按升序排列),如果能到,则输出曼哈顿距离,否则输出-1。

思路:离线操作,对于访问时间排升序(置一个top指针,跟着边走),最后输出的时候注意按输入顺序输出就好了。对点进行并查集,但是tot数组要保存横向和纵向的位移(有正负)。规定向南和向东为正方向,有点奇怪,不过画到图上好像好看一点。。。然后就是对于合并时的结点更新。。。
                if (b[i].di==‘E‘||b[i].di==‘W‘)

{

sum[r2][0]=sum[b[i].st][0]-sum[b[i].en][0]-b[i].va;

sum[r2][1]=sum[b[i].st][1]-sum[b[i].en][1];

}

else

{

sum[r2][0]=sum[b[i].st][0]-sum[b[i].en][0];

sum[r2][1]=sum[b[i].st][1]-sum[b[i].en][1]-b[i].va;

}

fa[r2]=r1;

code:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

using namespace std;

struct use{

int st,en,ti,yu;

}a[40001];

struct user{

int st,en,va;

char di;

}b[40001];

int fa[40001]={0},sum[40001][2]={0},ans[40001]={0};

int my_comp(const use &x,const use &y)

{

if (x.ti<y.ti) return 1;

else return 0;

}

int rool(int x)

{

int j;

if (fa[x]!=x)

{

j=rool(fa[x]);

sum[x][0]=sum[x][0]+sum[fa[x]][0];

sum[x][1]=sum[x][1]+sum[fa[x]][1];

fa[x]=j;

}

return fa[x];

}

int main()

{

int i,j,n,m,k,r1,r2,t,top;

char ch;

scanf("%d%d",&n,&m);

for (i=1;i<=m;++i)

{

scanf("%d%d%d",&b[i].st,&b[i].en,&b[i].va);

cin>>b[i].di;

if (b[i].di==‘E‘||b[i].di==‘N‘)

b[i].va=-1*b[i].va;

}

scanf("%d",&k);

for (i=1;i<=k;++i)

{

scanf("%d%d%d",&a[i].st,&a[i].en,&a[i].ti);

a[i].yu=i;

}

sort(a+1,a+k+1,my_comp);

top=1;

for (i=1;i<=n;++i)

fa[i]=i;

for (i=1;i<=m;++i)

{

r1=rool(b[i].st);

r2=rool(b[i].en);

if (r1!=r2)

{

if (b[i].di==‘E‘||b[i].di==‘W‘)

{

sum[r2][0]=sum[b[i].st][0]-sum[b[i].en][0]-b[i].va;

sum[r2][1]=sum[b[i].st][1]-sum[b[i].en][1];

}

else

{

sum[r2][0]=sum[b[i].st][0]-sum[b[i].en][0];

sum[r2][1]=sum[b[i].st][1]-sum[b[i].en][1]-b[i].va;

}

fa[r2]=r1;

}

if (top>k) break;

while (a[top].ti==i&&top<=k)

{

r1=rool(a[top].st);

r2=rool(a[top].en);

if (r1!=r2)

ans[a[top].yu]=-1;

else

{

ans[a[top].yu]=abs(sum[a[top].st][0]-sum[a[top].en][0])

+abs(sum[a[top].st][1]-sum[a[top].en][1]);

}

++top;

}

}

for (i=1;i<=k;++i)

printf("%d\n",ans[i]);

}

时间: 2024-10-13 23:26:10

POJ1984 Navigation Nightmare (hard?or!hard?)的相关文章

POJ 1984 Navigation Nightmare (数据结构-并查集)

Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1615 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series o

HDU 3085 Nightmare Ⅱ (双向广搜)

题意:有M,G两人和鬼魂(Z)在n*m的方格内,M每秒走3步,G每秒走一步,鬼魂每秒走2步,问是否能 不遇到鬼魂下两人相遇,鬼魂可以穿墙(X),人不可以.初始鬼魂有2个. #include<stdio.h> #include<string.h> #include<string> #include<queue> #include<map> #include<iostream> #include<algorithm> #def

[SCU 4498] RunningPhoton&#39;s Nightmare (BFS预处理+SPFA)

SCU - 4498 给定一张网格图,其中有一些不可到达点和一些时间重置装置 RunningPhoton从起点出发,身上有一个定时炸弹,当时间置0时他就会死 但是在置0前碰到时间重置装置又能重置时间 问 RunningPhoton是否能到达终点 若能,则输出最短时间,若不能,则输出 "Poor RunningPhoton" 这题虽然地图是有 600*600,但是有不超过 150个重置装置 普通 bfs搜的话肯定爆炸,因为你要存每个装置是否被用过了 正确解法如下: 因为我们只关心重置装置

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

Navigation Nightmare 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 <= 100

poj 1984 Navigation Nightmare(带权并查集+小小的技巧)

题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如果不存在则输出-1 其实就是将权值分一下x,y,x表示x轴方向的权值,y表示y轴方向的权值.然后最后询问时稍微有点技巧 可以先记录一下每次询问的位置然后再按照时间点从小到大来排序最后这样就方便并查集了. #include <iostream> #include <cstring> #i

POJ1984 Navigation Nightmare

Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 6219   Accepted: 2230 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 4

6、Struts2中的国际化、Ognl(对象图导航语言)表达式语言

Struts2中的国际化.Ognl(对象图导航语言)表达式语言 回顾:Servlet 中国际化: 1). 写资源文件 基础名.properties [默认的语言环境的配置] 基础名语言简称国家简称.properties 2). 读取资源文件,再使用 程序:ResourceBundle Jsp: jstl提供的格式化与国际化标签库. 一.Struts2中国际化: 1. 写资源文件 (同servlet) 2. 读资源文件 程序:ResourceBundle (同servlet) JSP: 1)jst

微信小程序把玩(三十九)navigation API

原文:微信小程序把玩(三十九)navigation API 演示效果也看到了小程序也就提供这几个处理导航控制.值得注意的是只能同时导航五个页面 主要属性: 导航条一些方法 wx.setNavigationBarTitle(object) 设置导航条的Title 导航标题可以通过三种方式设置,第一种是通过全局配置名字统一,第二种就是在page中新建个json文件配置它会覆盖全局配置的title,第三种就是通过API设置. wx.showNavigationBarLoading()设置在导航条上显示

POJ-1986 Distance Queries(LCA、离线)

Distance Queries Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 14378   Accepted: 5062 Case Time Limit: 1000MS Description Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifesty