Navigation Nightmare
Time Limit: 2000MS |
|
Memory Limit: 30000K |
Total Submissions: 5939 |
|
Accepted: 2102 |
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 <=) CO Nnect the farms. A Map of these farms might look something like the illustration below in which farms is labeled F1. F7 for clarity and lengths between connected Farms is shown as (n):
F1-------F6---(9)-----F3
| |
(3) |
| (7)
F4-----------F2 |
| |
(2) F5
Being an ASCII diagram, it's not precisely to scale, of course.
Each of the farm can connect directly to the most four of the farms via roads that leads exactly north, south, east, and/or west. Moreover, farms is only located at the endpoints of roads, and some farm can is found at every endpoint of every road. No roads Cross, and precisely one path
(Sequence of roads) links every pair of farms.
FJ lost his paper copy of the farm maps 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 ten 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's 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 has enough data yet). In the example above, the answer would is, since Bob wants to know the "Manhattan" distance between the pair of farms.
The Manhattan distance between, points (x1,y1) and (X2,y2) is just |x1-x2| + |y1-y2| (which is the distance a taxicab-a large city must travel-over-city streets in a-perfect grid to connect-X-y points ).
When Bob asks on a particular pair of farms, FJ might not yet has enough information to deduce the distance between th Em In the case, the 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
Farms connected by a road, L ' it length, and D is a
Character that's 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 <=), 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 is numbers of the farms in the query and I am 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 the 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 E6 3 9 E3 5 7 S4 1 3 N2 4 W4 7 2 S31 6 11 4 32 6 6
Sample Output
13-110
Hint
At time 1, FJ knows the distance between 1 and 6 are 13.
At time 3, the distance between 1 and 4 are still unknown. At the end, location 6 are 3 units West and 7 north of 2, so the distance is 10. Test instructions: There are n locations, M edges, for each edge, there is information X,y,len,d, where Len represents the length between x, Y, and D represents the position of y relative to X. Now there are Q queries, each with X,y,i, where I represents the state of the current 2 points when I complete the I route. Idea: First it is easy to think of this issue with the right to check the set of topics, because each point has a relationship. But the key is how to handle the position here. Now we use rel[x][0] to represent the X-point to the parent node of the tree where the horizontal axis of the vector, Rel[x][1] represents the X-point to the parent node of the tree, the coordinates of the vector, the rest of the operation is the common right and check set. Note, however, that the updated operation here (because it is a vector) and the x, y relationship of each edge (who is the parent node when updating).
#include <Set>#include<map>#include<queue>#include<stack>#include<cmath>#include<string>#include<time.h>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#defineINF 1000000001#definell Long Long#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd;Const intMAXN =40010;structnode{intx; inty; intZ; intID; Chars[Ten]; ll ans;} q[10010],A[MAXN];intpa[maxn],n,m,k,siz[maxn];ll rel[maxn][2];voidInit () { for(inti =0; I <= N; i++) {Pa[i]=i; Siz[i]=1; } memset (rel,0,sizeof(rel));}BOOLCMP1 (node Fa,node fb) {returnFa.z <fb.z;}BOOLCMP2 (node Fa,node fb) {returnFa.id <fb.id;}voidGETP (int& X,int& Y,node FP,intflag) { if(fp.s[0] =='E') {x=fp.z; Y=0; } Else if(fp.s[0] =='W') {x= -fp.z; Y=0; } Else if(fp.s[0] =='S') {x=0; Y=fp.z; } Else{x=0; Y= -fp.z; } if(flag) x *=-1, Y *=-1;}intFindintx) { if(X! =Pa[x]) { intFX =find (pa[x]); SIZ[FX]+=Siz[x]; rel[x][0] = rel[x][0] + rel[pa[x]][0]; rel[x][1] = rel[x][1] + rel[pa[x]][1]; PA[X]=FX; } returnpa[x];}intMain () { while(~SCANF ("%d%d",&n,&m)) {Init (); for(inti =0; I < m; i++) {scanf ("%d%d%d%s",&a[i].x,&a[i].y,&a[i].z,a[i].s); } scanf ("%d",&k); for(inti =0; I < K; i++) {q[i].id=i; scanf ("%d%d%d",&q[i].x,&q[i].y,&q[i].z); } sort (Q,q+K,CMP1); intp =0; for(inti =0; I < m; i++){ intx =a[i].x; inty =a[i].y; inttx =0, Ty =0; intFX =find (x); intFY =find (y); if(FX! =FY) { if(Siz[fx] >1) {GETP (tx,ty,a[i],0); SIZ[FX]+=Siz[fy]; PA[FY]=FX; rel[fy][0] =-rel[y][0]-TX + rel[x][0]; rel[fy][1] =-rel[y][1]-ty + rel[x][1]; } Else{GETP (tx,ty,a[i],1); SIZ[FY]+=SIZ[FX]; PA[FX]=fy; rel[fx][0] = rel[y][0]-tx-rel[x][0]; rel[fx][1] = rel[y][1]-ty-rel[x][1]; } } while(P < k && Q[p].z = = i +1) {x=q[p].x; Y=q[p].y; FX=find (x); FY=find (y); if(FX! =FY) {Q[p].ans= -1; } Else { //cout<<rel[x][0]<< ' <<rel[y][0]<< ' <<rel[x][1]<< ' <<rel[y][1]< <endl;Q[p].ans = Fabs (rel[x][0]-rel[y][0]) + fabs (rel[x][1]-rel[y][1]); } P++; }} sort (Q,q+K,CMP2); for(inti =0; I < K; i++) {printf ("%lld\n", Q[i].ans); } } return 0;}
poj1984 with Right and check set (vector processing)