Shortest path--dijkstra+ Priority Queue Optimization Template

Source: Internet
Author: User

Do not write the Normal template, or need priority queue optimization of the Ang

1#include <stdio.h>//basic need for header files2#include <string.h>3#include <queue>4#include <algorithm>5#include <vector>6 using namespacestd;7typedef pair<int,int>PII;8 Const intinf=0x3f3f3f3f;9 Ten  One structcmp{//Change priority queue to small Gan A     BOOL operator() (PII a,pii b) { -         returnA.first>B.first; -     } the }; -  - voidDij (intSintT) {//incoming starting point and arrival point -     inti; +Priority_queue<pii,vector<pii>,cmp>Q; -Q.push (Make_pair (0, s)); +memset (dist,-1,sizeof(Dist)); Adist[s]=0; at      while(!Q.empty ()) { -PII u=q.top (); - Q.pop (); -         if(U.first>dist[u.second])Continue; -          for(i=head[u.second];~i;i=Next[i]) { -             intj=Point[i]; in             if(dist[j]==-1|| dist[j]>u.first+Val[i]) { -dist[j]=u.first+Val[i]; to Q.push (Make_pair (dist[j],j)); +             } -         } the     } *printf"%d\n", Dist[t]);//or remove the output or action in the main function $}

Shortest path--dijkstra+ Priority Queue Optimization Template

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.