Shortest path--spfa+ Queue optimization template

Source: Internet
Author: User

SPFA Ordinary version will not write, optimization or to the ANG, SPFA can be sentenced to negative ring, accept negative right side and heavy edge, judge negative ring only need to open an array to record each node of the number of times, when there is any one node more than a point on the queue indicates that there is a negative ring exists

1#include <stdio.h>//SPFA basically want these header files2#include <string.h>3#include <queue>4 using namespacestd;5 6 voidSPFA (intSintp) {7memset (Vis,0,sizeof(Vis));8memset (dist,-1,sizeof(Dist));9queue<int>Q;Tenvis[s]=1; Onedist[s]=0; A Q.push (s); -      while(!Q.empty ()) { -         inti,t=Q.front (); thevis[t]=0; - Q.pop (); -          for(i=head[t];~i;i=Next[i]) { -             intj=Point[i]; +             if(dist[j]==-1|| dist[j]>dist[t]+Val[i]) { -dist[j]=dist[t]+Val[i]; +                 if(!Vis[j]) { A Q.push (j); atvis[j]=1; -                 } -             } -         } -     } -printf"%d\n", Dist[p]); in}

Shortest path--spfa+ 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.