O(n3),n是端点数。
template
class CNeiBoMat
{
public:
CNeiBoMat(int n, const vector
{
m_vMat.assign(n, vector
for (int i = 0; i < n; i++)
{
m_vMat[i][i] = 0;
}
for (const auto& v : edges)
{
m_vMat[v[0]- b1Base][v[1]- b1Base] = v[2];
if (!bDirect)
{
m_vMat[v[1]- b1Base][v[0]- b1Base] = v[2];
}
}
}
vector
};
//多源码路径
template
class CFloyd
{
public:
CFloyd(const vector
{
m_vMat = mat;
const int n = mat.size();
for (int i = 0; i < n; i++)
{//通过i中转
for (int i1 = 0; i1 < n; i1++)
{
for (int i2 = 0; i2 < n; i2++)
{
//此时:m_vMat[i1][i2] 表示通过[0,i)中转的最短距离
m_vMat[i1][i2] = min(m_vMat[i1][i2], m_vMat[i1][i] + m_vMat[i][i2]);
//m_vMat[i1][i2] 表示通过[0,i]中转的最短距离
}
}
}
};
vector
};
当一层循环执行完后,m_vMat[i1][i2]表示经过[0,i)中的任意个点的最短距离。
初始状态下, m_vMat[i1][i2]表示直达的最小距离,也就是经过0个点。
通过[0,i)中任意个点,i1到i2的最短路径记为PrePathi1i2,通过[0,i+1)中任意个点,i1到i2的距离的路径为Pathi1i2,如果Path不经过Pathi1i2,则和PrePathi1i2相同。如果经过则可以拆分成{i1…i}+{i…i2},显然{i1…i}是PrePathi1i,{i…i2}是PrePathii2,否则替换成PrePathi1i和PrePathii2。
m_vMat同时表示PreMath和Math,如果m_vMat[i1][i]或m_vMat[i][i2]已经更新,会带来错误的结果么?结果是不会,会更新但值不变。
当i1等于i时:
m_vMat[i][i2] = min(…, m_vMat[i][i] + m_vMat[i][i2]);
由于m_vMat[i][i]为0,所以右式就是左式。
当i2等于i时,类似。
初始状态 |
处理完i等于0(不变) |
|||||||||
0 |
1 |
4 |
INF |
INF |
||||||
1 |
0 |
2 |
4 |
INF |
||||||
4 |
2 |
0 |
3 |
INF |
||||||
INF |
4 |
3 |
0 |
INF |
||||||
INF |
INF |
INF |
INF |
0 |
||||||
处理完i等于1 |
处理完i等于2(不变) |
|||||||||
3 |
5 |
|||||||||
3 |
||||||||||
5 |
||||||||||
处理完i等于3,结果不变 |
最终结果 |
|||||||||
0 |
1 |
3 |
5 |
INF |
||||||
1 |
0 |
2 |
4 |
INF |
||||||
3 |
2 |
0 |
3 |
INF |
||||||
5 |
4 |
3 |
0 |
INF |
||||||
INF |
INF |
INF |
INF |
0 |
#include
#include
using namespace std;
struct CDebugParam
{
int n;
vector
vector
};
int main()
{
const int INF = 1000 * 1000 * 1000;
vector
{
{0,1,3,5,INF},
{1,0,2,4,INF},
{3,2,0,3,INF},
{5,4,3,0,INF},
{INF,INF,INF,INF,0}
}
} };
for (const auto& param : params)
{
CNeiBoMat
CFloyd
for (int r = 0; r < param.n; r++)
{
for (int c = 0; c < param.n; c++)
{
assert(param.result[r][c] == floyd.m_vMat[r][c]);
}
}
}
}
win7 VS2019 C++17
https://download.csdn.net/download/he_zhidan/88393631
https://download.csdn.net/download/he_zhidan/88348653