hdu2544 (self-fulfilling priority queue)

Source: Internet
Author: User

hdu2544 Dij water problem, used to test their own implementation of the priority queue right

1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <algorithm>5#include <iostream>6#include <queue>7#include <stack>8#include <vector>9#include <map>Ten#include <Set> One#include <string> A#include <math.h> - using namespacestd; - #pragmaWarning (disable:4996) thetypedefLong LongLL; - Const intINF =1<< -; - Const intN = ++Ten; - intLen; + structnode - { +     intV, cost; A     intNext; at}g[10000+Ten]; - intHead[n], E; - intDist[n]; - BOOLVis[n]; - voidInitintN) - { in      for(inti =1; I <= N; ++i) -Head[i] =-1; toE =0; + } - voidAddedge (intUintVintc) the { *G[E].V =v; $G[e].cost =C;Panax NotoginsengG[e].next =Head[u]; -Head[u] = e++; the } + structNode2 A { the     intV, Dist; +     BOOL operator< (ConstNODE2&AMP;RHS)Const -     { $         returnDist >rhs.dist; $     } -}q[100000]; -  the  - voidPush (Node2 tmp)//add an element at the end of the team, as long as the father's knot is not too much.Wuyi { the     intAdjustindex = + +Len; -      for(inti = adjustindex/2; I >=1; I/=2) Wu     { -         if(Q[i].dist <tmp.dist) About              Break; $Q[adjustindex] =Q[i]; -Adjustindex =i; -     } -Q[adjustindex] =tmp; A } +  theNode2Get() - { $NODE2 ret = q[1];//get the first element of the team theNode2 tmp = q[len--];//put the tail elements on the team head the     intAdjustindex =1; the     //adjusted into a small top heap the      for(inti = Adjustindex *2; I <= Len; i = i *2) -     { in         if(I < len && q[i].dist > Q[i +1].dist) thei++; the         if(Tmp.dist <q[i].dist) About              Break; theQ[adjustindex] =Q[i]; theAdjustindex =i; the     } +Q[adjustindex] =tmp; -     returnret; the }Bayi  the voidDij (intN) the { -     //priority_queue<node2> Q; -      for(inti =1; I <= N; ++i) the     { theDist[i] =INF; theVis[i] =false; the     } -Len =0; thedist[1] =0; the node2 cur, tmp; theCUR.V =1;94Cur.dist =0; the     //Q.push (cur); the push (cur); the      while(len!=0)98     { AboutCur =Get(); -         //cur = q.top ();101         //Q.pop ();102         intx =cur.v;103         if(Vis[x])Continue;104VIS[X] =true; the          for(inti = head[x]; I! =-1; i =g[i].next)106         {107             intv =g[i].v;108TMP.V =v;109             if(Dist[v] > Dist[x] +g[i].cost) the             {111Tmp.dist = Dist[v] = dist[x] +G[i].cost; the                 //Q.push (TMP);113 push (TMP); the             } the         } the 117     }118 }119 intMain () - {121     intN, M, A, B, C, I;122      while(SCANF ("%d%d", &n, &m), N)123     {124 init (n); the          for(i =0; I < m; ++i)126         {127scanf"%d%d%d", &a, &b, &c); - Addedge (A, B, c);129 Addedge (b, A, c); the         }131 Dij (n); theprintf"%d\n", Dist[n]);133     }134}
View Code

hdu2544 (self-fulfilling priority queue)

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.