Stack-optimized Dijkstra

Source: Internet
Author: User

I knocked on the heap again and got a deeper understanding.

Insert operation: add elements to the end of the array to automatically move up.

Delete operation: Move the elements at the end of the array to the root node and sink to a small subnode.

In the impression, the priority queue is a pointer + tedious operation. In fact, the code can be simplified to six rows.

It is only a little less than spfa, rank2's shortest path algorithm.

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <vector> <br/> using namespace STD; <br/> const int maxn = 10001; <br/> const int INF = 0x7fffffff/2-1; <br/> typedef struct {int DIS, Vex;} elemtype; <br/> typedef struct {int Vex, DIS;} edge; <br/> typedef vector <edge> ve; <br/> ve edge [maxn]; <br/> bool CMP (elemtype E1, elemtype E2) <br/>{< br/> return e1.dis <e2.dis; <br/>}< br/> class hea P <br/>{< br/> Public: <br/> heap () {n = 0 ;}< br/> int min (INT _ A, int _ B) {<br/> return a [_ A]. dis <A [_ B]. dis? _ A: _ B; <br/>}< br/> void push (elemtype e) {<br/> for (I = ++ N; i> 1 & CMP (E, a [I> 1]); I >>= 1) {<br/> A [I] = A [I> 1]; <br/>}< br/> A [I] = E; <br/>}< br/> void POP () {<br/> elemtype E = A [n]; <br/> for (I = 2; I <N; I <= 1) {<br/> if (I <n-1) {<br/> I = min (I, I + 1 ); <br/>}< br/> If (CMP (E, a [I]) break; <br/> A [I> 1] = A [I]; <br/>}< br/> A [I> 1] = A [n --]; <br/>}< br/> elemtype top () {<br/> r Eturn A [1]; <br/>}< br/> bool empty () {<br/> return! N; <br/>}< br/> PRIVATE: <br/> elemtype A [maxn]; <br/> int N, I; <br/> }; <br/> void Dijkstra (INT nvex, int st) <br/>{< br/> heap h; <br/> edge e; <br/> elemtype TMP; <br/> int dis [maxn], I, now; <br/> bool visited [maxn] = {0}; </P> <p> TMP. dis = 0; <br/> TMP. vex = sT; <br/> H. push (TMP); <br/> for (I = 0; I <nvex; I ++) dis [I] = inf; <br/> dis [st] = 0; <br/> while (! H. empty () {<br/> TMP = H. top (); H. pop (); <br/> now = TMP. vex; <br/> If (! Visited [now]) {<br/> visited [now] = true; <br/> for (I = 0; I <(INT) edge [now]. size (); I ++) {<br/> E = edge [now] [I]; <br/> If (! Visited [E. vex] & dis [now] + E. dis <dis [E. vex]) {<br/> TMP. dis = dis [TMP. vex = E. vex] = dis [now] + E. DIS; <br/> H. push (TMP); <br/>}< br/> for (I = 0; I <nvex; I ++) {<br/> If (DIS [I] = inf) {<br/> printf ("-1 "); <br/>} else {<br/> printf ("% d", DIS [I]); <br/>}< br/> void _ test () <br/>{< br/> edge E; <br/> int n, m, I, A, B, C; </P> <p> scanf ("% d", & N, & M ); <br/> for (I = 0; I <n; I ++) {<br/> edge [I]. clear (); <br/>}< br/> for (I = 0; I <m; I ++) {<br/> scanf ("% d", & A, & B, & E. dis); <br/> E. vex = B; edge [A]. push_back (E); <br/> E. vex = A; edge [B]. push_back (E); <br/>}< br/> scanf ("% d", & A); <br/> Dijkstra (n, ); <br/>}< br/> int main () <br/>{< br/>__ test (); <br/>}< br/>/* <br/> 8 9 <br/> 0 1 5 <br/> 1 2 8 <br/> 1 3 7 <br /> 1 5 4 <br/> 4 5 6 <br/> 5 6 7 <br/> 6 7 3 <br/> 1 7 2 <br/> 1 6 1 <br/> 2 <br/> 3 3 <br/> 0 1 5 <br/> 0 2 1 <br/> 1 2 3 <br/> 0 <br/> 4 5 <br/> 0 1 1 <br/> 0 2 2 <br/> 0 3 5 <br/> 1 2 1 <br/> 1 3 2 <br /> */

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.