Hdu1245+dij, heap optimization

Source: Internet
Author: User

There is a 100*100 lake, the central coordinate (0,0), that is, the upper right corner of the lake coordinates (50,50), the middle of the lake with a (0,0) as the center, 15 of the diameter of the circular land. There is a man in the land, the lake scattered some points can be trampled, this person to use these points to jump to the shore, the shortest path and shortest path to the shortest number of steps.

SPFA inexplicable timeout, dij+ heap optimization can be over ... Perhaps the SPFA is more suitable for the map bar.

With vector<vector<node> > G This dual structure, it is best to g.resize (N) Set the capacity, otherwise the direct insertion will be wrong.

1#include <cstdio>2#include <cstring>3#include <vector>4#include <cmath>5#include <cstdlib>6#include <queue>7 using namespacestd;8 Const Doubleinf=1<< -;9 intN;Ten Doublelimit; One structnode A { -     DoubleW; -     intv; the     intpath; -NodeDoubleWwintvvintPpath): W (WW), V (VV), path (Ppath) {} - node () {} -     BOOL operator< (ConstNode &dd)Const +     { -         if(w!=DD.W) +         returnW>DD.W; A         Else at             returnPath>Dd.path; -     } - }; -vector<vector<node> >G; - DoubleDisDoubleX1,DoubleY1,DoubleX2,Doubley2) - { in     returnsqrt ((x1-x2) * (X1-X2) + (y1-y2) * (y1-y2)); - } to DoubleMmax (DoubleAaDoubleBB) + { -     if(AA&GT;BB)returnAA; the     Else returnBB; * } $ intvis[ the];Panax Notoginseng  - intMain () the { +     Doublex[ the],y[ the]; A     inti,j; the      while(SCANF ("%D%LF", &n,&limit)! =EOF) +     { -Priority_queue<node>Q; $ g.clear (); $G.resize (n+Ten); -          for(i=1; i<=n;i++) -scanf"%LF%LF",&x[i],&y[i]); the          for(i=1; i<=n;i++) -         {Wuyi             DoubleDd=dis (X[i],y[i],0.0,0.0)-7.5; the             if(dd<=limit) -             { Wug[0].push_back (Node (dd,i,0)); -G[i].push_back (Node (DD,0,0)); About             } $         } -          for(i=1; i<=n;i++) -         { -              for(j=i+1; j<=n;j++) A             { +                 DoubleDd=dis (x[i],y[i],x[j],y[j]); the                 if(dd<=limit) -                 { $G[i].push_back (Node (dd,j,0)); theG[j].push_back (Node (dd,i,0)); the                 } the             } the         } -         Doublemm; in          for(i=1; i<=n;i++) the         { themm=Mmax (Fabs (X[i]), Fabs (Y[i])); About             if( --mm<=limit) the             { theG[i].push_back (Node ( --mm,n+1,0)); theg[n+1].push_back (Node ( --mm,i,0)); +             } -         } the         //It's all built in front .BayiQ.push (Node (0,0,0));//put the starting point thememset (Vis,0,sizeof(VIS));//Mark whether the shortest path to this point has been calculated the node Z; -         BOOLflag=false;//Determine whether there is a solution -          while(!q.empty ()) the         { thez=q.top (); the Q.pop (); the             if(VIS[Z.V])Continue;//this point is the shortest circuit has been found, do not need to continue to beg -vis[z.v]=1;//The shortest path to mark this point has been calculated the             if(z.v==n+1) {flag=true; Break;}//because you only need to ask for the shortest path from the starting point to the n+1, it ends. the              for(i=0; I<g[z.v].size (); i++) the             {94 node zz; thezz.v=g[z.v][i].v; the                 if(VIS[ZZ.V])Continue; thezz.w=z.w+G[Z.V][I].W;98zz.path=z.path+1; AboutQ.push (ZZ);//It is a priority queue, regardless of whether there is already a ZZ in the queue, and whether or not the current shortest path can be shortened without having to update the shortest circuit. -             }101         }102         if(flag)103printf"%.2f%d\n", Z.w,z.path);104         Elseprintf"can ' t be saved\n"); the     }106     return 0;107}

Hdu1245+dij, heap optimization

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.