ps. f 数组是floyed求出的两点间最短距离
证明该公式如下: 1.如图,当 k在线段ij两侧时 (这里只有在右边,但是左右证明方法相同) f [ k ] [ i ] = f [ i ] [ j ] + f [ j ] [ k ] f[k][i]=f[i][j]+f[j][k] f[k][i]=f[i][j]+f[j][k] f [ k ] [ j ] = f [ k ] [ j ] f[k][j]=f[k][j] f[k][j]=f[k][j] f [ i ] [ j ] = f [ i ] [ j ] f[i][j]=f[i][j] f[i][j]=f[i][j]所以 ( f [ k ] [ i ] + f [ k ] [ j ] − f [ i ] [ j ] ) / 2 (f[k][i]+f[k][j]-f[i][j])/2 (f[k][i]+f[k][j]−f[i][j])/2等于 ( f [ i ] [ j ] + f [ j ] [ k ] + f [ k ] [ j ] − f [ i ] [ j ] ) / 2 (f[i][j]+f[j][k]+f[k][j]-f[i][j])/2 (f[i][j]+f[j][k]+f[k][j]−f[i][j])/2即 ( f [ j ] [ k ] + f [ j ] [ k ] ) / 2 (f[j][k]+f[j][k])/2 (f[j][k]+f[j][k])/2所以在此时 ( f [ k ] [ i ] + f [ k ] [ j ] − f [ i ] [ j ] ) / 2 = f [ j ] [ k ] (f[k][i]+f[k][j]-f[i][j])/2=f[j][k] (f[k][i]+f[k][j]−f[i][j])/2=f[j][k]由图可知 f[j][k] 即为k到线段ij的距离 如图,当点k到线段ij的最短路径在ij中部时f [ k ] [ i ] = f [ i ] [ m ] + f [ m ] [ k ] f[k][i]=f[i][m]+f[m][k] f[k][i]=f[i][m]+f[m][k] f [ k ] [ j ] = f [ j ] [ m ] + f [ m ] [ k ] f[k][j]=f[j][m]+f[m][k] f[k][j]=f[j][m]+f[m][k] f [ i ] [ j ] = f [ i ] [ j ] f[i][j]=f[i][j] f[i][j]=f[i][j]
所以 ( f [ k ] [ i ] + f [ k ] [ j ] − f [ i ] [ j ] ) / 2 (f[k][i]+f[k][j]-f[i][j])/2 (f[k][i]+f[k][j]−f[i][j])/2等于 ( f [ i ] [ m ] + f [ m ] [ k ] + f [ j ] [ m ] + f [ m ] [ k ] − f [ i ] [ j ] ) / 2 (f[i][m]+f[m][k]+f[j][m]+f[m][k]-f[i][j])/2 (f[i][m]+f[m][k]+f[j][m]+f[m][k]−f[i][j])/2即 ( f [ m ] [ k ] + f [ m ] [ k ] ) / 2 (f[m][k]+f[m][k])/2 (f[m][k]+f[m][k])/2所以在此时 ( f [ i ] [ m ] + f [ m ] [ k ] + f [ j ] [ m ] + f [ m ] [ k ] − f [ i ] [ j ] ) / 2 = f [ m ] [ k ] (f[i][m]+f[m][k]+f[j][m]+f[m][k]-f[i][j])/2=f[m][k] (f[i][m]+f[m][k]+f[j][m]+f[m][k]−f[i][j])/2=f[m][k]由图可知 f[m][k] 即为k到线段ij的距离至此,公式正确性证明完毕