最短路算法的整理

Floyd-Warshall算法

        for (int k=1;k<=n;k++)
        {
            for (int i=1;i<=n;i++)
            {
                for (int j=1;j<=n;j++)
                {
                    if (a[i][k]+a[k][j]<a[i][j])
                    {
                        a[i][j]=a[i][k]+a[k][j];
                    }
                }
            }
        }

Bellman-Ford算法

        for (int loop=1;loop<=n;loop++)
        {
            flag=true;
            for (int i=1;i<=n;i++)
            {
                for (int j=1;j<=n;j++)
                {
                    if (a[i][j]!=0)
                    {
                        if (dist[j]>dist[i]+a[i][j])
                        {
                            dist[j]=dist[i]+a[i][j];
                            flag=false;
                        }
                    }
                }
            }
            if (flag)break;
        }


原文地址:https://www.cnblogs.com/cyendra/p/3038404.html