基本上,使用 Floyd-Warshall 算法的目的是确定连通图中两个节点之间的最短路径。我想要做的不是简单地找到最短路径,我想要的是最短路径,它也是一个均匀的重量。
例如,这是 Floyd-Warshall 算法的简单实现:
#include <stdio.h>
main()
{
int dist[10][10],succ[10][10],n,i,j,k;
int newDist;
scanf("%d",&n);
for (i=0;i<n;i++)
for (j=0;j<n;j++)
{
dist[i][j]=999;
succ[i][j]=j;
}
while (1)
{
scanf("%d %d %d",&i,&j,&k);
if (i==(-1))
break;
dist[i][j]=k;
distOdd[i][j]=k;
distEven[i][j]=k;
}
printf(" ");
for (i=0;i<n;i++)
printf("%3d ",i);
printf("\n");
for (i=0;i<n;i++)
{
printf("%3d ",i);
for (k=0;k<n;k++)
printf("%3d %d ",dist[i][k],succ[i][k]);
printf("\n");
}
printf("-------------------------------\n");
/* Floyd-Warshall */
for (j=0;j<n;j++)
{
for (i=0;i<n;i++)
if (dist[i][j]<999)
for (k=0;k<n;k++)
{
newDist=dist[i][j]+dist[j][k];
if (newDist<dist[i][k])
{
dist[i][k]=newDist;
succ[i][k]=succ[i][j];
}
}
printf(" ");
for (i=0;i<n;i++)
printf("%3d ",i);
printf("\n");
for (i=0;i<n;i++)
{
printf("%3d ",i);
for (k=0;k<n;k++)
printf("%3d %d ",dist[i][k],succ[i][k]);
printf("\n");
}
printf("-------------------------------\n");
}
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if (dist[i][j]==999)
printf("No path from %d to %d\n",i,j);
else
{
printf("Distance %d for %d ",dist[i][j],i);
for (k=succ[i][j];
k!=j;
k=succ[k][j])
printf("%d ",k);
printf("%d\n",j);
}
}
给定以下输入:
6
0 1 1
1 2 1
2 3 1
3 1 1
1 4 1
4 5 1
-1 -1 -1
我想要以下输出(忽略格式,我只需要一种在每一步找到“奇数矩阵”的方法)
initial odd matrix
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 1 2 999 3 1 4 999 5
999 0 999 1 999 2 1 3 999 4 999 5
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 0
odd matrix
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 1 2 999 3 1 4 999 5
999 0 999 1 999 2 1 3 999 4 999 5
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 1
odd matrix
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 1 2 999 3 1 4 999 5
999 0 999 1 999 2 1 3 999 4 999 5
999 0 1 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 999 1 2 1 999 3 2 1 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 2 1 999 3 2 1 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 2
odd matrix
999 0 1 1 999 2 3 1 999 4 999 5
999 0 999 1 1 2 999 3 1 4 999 5
999 0 999 1 999 2 1 3 999 4 999 5
999 0 1 1 999 2 3 1 999 4 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 999 1 2 1 999 3 2 1 999 5
999 0 999 1 999 2 2 2 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 2 1 999 3 2 1 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 3
odd matrix
999 0 1 1 5 1 3 1 5 1 999 5
999 0 3 2 1 2 5 2 1 4 999 5
999 0 5 3 3 3 1 3 3 3 999 5
999 0 1 1 5 1 3 1 5 1 999 5
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 4 1 2 1 6 1 2 1 999 5
999 0 6 2 4 2 2 2 4 2 999 5
999 0 2 3 6 3 4 3 6 3 999 5
999 0 4 1 2 1 6 1 2 1 999 5
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 4
odd matrix
999 0 1 1 5 1 3 1 5 1 3 1
999 0 3 2 1 2 5 2 1 4 5 2
999 0 5 3 3 3 1 3 3 3 7 3
999 0 1 1 5 1 3 1 5 1 3 1
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 4 1 2 1 6 1 2 1 6 1
999 0 6 2 4 2 2 2 4 2 2 4
999 0 2 3 6 3 4 3 6 3 4 3
999 0 4 1 2 1 6 1 2 1 6 1
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
Process column 5
odd matrix
999 0 1 1 5 1 3 1 5 1 3 1
999 0 3 2 1 2 5 2 1 4 5 2
999 0 5 3 3 3 1 3 3 3 7 3
999 0 1 1 5 1 3 1 5 1 3 1
999 0 999 1 999 2 999 3 999 4 1 5
999 0 999 1 999 2 999 3 999 4 999 5
even matrix
999 0 4 1 2 1 6 1 2 1 6 1
999 0 6 2 4 2 2 2 4 2 2 4
999 0 2 3 6 3 4 3 6 3 4 3
999 0 4 1 2 1 6 1 2 1 6 1
999 0 999 1 999 2 999 3 999 4 999 5
999 0 999 1 999 2 999 3 999 4 999 5
-------------------------------
我的代码目前所做的是获得最佳权重,该权重在每个单独的“奇数”和“偶数”矩阵中表示。
我缺乏理解的是,当最优值位于相反矩阵(奇/偶)中时,“奇”和“偶”矩阵如何得出它们的非最优值。在我看来,必须有某种循环或递归才能做到这一点,但我不知道如何实现这一点。