Dijkstra happy STL version optimized by heap

Source: Internet
Author: User

# Include <stdio. h> <br/> # include <string. h> <br/> # include <vector> <br/> # include <queue> <br/> # include <iostream> <br/> using namespace STD; </P> <p> const int maxn = 201; <br/> const int INF = 0x7fffffff/2; <br/> typedef struct {int L, V;} edge; <br/> typedef vector <edge> ve; <br/> struct Vex {<br/> int V, DIS; <br/> friend bool operator <(Vex V1, vex V2) {<br/> return v1.dis> v2.dis; <br/>}< br/>}; <B R/> ve e [maxn]; <br/> priority_queue <Vex> q; </P> <p> int Dijkstra (VE * E, int N, int St, int ed) <br/>{< br/> int dis [maxn], I; <br/> bool visited [maxn] = {0 }; <br/> for (I = 0; I <n; I ++) {<br/> dis [I] = inf; <br/>}</P> <p> Vex now, next; <br/> now. dis = dis [now. V = sT] = 0; <br/> edge et; <br/> while (! Q. Empty () Q. Pop (); <br/> q. Push (now); <br/> while (! Q. Empty () {<br/> now = Q. Top (); q. Pop (); <br/> If (! Visited [now. v]) {<br/> visited [now. v] = true; <br/> for (I = 0; I <E [now. v]. size (); I ++) {<br/> ET = E [now. v] [I]; <br/> next. V = et. v; <br/> If (! Visited [et. v] & et. L + dis [now. v] <dis [et. v]) {<br/> next. dis = dis [next. V = et. v] = et. L + dis [now. v]; <br/> q. push (next); <br/>}< br/> for (I = 0; I <N; I ++) {<br/> If (DIS [I] = inf) {<br/> printf ("-1 "); <br/>} else {<br/> printf ("% d", DIS [I]); <br/>}< br/> return dis [ed]; <br/>}</P> <p> void _ test () <br/>{< br/> edge Ed; <br/> int n, m, I, A, B, C; </P> <p> scanf ("% d", & N, & M); <br/> for (I = 0; I <N; I ++) {<br/> E [I]. clear (); <br/>}< br/> for (I = 0; I <m; I ++) {<br/> scanf ("% d", & A, & B, & ED. l); <br/> ed. V = B; E [A]. push_back (ed); <br/> ed. V = A; E [B]. push_back (ed); <br/>}< br/> scanf ("% d", & A, & B); <br/> Dijkstra (E, n, a, B); <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 3 <br/> 3 3 <br/> 0 1 5 <br/> 0 2 1 <br/> 1 2 3 <br/> 0 1 <br/> 4 5 <br/> 0 1 1 <br/> 0 2 2 <br/> 0 3 5 <br/> 1 2 1 <br/> 1 3 2 <br/> 0 1 <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.