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]);
}