#include
#include
using namespace std;
#define MAXV 50
#define INF 1000000000
typedef int InfoType;
//邻接矩阵存储方法
typedef struct
{
int n;
int edges[MAXV][MAXV];
} MGraph;
FILE *f_in;//输入
FILE *f_out;//输出
int n;//n个点
//狄克斯特拉算法
//递归输出
void Ppath(int path[],int i,int v)
{
int k;
k = path[i];
if(k == v) //递归出口
return;
Ppath(path,k,v);
fprintf(f_out,"%d->",k);
}
//判断两点之间是否存在路径
int biaoji1=1,biaoji2=0;
void Dispath(int dist[],int path[],bool s[],int n,int v)
{
int i;
for(i = 0;i < n;i ++)
{
if(i == v)
continue;
if(s[i] == 1)//证明v到i存在最短一条路
{
fprintf(f_out,"从%d到%d的最短距离为:%d ",v,i,dist[i]);
fprintf(f_out,"路径为 : ");
fprintf(f_out,"%d->",v);
//递归入口
Ppath(path,i,v);
fprintf(f_out,"%d\n",i);
if(biaoji1 + 1 != n)
{
biaoji2+=dist[i];biaoji1++;
}
else
{
fprintf(f_out,"和为:%d\n",biaoji2);
biaoji1=1;biaoji2=0;
}
}
else
fprintf(f_out,"从%d到%d不存在的路径\n",v,i);
}
}
//path数组用来记录路径
//dist数组记录点v到其他任意点的最短距离
//s数组用来标记
void Dijkstra(MGraph g,int v)
{
int dist[MAXV],path[MAXV];
bool s[MAXV];
int mindis,i,j,u;
for(i = 0;i < g.n;i ++)
{
dist[i] = g.edges[v][i];
//标记数组初始化
s[i] = 0;
//path数组初始化
if(g.edges[v][i] < INF) path[i] = v;
else path[i] = -1;
}
//循环进行的初始条件
s[v] = 1;dist[v] = 0;
//Dijkstra算法核心
for(i = 0;i < g.n;i ++)
{
mindis = INF;
for(j = 0;j < g.n;j ++)
{
if(!s[j] && dist[j] < mindis)
{
u = j;
mindis = dist[j];
}
}
s[u] = 1;
for(j = 0;j < g.n;j ++)
{
if(!s[j])
{
if(g.edges[u][j] < INF && dist[u] + g.edges[u][j] < dist[j])
{
dist[j] = dist[u] + g.edges[u][j];
path[j] = u;
}
}
}
}
Dispath(dist,path,s,g.n,v);
}
//弗洛伊德算法
//递归输出
void Ppath1(int path[][MAXV],int i,int j)
{
int k;
k = path[i][j];
if(k == -1) return;//递归出口
Ppath1(path,i,k);
fprintf(f_out,"%d->",k);
Ppath1(path,k,j);
}
//判断两点之间是否存在路径
void Dispath1(int A[][MAXV],int path[][MAXV],int n)
{
int i,j;
for(i = 0;i < n;i ++)
{
for(j = 0;j < n;j ++)
{
if(i == j) continue;
if(A[i][j] == INF)
{
if(i != j)
fprintf(f_out,"从%d到%d不存在路径",i,j);
}
else
{
fprintf(f_out,"从%d到%d的最短距离为:%d ",i,j,A[i][j]);
fprintf(f_out,"路径为 : ");
fprintf(f_out,"%d->",i);
Ppath1(path,i,j);//如果存在则进行递归输出
fprintf(f_out,"%d\n",j);
}
}
}
}
//求邻接矩阵中任意两点之间的最短距离
void Floyd(MGraph g)
{
int A[MAXV][MAXV],path[MAXV][MAXV];
int i,j,k;
//初始化
for(i = 0;i < g.n;i ++)
{
for(j =0;j < g.n;j ++)
{
A[i][j] = g.edges[i][j];
path[i][j] = -1;
}
}
//Folyd算法核心
for(k = 0;k < g.n;k ++)
{
for(i = 0;i < g.n;i ++)
{
for(j = 0;j < g.n;j ++)
{
if(A[i][j] > A[i][k] + A[k][j])
{
A[i][j] = A[i][k] + A[k][j];
path[i][j] = k;
}
}
}
}
Dispath1(A,path,g.n);
}
//主函数
int main()
{
f_in = fopen("F:\\数据结构课程设计\\课程设计\\in.txt","r");
f_out = fopen("F:\\数据结构课程设计\\课程设计\\out.txt","w");
int i,j;
char s[1010];//用来输入提示信息
fscanf(f_in,"%s",s);//输入提示信息
while(fscanf(f_in,"%d",&n)!=EOF/*cin>>n,n!=EOF*/)
{
MGraph g;//定义邻接矩阵
fscanf(f_in,"%s",s);//输入提示信息
//输入两个点之间的距离,若为无穷大,不存在
for(i = 0;i < n;i ++)
for(j = 0;j < n;j ++)
fscanf(f_in,"%d",&g.edges[i][j]);
g.n = n;//点的个数
fprintf(f_out,"输出距离,最短路径和及路径\n");//输出提示信息
for(i=0;i
输入文件中数据(in.txt)
请输入带权有向图的顶点个数:
6
请输入带权有向图的邻接矩阵:
0 5 1000000000 7 1000000000 1000000000
1000000000 0 4 1000000000 1000000000 1000000000
8 1000000000 0 1000000000 1000000000 9
1000000000 1000000000 5 0 1000000000 6
1000000000 1000000000 1000000000 5 0 1000000000
3 1000000000 1000000000 1000000000 1 0