POJ1984 Navigation Nightmare (Hard?or!hard?)

Source: Internet
Author: User

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



Topic: in 1 ... M time, each second to two points, two points between the road length, road East. K is given in the following K-line query, the corresponding query gives two nodes and the query time (not necessarily in ascending order), if it can, then output the Manhattan distance, otherwise output-1.

Idea: Offline operation, for access time in ascending order (set a top pointer, followed by the side walk), the final output when the attention of the input sequence output is good. The points are checked and set, but the tot array is to preserve the horizontal and vertical displacements (positive or negative). It's a little strange to have a positive direction to and from the east, but it looks a little nicer on the picture ... Then there is the node update for the merge ...
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 &AMP;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)     {      & Nbsp;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]);           }    & nbsp       ++top;}    }    for (i=1;i<=k;++i)       printf ("%d\n", Ans[i]);}  

POJ1984 Navigation Nightmare (hard?or!hard?)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.