#include<iostream>
#include<time.h>
using namespace std;
void floyd(int a[][4],int path[][4],int n);
int main()
{
int a[4][4]={0,7,1,6,9999,0,9,9999,4,4,0,2,1,9999,9999,0};
int path[4][4]={0,7,1,6,9999,0,9,9999,4,4,0,2,1,9999,9999,0};
floyd(a,path,4);
for(int i=0;i<4;i++)
{ for(int j=0;j<4;j++)
{cout<<path[i][j]<<" ";}
cout<<endl;}
return 0;
}
void floyd(int a[][4],int path[][4],int n)
{
for(int k=0;k<n;k++)
{ for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(a[i][k]+a[k][j]<a[i][j])
{
a[i][j]=a[i][k]+a[k][j];
path[i][j]=path[k][j];
}
for(int x=0;x<n;x++)
{for(int y=0;y<n;y++)
cout<<a[x][y]<<" ";
cout<<endl;
}
}
}
(算法设计技巧与分析)floyd
最新推荐文章于 2022-12-11 01:56:14 发布