P3383: [Usaco2004 Open]cave cows 4 The four cows in the cave

Source: Internet
Author: User

This series is finally done, this is my first high efficiency to finish four questions, although there are two water problems, but the first and fourth is fine, but as long as you can think of ideas will soon be finished.

Like this question, just started thinking can you use DP? But do not know how to implement, and then think, so many points, a bit like the shortest path, but there are a total of 50,000 points, the number of sides may be n*n it?

So I took a pen to forget, found that even the words, first by the x-axis from small to large sort of again, and then looking back x than the current point of a large <=2 x, and then by comparing the relationship between Y, as long as the difference does not exceed 2 to connect a two-way edge, so that the point behind it does not need to look *25000 and timeout? I also thought about it, but found it impossible, because a point can connect up to 24 sides, the child can find up to 14 points, that is, time is 50000*14 and in fact, this is simply impossible to achieve. So we can do the SPFA after the good side.

1 Constmaxn=50001;2 type3Node=Record4 F,t:longint;5 end;6position=Record7 X,y:longint;8 end;9 varN,t,i,j,num,now,ans:longint;Tenb:array[0..750000] of node; Oned,head:array[0..50001] of Longint; Apos:array[0..50001] of position; -p:array[0..50001] of Boolean; -f:array[0..5000000] of Longint; the procedure Qs (t,w:longint); - varL,r,mid:longint; - tem:position; - begin +l:=t; R:=w; mid:=pos[(l+r) shr1].x; - Repeat + begin A        whilePos[l].x<mid DoInc (L); at        whilePos[r].x>mid DoDec (r); -       ifl<=R Then - begin -tem:=Pos[l]; -pos[l]:=Pos[r]; -pos[r]:=tem; in Inc (L); - Dec (r); to end; + end; -Until l>R; the   ift<R then QS (T,R); *   ifl<W then QS (l,w); $ end;Panax Notoginseng procedure SPFA; - varL,r,now,nowe:longint; the begin +Fillchar (P,sizeof(p),true); A    forl:=1to n Dod[l]:=MAXN; thel:=1; r:=1; f[1]:=0; d[0]:=0; p[0]:=false; +    whileL<=r Do - begin $now:=F[l]; $nowe:=Head[now]; -        whileNowe<>0  Do - begin the            ifd[now]+1<d[b[nowe].t] Then - beginWuyid[b[nowe].t]:=d[now]+1; the                ifp[b[nowe].t] Then - begin Wup[b[nowe].t]:=false; - Inc (R); Aboutf[r]:=b[nowe].t; $ end; - end; -nowe:=b[nowe].f; - end; A Inc (L); +p[now]:=true; the end; - end; $ procedure Insert (f,t:longint); the begin the Inc. (NUM); theb[num].f:=Head[f]; theb[num].t:=T; -head[f]:=num; in end; the begin the readln (n,t); About    fori:=1to n Do theWith Pos[i] Doreadln (x, y); theQs1, n); thepos[0].x:=0; pos[0].y:=0; +    fori:=0to n1  Do - begin thenow:=i+1;Bayi        while(pos[now].x-pos[i].x<=2) and (Now<=n) Do the begin the           ifABS (POS[NOW].Y-POS[I].Y) <=2 Then - begin - Insert (i,now); the Insert (now,i); the end; the Inc (now); the end; - end; the SPFA; theans:=MAXN; the    fori:=1to n Do94     if(Ans>d[i]) and (pos[i].y=t) then ans:=D[i]; the   ifAns<>Maxn then Writeln (ans) the     ElseWriteln (-1); theEnd.
View Code

(Reproduced Please specify Source: http://www.cnblogs.com/Kalenda/)

P3383: [Usaco2004 Open]cave cows 4 The four cows in the cave

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.