Poj-1984-navigation nightmare+ with Right and check (intermediate

Source: Internet
Author: User

Portal: Navigation Nightmare

Reference: 1:https://www.cnblogs.com/huangfeihome/archive/2012/09/07/2675123.html

Reference: 2:http://blog.csdn.net/tc_to_top/article/details/43447727

Test instructions: Give all the information of a square-shaped city map first,

Then, in the case of only the first K information, ask the distance between a, if not connected, output-1;

Ideas:

First of all, each inquiry K is in ascending order (the discussion version of the god Bull said, there is a great meaning of the title number);

This problem let me have a new understanding of and check set: The root of the representative is very strong! And check the set if the change of a node will affect the whole and check all the nodes, then, in the Union_set only need to change the root node can be, of course, in the Find_set function to update all nodes (this is equivalent to a delay tag). We know that the Find_set function has gone two ways, one is the way to the root, one is the way back from the heel, and if the root is found to have been altered, all the nodes that pass through are updated on the way back from the root. This is achievable in the Find_set function.

//Sample Example7 61 6  -E6 3 9E3 5 7S4 1 3N2 4  -W4 7 2S31 6 11 4 32 6 6

Simulate the sample once, here is the coordinates after the completion of the Find (1-7)
t = 1:x[1] = 0, y[1] = 0
X[6] = y[6] = 0

t = 2:x[1] = 0, y[1] = 0
X[6] = y[6] = 0
X[3] = $, y[3] = 0

t = 3:x[1] = 0, y[1] = 0
X[6] = y[6] = 0
X[3] = $, y[3] = 0
X[5] = $, y[5] = 7

t = 4 (change origin):
X[4] = 0, y[4] = 0
X[1] = 0, y[1] = 3
X[6] = y[6] = 3
X[3] = $, y[3] = 3
X[5] = $, y[5] = 4

t = 5 (change origin):
X[2] = 0, y[2] = 0
X[4] = -20,y[4] = 0
X[1] = -20,y[1] = 3
X[6] = 7, y[6] = 3
X[3] = 2, y[3] = 3
X[5] = 2, y[5] = 4

t = 6:x[2] = 0, y[2] = 0
X[4] = -20,y[4] = 0
X[1] = -20,y[1] = 3
X[6] = 7, y[6] = 3
X[3] = 2, y[3] = 3
X[5] = 2, y[5] = 4
X[7] = -20,y[7] = 2

/*take right and check the set;*/#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>using namespacestd;Const intMAXN =40009;intN,M,FA[MAXN],S[MAXN],H[MAXN];structnode{intA,b,len; Charc[5];} MP[MAXN];voidinit () { for(intI=1; i<=n;i++) Fa[i]=i; memset (s),0,sizeof(s)); memset (H,0,sizeof(h));}intAbintXinty) {    if(x-y>0)returnX-y; Else returnY-x;}intFindintx) {    if(fa[x]==x)returnx; intTMP =Fa[x]; FA[X]=find (fa[x]); H[X]= H[x] + h[tmp];//Update child nodes on the way back from the rootS[X] = S[x] +S[tmp]; returnfa[x];}voidUni (Node T,intDxintdy) {    intPX =find (T.A); intPY =find (T.B); if(px==py)return; FA[PX]=py;//Random MergeH[PX] = h[t.b]-H[T.A] + dx;//for the moment, only the root is offsetS[PX] = s[t.b]-S[T.A] +dy;}intMain () {scanf ("%d%d",&n,&m);    Init ();  for(intI=0; i<m;i++) {scanf ("%d%d%d%s", &AMP;MP[I].A, &mp[i].b, &Mp[i].len, MP[I].C); }    intq,k=0; scanf ("%d",&q);  while(q--)    {        intl,r,e; scanf ("%d%d%d",&l,&r,&e);  for(inti=k;i<e;i++)         {             intdx=0, dy=0; Switch(mp[i].c[0])             {                  Case 'N':d Y+=mp[i].len; Break;  Case 'S':d Y-=mp[i].len; Break;  Case 'E':d X+=mp[i].len; Break;  Case 'W':d X-=mp[i].len; Break;         } uni (mp[i],dx,dy); } k=e; intPl=find (l), pr=find (R); if(pl!=PR) {Puts ("-1"); }         Else{printf ("%d\n", AB (H[l],h[r]) +AB (S[l],s[r])); }    }    return 0;}

Poj-1984-navigation nightmare+ with Right and check (intermediate

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.