# FLOYD算法之简单版

#include<iostream.h>
#include<cstdlib>
#define vex 3//定义结点的个数
#define MAX 32767//设定一个极大值
typedef struct{
char p[128];
}paths;
void Ppath1(int i,int j,paths path[vex][vex]){
if(i!=j){
int n=0;
path[i][j].p[n++]=i+65;
path[i][j].p[n++]=j+65;
path[i][j].p[n]='/0';
}
}
void Ppath(int i,int j,int k,paths path[vex][vex]){
if(i!=j){
int n=0;
path[i][j].p[n++]=i+65;
path[i][j].p[n++]=k+65;
path[i][j].p[n++]=j+65;
path[i][j].p[n]='/0';}
}
/*
0 4 11
6 0 2
3 -1 0
*/
void floyd(paths path[vex][vex],int v[vex][vex]){
for(int i=0;i<vex;i++)
for(int j=0;j<vex;j++)
Ppath1(i,j,path);
for(int k=0;k<vex;k++){
for(i=0;i<vex;i++)
for(int j=0;j<vex;j++)
if(v[i][j]>v[i][k]+v[k][j]){
v[i][j]=v[i][k]+v[k][j];
Ppath(i,j,k,path);
}
}
}
void main(){
int v[vex][vex]={{0,4,11},{6,0,2},{3,MAX,0}},i,j,k;
paths path[vex][vex];
cout<<"邻接矩阵如下："<<endl;
for(i=0;i<vex;i++){
for(j=0;j<vex;j++)
cout<<v[i][j]<<"  ";
cout<<endl;
}
floyd(path,v);
cout<<"最短路径如下："<<endl;
for(i=0;i<vex;i++){
for(j=0;j<vex;j++)
cout<<v[i][j]<<"  ";
cout<<endl;
}
cout<<"具体路径如下："<<endl;
for(i=0;i<vex;i++)
for(j=0;j<vex;j++)
if(i!=j)
cout<<i<<"  "<<j<<"   "<<path[i][j].p<<endl;
}

03-17 928

01-25 88

08-21 507

07-23 6730

02-28 1005

09-07 2018

02-26 771

12-17 1938

07-03 855