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 <=) 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.
Source
Usaco 2004 February
"Test Instructions" The village where John is located can be seen as a two-dimensional plane, which has
NRanch, each ranch has its own coordinates, numbered 1.
To
N。 There are some roads between the pastures, each road connected to two different pastures, the direction must be parallel to the X-axis or the Y-axis. Xiang
There are not too many roads in the next place, and the path between the two ranches is unique.
All of a sudden, John's navigator failed, and the ranch's coordinate record disappeared. Fortunately, John found a way to express the
Data that can be used to learn about the relative position of the pasture. But Bessie has an emergency, and he needs to know when John is halfway to work.
Some of the distance between the pastures of Manhattan. At this point, John would tell Bessie if he could figure out the answer between the recovered road information. Please
You help John answer Bessie's question. (
x1
, y1) and (
x2
, y2) The distance between Manhattan is defined as
|
x1
?
x2
| +
|
y1
?
y2
|。
"Analysis" Mom, this is the more than half is engaged in this input, online on-line, should not be so disgusting!!! Drunk ~ ~ Ah, with the right and check set, is to restore and check set for a tree, and then change the FA when changing the good. This tree and the tree, of course, the less the number of the better. Weight I record is its horizontal and vertical axis and the difference between the father, change the father's time (that is, when abandoning the original father) to the father's weight added to his body and then modified. Then merge two trees, first put a point into that and check the tree with, in fact, do find it has put it in the root of the son, and then exchange the root is good, and then put the whole tree into the good. Well, I can have a yy for y out. The code length is absolutely because the input is too!! Nausea!! The code is as follows:
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <queue>7#include <cmath>8 using namespacestd;9 #defineMAXN 40010Ten One intFA[MAXN],NX[MAXN],NY[MAXN]; A - intMyabs (intx) {returnX>0? x:-x;} - the intFfind (intx) - { - inty=Fa[x]; - if(X!=fa[x]) fa[x]=Ffind (fa[x]); +nx[x]+=nx[y];ny[x]+=Ny[y]; - returnFa[x]; + } A at Chars[Ten]; - - structnode - { - intx,y,c,p; - intans; in}t[40010],tt[50010]; - to BOOLCMP (node X,node y) {returnx.c<y.c;} + BOOLCMP2 (node X,node y) {returnx.p<Y.P;} - the intMain () * { $ intn,m;Panax Notoginsengscanf"%d%d",&n,&m); - for(intI=1; i<=n;i++) fa[i]=i,nx[i]=ny[i]=0; the for(intI=1; i<=m;i++) + { A intx,y,c; thescanf"%d%d%d%s",&x,&y,&c,s); + -T[i].x=x;t[i].y=y;t[i].c=C; $ if(s[0]=='E') t[i].p=0; $ Else if(s[0]=='W') t[i].p=1; - Else if(s[0]=='N') t[i].p=2; - Elset[i].p=3; the } - intQ;Wuyiscanf"%d",&q); the for(intI=1; i<=q;i++) - { Wu intx,y,c; -scanf"%d%d%d",&x,&y,&c); AboutTt[i].x=x;tt[i].y=y;tt[i].c=C; $tt[i].p=i; - } -Sort (tt+1, tt+1+q,cmp); - intnow=0; A for(intI=1; i<=q;i++) + { the while(now<tt[i].c) - { $now++; the intX=t[now].x,y=t[now].y,c=t[now].c; the intHfSFfind (y); thenx[ff]=-nx[y];ny[ff]=-ny[y];fa[ff]=y; thenx[y]=ny[y]=0; fa[y]=x; - in if(t[now].p==0) nx[y]=C; the Else if(t[now].p==1) nx[y]=-C; the Else if(t[now].p==2) ny[y]=C; About Elseny[y]=-C; the } the the intx=tt[i].x,y=tt[i].y; + if(Ffind (x)!=ffind (y)) tt[i].ans=-1; - Else the {BayiTt[i].ans=myabs (Nx[x]-nx[y]) +myabs (ny[x]-ny[y]); the } the } -Sort (tt+1, tt+1+q,cmp2); - for(intI=1; i<=q;i++) printf ("%d\n", Tt[i].ans); the return 0; the}
[POJ 1984]
2016-10-27 18:26:37
"POJ 1984" Navigation Nightmare (with right and check set)