floyd算法C++实现

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include

#include

using namespace std;

#define MAXV 100

#define INF 32767

typedef int InfoType;

typedef int Vertex;

typedef struct

{ int no;

InfoType info;

} VertexType; //顶点类型

typedef struct

{ int edges[MAXV][MAXV];

int n,e;

VertexType vexs[MAXV];

} MGraph; //图类型

void Ppath(int path[][MAXV], int i, int j)

{ int k;

k=path[i][j];

if (k==-1) return; //递归出口

Ppath(path,i,k);

cout<

Ppath(path,k,j);

}

void Dispath(int A[][MAXV],int path[][MAXV],int n) { int i,j;

for (i=0;i

for (j=0;j

if (A[i][j]==INF)

{ if(i!=j) cout<< "从" <

<

}

else

{ cout<< "从" <

<< " 的路径为: "<< i <<" ";

Ppath(path, i, j);

cout<

cout<< " \t\t 路径长度为:"<

<

}

}

void Floyd(MGraph g)

{ int A[MAXV][MAXV],path[MAXV][MAXV];

int i,j,k;

for (i=0;i

for (j=0;j

{

A[i][j]=g.edges[i][j];

path[i][j]=-1;

}

for (k=0;k

for (i=0;i

for (j=0;j

if (A[i][j]>(A[i][k]+A[k][j]))

{

A[i][j]=A[i][k]+A[k][j];

path[i][j]=k;

}

Dispath(A,path,g.n); //输出最短路径}

void DispMat(MGraph g)

{

int i,j;

for(i=0;i

{

for(j=0;j

if(g.edges[i][j]==INF)

cout <

else cout<

cout<

}

}

void Floyd(MGraph g);

void Ppath(int path[][MAXV], int i, int j) ;

void Dispath(int A[][MAXV],int path[][MAXV],int n); void DispMat(MGraph g);

void main()

{

int i,j;

MGraph g;

cout<<"输入邻接矩阵g总顶点数g.n和总边数g.e:"; cin>>g.n>>g.e;

cout<<"输入邻接矩阵g的元素值:\n";

for(i=0;i

{

cout<<"第"<

for(j=0;j

cin>>g.edges[i][j];

}

cout<<"输出邻接矩阵g:\n";

DispMat(g);

cout<<"输出每对顶点之间的最短路径:\n";

Floyd(g);

cout<

}

C++语言:

#include

#define Maxm 501

using namespace std;

ifstream fin; ofstream fout("APSP.out");

int p,q,k,m; int Vertex,Line[Maxm];

int Path[Maxm][Maxm],Dist[Maxm][Maxm];

void Root(int p,int q)

{ if (Path[p][q]>0)

{ Root(p,Path[p][q]); Root(Path[p][q],q); }

else { Line[k]=q; k++; } }

int main()

{ memset(Path,0,sizeof(Path));

memset(Dist,0,sizeof(Dist));

fin >> Vertex;

for(p=1;p<=Vertex;p++)

for(q=1;q<=Vertex;q++)

fin >> Dist[p][q];

for(k=1;k<=Vertex;k++)

for(p=1;p<=Vertex;p++)

if (Dist[p][k]>0) for(q=1;q<=Vertex;q++) if (Dist[k][q]>0)

{ if (((Dist[p][q]>Dist[p][k]+Dist[k][q])||(Dist[p][q]==0))&&(p!=q)) { Dist[p][q]=Dist[p][k]+Dist[k][q]; Path[p][q]=k; }

} for(p=1;p<=Vertex;p++)

{ for(q=p+1;q<=Vertex;q++)

{ fout << "\n==========================\n";

fout << "Source:" << p << '\n' << "Target " << q << '\n';

fout << "Distance:" << Dist[p][q] << '\n';

fout << "Path:" << p; k=2; Root(p,q);

for(m=2;m<=k-1;m++) fout << "-->" << Line[m];

fout << '\n'; fout << "==========================\n"; } } fin.close(); fout.close(); return 0; }

注解:无法连通的两个点之间距离为0;

Sample Input 7 00 20 50 30 00 00 00 20 00 25 00 00 70 00 50 25 00 40 25 50 00 30 00 40 00 55 00 00 00 00 25 55 00 10 70 00 70 50 00 10 00 50 00 00 00 00 70 50 00 Sample Output ========================== Source:1 Target 2 Distance:20 Path:1-->2 ========================== ========================== Source:1 Target 3 Distance:45 Path:1-->2-->3 ========================== ========================== Source:1 Target 4 Distance:30 Path:1-->4 ========================== ========================== Source:1 Target 5 Distance:70 Path:1-->2-->3-->5 ========================== ========================== Source:1 Target 6 Distance:80 Path:1-->2-->3-->5-->6 ========================== ========================== Source:1 Target 7 Distance:130 Path:1-->2-->3-->5-->6-->7 ========================== ========================== Source:2 Target 3 Distance:25 Path:2-->3 ========================== ========================== Source:2 Target 4 Distance:50 Path:2-->1-->4 ========================== ========================== Source:2 Target 5 Distance:50 Path:2-->3-->5 ==========================

相关文档
最新文档