思路什么的应该都清楚,直接上源码(VS上以调试)
此算法用的是有向网结构存储
#include<iostream>
using namespace std;
const int MAX_VERtEX_NUM = 20;
const int OK = 1;
const int ERROR = -1;
const int overflow = -2;
const int N_infinity = 65535;
typedef int VRType; //表示顶点之间的关系的变量类型
typedef int InfoType; //存储弧或边额外信息的指针变量类型
typedef int VertexType; //图中顶点的数据类型
typedef int Status;
typedef struct
{
VRType adj; //对于无权图,用1或0表示是否相邻;对于带权图,直接为权值
InfoType* info; //弧或边额外含有的信息指针
}ArcCell, AdjMatrix[MAX_VERtEX_NUM][MAX_VERtEX_NUM];
typedef struct
{
VertexType vexs[MAX_VERtEX_NUM]; //存储图中顶点数据
AdjMatrix arcs; //二维数组,记录顶点之间的关系
int vexnum, arcnum; //记录图的顶点数和弧(边)数
}MGraph;
/******************函数声明*******************/
Status LocateVex(MGraph G, VertexType v);
//根据顶点本身数据,判断出顶点在二维数组中的位置
Status CreatUDG(MGraph& G);
//创建有向网
Status PrintGraph(MGraph G);
Status Floyd(MGraph& G, int path[][MAX_VERtEX_NUM], int dist[][MAX_VERtEX_NUM]);
Status showPath(MGraph G, int path[][MAX_VERtEX_NUM], int start, int end);
/*************main*************/
int main()
{
MGraph G1;
int reStart;
int reEnd;
int path[MAX_VERtEX_NUM][MAX_VERtEX_NUM];
int dist[MAX_VERtEX_NUM][MAX_VERtEX_NUM];
CreatUDG(G1);
cout << "显示有向网的邻接矩阵:" << endl;
PrintGraph(G1);
cout << endl << "打印出最短路径:" << endl;
Floyd(G1, path, dist);
cout << endl;
cout << "分别输入要任意两个顶点的下标:" << endl;
cin >> reStart >> reEnd;
cout << "则两个顶点间的最短路径:" << endl;
showPath(G1, path, reStart, reEnd);
return OK;
}
/**************函数定义****************/
Status LocateVex(MGraph G, VertexType v)
{
int i;
for (i = 0; i < G.vexnum; i++)
{
if (G.vexs[i] == v)
{
break;
}
}
//如果找不到,输出提示语句,返回ERROR
if (i > G.vexnum)
{
cout << "no such vertex." << endl;
return ERROR;
}
return i;
}
Status CreatUDG(MGraph& G)
{
cout << "分别输入有向网的顶点数和弧数:" << endl;
cin >> G.vexnum >> G.arcnum;
cout << "输入顶点的数据:" << endl;
for (int i = 0; i < G.vexnum; i++)
{
cin >> G.vexs[i];
}
for (int i = 0; i < G.vexnum; i++)
{
for (int j = 0; j < G.vexnum; j++)
{
G.arcs[i][j].adj = N_infinity;
G.arcs[i][j].info = NULL;
}
}
cout << "输入顶点间的关系:" << endl;
for (int i = 0; i < G.arcnum; i++)
{
int v1, v2; //v1为弧头,v2为弧尾
int w; //权值
cin >> v1 >> v2 >> w;
int n = LocateVex(G, v1);
int m = LocateVex(G, v2);
if (m == ERROR || n == ERROR)
{
cout << "no such vertex" << endl;
return ERROR;
}
G.arcs[n][m].adj = w;
}
return OK;
}
Status PrintGraph(MGraph G)
{
for (int i = 0; i < G.vexnum; i++)
{
for (int j = 0; j < G.vexnum; j++)
{
if (G.arcs[i][j].adj == N_infinity)
{
cout << "∞" << ' ';
}
else
{
cout << G.arcs[i][j].adj << ' ';
}
}
cout << endl;
}
return OK;
}
Status Floyd(MGraph& G, int path[][MAX_VERtEX_NUM], int dist[][MAX_VERtEX_NUM])
{
int i, j, k;
int temp;
//初始化
for (i = 0; i < G.vexnum; i++)
{
for (j = 0; j < G.vexnum; j++)
{
dist[i][j] = G.arcs[i][j].adj;
path[i][j] = j;
}
}
//计算最短路径
for (k = 0; k < G.vexnum; k++)
{
for (i = 0; i < G.vexnum; i++)
{
for (j = 0; j < G.vexnum; j++)
{
//如果经过下标为k顶点路径比原两点间路径更短,则更新dist[i][j]和path[i][j]
temp = (dist[i][k] == N_infinity || dist[k][j] == N_infinity) ? N_infinity : (dist[i][k] + dist[k][j]);
if (dist[i][j] > temp)
{
//"i到j最短路径"对应的值设,为更小的一个(即经过k)
dist[i][j] = temp;
//"i到j最短路径"对应的路径,经过k
path[i][j] = path[i][k];
}
}
}
}
//打印fLoyd最短路径的结果
cout << "floyd:" << endl;
cout << "最短路径权值和矩阵:" << endl;
for (i = 0; i < G.vexnum; i++)
{
for (j = 0; j < G.vexnum; j++)
{
if (dist[i][j] == N_infinity)
{
cout << "∞" << ' ';
}
else
{
cout << dist[i][j] << ' ';
}
}
cout << endl;
}
return OK;
}
Status showPath(MGraph G, int path[][MAX_VERtEX_NUM], int start, int end)
{
int begin = path[start][end];
cout << G.vexs[start] << "到" << G.vexs[end] << "的最短路径:" << endl;
cout << G.vexs[start];
while (begin != end)
{
cout << "->" << G.vexs[begin];
begin = path[begin][end];// 迭代后半段
}
cout << "->" << G.vexs[end] << endl;
return OK;
}