Navigation Nightmare
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 6962 | Accepted: 2489 | |
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 < 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):
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".
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.
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.
Source
/*
题目大意:有N个农场由M条南北东西方向的道路连接
主人公吧农场和道路的地图给弄丢了,去计算机中搜索答案
计算机中的信息是这样的,A,B村庄由南北相连的长度为X的路相连.
主人公就这样一条一条的看这样的信息
有人在他看的时候问他问题,X,Y村庄的距离为多少.
主人公可能知道,亦可能不知道,知道就输出X,Y的曼哈东距离,不知道,输出-1
解题思路:
对询问的时间点进行排序
一遍建立地图一遍回答相应的询问
首先只有已经通过道路相连的两个点,我们可以算出他们的相对为之类.
由此我们可以看出大概是要用到并查集
并查集中我们将同一个集合中相邻的节点连上一条边(其实是指针指向和他相连的节点)
我们可以定义一个关系数组,表示这个节点和与他相邻的几点的关系
这样我们可以通过并查集的Pre数组,走到点X所在集合的根上,同时在这个过程中算出X和他的根的关系
同理我们可以算出Y和他的根的关系,如果XY的根不相同那么XY的位置关系是未知的,如果XY的跟相同,
可以根据XY和根的相对关系,算出XY的相对关系
*/
#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<set>
#include<cstdlib>
#include<stack>
typedef long long LL;
const int maxsize = 1e6;
using namespace std;
int Pre[maxsize];//指向他的上一个节点
int Rank[maxsize];//用于连接的时候使得并查集不至于连接成一条链而是连成一棵对称的树一样
int D[maxsize][2];//表示这个节点在他的Pre[x]节点的北面多少,东面多少,可以是负的值
void init(int n)//并查集初始化
{
for(int i=0;i<=n;i++)
{
Pre[i] = i;//自己指向自己,自己就是一个集合
Rank[i] = 0;//树的高度
D[i][0] = D[i][1] = 0;
}
}
int find(int x,int val[2])//val[]表示x在根的北面多少,东面多少
{
int root = x;
val[0] = val[1] = 0;
while(root!=Pre[root])//并非是根
{
val[0]+=D[root][0];
val[1]+=D[root][1];
root = Pre[root];
}
//同时进行路径的压缩,以减少后面查询的复杂度
int prela[2] = {0};//当前节点和上一个结点的关系
int pval[2] = {val[0],val[1]};//上一个几点和根的关系
while(x!=Pre[x])
{
//因为在路径压缩的时候,节点的P
//re所指向的节点变成他们的root了,所以D数组的内容要做出相应的变化
//通过上一个几点和根的关系和这个节点与上一个节点和根的关系,计算这个节点和根的关系
int t[2] = {D[x][0],D[x][1]};
pval[0]=D[x][0] = pval[0] - prela[0];
pval[1]=D[x][1] = pval[1] - prela[1];
prela[0] = t[0];
prela[1] = t[1];
int cx = x;
x = Pre[x];
Pre[cx] = root;//注意处理的先后顺序
}
return root;
}
void unit(int root1,int root2,int val[2])//根据Rank合并root1,root2所在集合,并建立关系,
{//val中给的值默认是root2链接向root1,既root2在root1的北面东面多少
if(root1!=root2)
{
if(Rank[root1]>Rank[root2])//root1的高度高,将root2链接到root1
{
Pre[root2] = root1;
D[root2][0] = val[0];
D[root2][1] = val[1];
}
else
{
Pre[root1] = root2;
D[root1][0] = -val[0];
D[root1][1] = -val[1];
if(Rank[root1]==Rank[root2])
Rank[root2]++;
}
}
}
int To[500];
int MArr[maxsize][4];//起点终点和起点相对终点的相对关系
int BArr[maxsize][3];
int Index[maxsize];
bool cmp(int a,int b)
{
return BArr[a][2]<BArr[b][2];
}
void workFun(int id)
{
int x = MArr[id][0];
int y = MArr[id][1];
int valx[2];
int valy[2];
int val[2];
val[0] = MArr[id][2],val[1] = MArr[id][3];//保存的是x相对于y的位置
int rootx = find(x,valx);
int rooty = find(y,valy);
int rval[2];
rval[0] = valx[0]-valy[0] - val[0];
rval[1] = valx[1]-valy[1] - val[1];
unit(rootx,rooty,rval);
}
int Ans[maxsize];
void ansFun(int from,int to,int id)
{
int valx[2];
int valy[2];
int root1 = find(from,valx);
int root2 = find(to,valy);
if(root1!=root2)
{
Ans[Index[id]] = -1;
}
else
{
int t = abs(valx[0]-valy[0])+abs(valx[1]-valy[1]);
Ans[Index[id]] = t;
}
}
int main()
{
//freopen("finput.txt","r",stdin);
int n,m;
To['N'] = 0;
To['S'] = 1;
To['E'] = 2;
To['W'] = 3;//北南东西的索引数组
while(~scanf("%d%d",&n,&m))
{
init(n);
//先将M存起来
int from,to,len;
char S[10];
for(int i = 0;i<m;i++)
{
scanf("%d%d%d%s",&from,&to,&len,S);
MArr[i][0] = from;
MArr[i][1] = to;
MArr[i][2] = MArr[i][3] = 0;
if(S[0]=='N')
MArr[i][2] = len;
else if(S[0]=='S')
MArr[i][2] = -len;
else if(S[0]=='E')
MArr[i][3] = len;
else
MArr[i][3] = -len;
}
int k;
scanf("%d",&k);
for(int i=0;i<k;i++)
{
scanf("%d%d%d",&BArr[i][0],&BArr[i][1],&BArr[i][2]);
Index[i] = i;//排序用的下标数组
}
sort(Index,Index+k,cmp);
int pM = 0,pB=0;
while(pM<m&&pB<k)
{
workFun(pM);
while(pM+1==BArr[Index[pB]][2])//要注意的一点,同一时间内可能有多个问题,同一时间内可能有多个问题,同一时间内可能有多个问题
{
ansFun(BArr[Index[pB]][0],BArr[Index[pB]][1],pB);
pB++;
}
pM++;
}
for(int i=0;i<k;i++)//将顺序更改回来再输出一遍
{
cout<<Ans[i]<<endl;
}
}
return 0;
}
数据:
5 4 1 3 5 E 1 2 5 S 1 5 5 W 1 4 5 N 3 2 5 4 2 4 4 3 4 4 答案是 10 10 10题目的数据,题目的数据挺水的
祝AC