ACwing(基础)--- Floyd

ACwing(基础)--- Floyd

Floyd算法

  • 时间复杂度是O(n^3)

初始化

初始化:
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= n; j ++ )
            if (i == j) mp[i][j] = 0;
            else mp[i][j] = INF;

核心算法

  • 注意:k,i,j不能调换顺序
// 算法结束后,mp[a][b]表示a到b的最短距离
void floyd()
{
    for (int k = 1; k <= n; k ++ )
        for (int i = 1; i <= n; i ++ )
            for (int j = 1; j <= n; j ++ )
                mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}

ACwing(基础)--- Floyd

上一篇:c# 对象拷贝 object clone


下一篇:ACwing(基础) --- Bellman-Ford&SPFA