"K Short Circuit &a* algorithm" BZOJ1975: [Sdoi2010] Academy of Magical Pigs

Source: Internet
Author: User

Description find out the length of the 1~k short circuit. Solution

A * algorithm is used to solve the K short circuit

heuristic function of A * algorithm f (n) =g (n) +h (n)

g (n) is the actual cost of searching N in the state space

H (N) is the estimated cost of the best path from N to the end state

With respect to the selection of H (n), when H (n) < actual cost, the search is slow but solvable; h (n) = The actual cost, the correct rate and efficiency is the highest, h (n) > The actual cost, fast but only approximate solution can be obtained.

However, in the K short circuit problem, h (n) can be selected to the exact value, that is, N to the end of the node's shortest path, preprocessing from the end node to do once single source the shortest possible.

Expand the node in a wide-search manner, giving priority to the smallest node of f (n) at a time.

The second extension to the target node, the representative found a short circuit.

Correctness is well understood.

K Short Circuit about a * part of the code is very concise, with priority queue maintenance.

This problem is a naked problem, but this problem is very sick to let proirity_queuemle, so to handwritten heap.

Let me have this little handwritten STL dog QWQ.

Code
1#include <cstdio>2#include <algorithm>3#include <queue>4#include <cstring>5 using namespacestd;6 Const intn=5e3+5, m=2e5+5;7 8 DoubleD[n];9 inthead[n],e[m],nxt[m],cnt;Ten DoubleW[m]; One intAdde (intUintVDoubleg) { Ae[++cnt]=v;w[cnt]=g;nxt[cnt]=head[u];head[u]=CNT; - } - int_head[n],_e[m],_nxt[m],_cnt; the Double_w[m]; - int_adde (intUintVDoubleg) { -_e[++_cnt]=v;_w[_cnt]=g;_nxt[_cnt]=_head[u];_head[u]=_cnt; - } + structnode{ -     Doublef,g; +     into; A     BOOL operator< (Constnode&a) at         Const{returnf<A.F;} - }; - intn,m; - DoubleC; -  -queue<int>Q; in intInque[n]; - intSPFA () { tomemset (D,127,sizeof(d)); +d[n]=0; -inque[n]=1; the Q.push (n); *      $      while(!Q.empty ()) {Panax Notoginseng         intu=Q.front (); Q.pop (); -          for(intI=_head[u];i;i=_nxt[i]) { the             intv=_e[i]; +             if(d[v]>d[u]+_w[i]) { Ad[v]=d[u]+_w[i]; the                 if(!Inque[v]) { + Q.push (v); -inque[v]=1; $                 } $             } -         } -inque[u]=0; the     } - }Wuyi  the intans,size; -Node q[2000000]; Wu intPush (node x) { -     intNow,next; Aboutq[++size]=x; $now=size; -      while(now>1){ -Next=now>>1; -         if(Q[next]<q[now]) Break; A swap (Q[now],q[next]); +now=Next; the     } - } $ node Pop () { the     intNow,next; the node ret; theret=q[1]; theq[1]=q[size--]; -now=1; in      while((now<<1) <=size) { thenext=now<<1; the         if(next<size&&q[next+1]<q[next]) next++; About         if(Q[now]<q[next]) Break; the swap (Q[now],q[next]); thenow=Next; the     } +     returnret; - } the voidAstar () {BayiPush (node) {d[1],0,1}); the      while(size) { theNode x=pop (); -          for(intI=head[x.o];i;i=Nxt[i]) { -             intv=E[i]; thePush (node) {x.g+w[i]+d[v],x.g+w[i],v}); the         } the         if(x.o==N) { thec-=x.f; -             if(c<0)return; theans++; the         } the     }94 } the  the intMain () { thescanf"%D%D%LF",&n,&m,&c);98     intU,v;DoubleG; About      for(intI=1; i<=m;i++){ -scanf"%D%D%LF",&u,&v,&g);101 Adde (u,v,g);102 _adde (v,u,g);103     }104      the SPFA ();106 Astar ();107     108printf"%d\n", ans);109     return 0; the}

"K Short Circuit &a* algorithm" BZOJ1975: [Sdoi2010] Academy of Magical Pigs

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.