Shortest circuit template (dijkstra&&flody)

Source: Internet
Author: User

1.dijkstra

void Dijkstra (int x,int N)//x represents the starting point, and N represents the total number of {    int min=0,p=0;    for (int i=1;i<=n;i++)    {        dis[i]=map[x][i];        visited[i]=0;    }    Visited[x]=1;    for (int i=1;i<=n;i++)    {        min=inf;        for (int j=1;j<=n;j++)        {            if (!visited[j]&&dis[j]<min)            {                min=dis[j];                p=j;            }        }        Visited[p]=1;        for (int j=1;j<=n;j++)        {            if (!visited[j]&&dis[j]>dis[p]+map[p][j])                Dis[j]=dis[p]+map [P] [j];}}    }

2.flody

void Floyd () {    int i,j,k;    for (k=0;k<n;k++) for (        i=0;i<n;i++) for (            j=0;j<n;j++)                if (Dis[i][j]>dis[i][k]+dis[k][j] )                   dis[i][j]=dis[i][k]+dis[k][j];}

Personal Advice if you can use Dijkstra where possible, flody will usually time out. Of course, if the case of negative ring, you need to use the SPFA algorithm.

Shortest circuit template (dijkstra&&flody)

Related Article

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.