主题:请麻烦各位哥哥姐姐能否帮忙解决一下这个程序的错误,小女子在此谢谢
是关于floyd算法的计算,不知道为什么小女子我怎么都没法算出隔点的权值(例如1->2权为1 2->3的权为4 但他总是没法显示1->3的权为5)而且显示路径的时候总是出现死循环>.<
麻烦各位哥哥姐姐看看我的程序是哪里出现错误了?非常感谢啊~~~~^.^
#include<stdio.h>
#include<stdlib.h>
#define M 100
#define MAX 100
typedef struct
{
int adj;
int weight;
}AdjMatrix[MAX][MAX];
typedef struct
{
AdjMatrix arc;
int vexnum, arcnum;
}MGraph;
void CreatGraph(MGraph *);//函数申明
void Floyd(MGraph *);
void CreatGraph(MGraph *G)//构件图
{
int i, j,n, m;
printf("请输入边数和顶点数:");
scanf("%d %d",&G->arcnum,&G->vexnum);
for (i = 1; i <= G->vexnum; i++)//初始化图
{
for ( j = 1; j <= G->vexnum; j++)
{
G->arc[i][j].adj = G->arc[j][i].adj = 0;
}
}
for ( i = 1; i <= G->arcnum; i++)//输入边和权值
{
printf("\n请输入有边的2个顶点");
scanf("%d %d",&n,&m);
while(n < 0 || n > G->vexnum || m < 0 || n > G->vexnum)
{
printf("输入的数字不符合要求 请重新输入:");
scanf("%d%d",&n,&m);
}
G->arc[n][m].adj = G->arc[m][n].adj = 1;
getchar();
printf("\n请输入%d与%d之间的权值:", n, m);
scanf("%d",&G->arc[n][m].weight);
G->arc[m][n].weight=G->arc[n][m].weight;
}
}
void Floyd(MGraph *G)
{
int n,m,k,next;
int path[MAX][MAX];
for(n=1;n<=G->vexnum;n++)
for(m=1;m<=G->vexnum;m++)
{ path[n][m]=-1; }
for (k=1;k<=G->vexnum;k++)
for (n=1;n<=G->vexnum;n++)
for (m=1;m<=G->vexnum;m++)
if (n==m)
{
G->arc[n][m].weight=9999;
}
if (G->arc[n][m].weight>(G->arc[n][k].weight+G->arc[k][m].weight))
{
G->arc[n][m].weight=G->arc[n][k].weight+G->arc[k][m].weight;
path[n][m]=k;
}
for (n=1;n<=G->vexnum;n++)
for (m=1;m<=G->vexnum;m++)
{
if (n==m)
printf("从%d到%d没有路径\n",n,m);
else
{
printf("从%d到%d路径为:",n,m);
next=path[n][m];
do
{
printf("->%d",next);
next=path[next][m];
}while(next!=m);
printf("\t路径长度为:%d\n",G->arc[n][m].weight);
}
}
}
int main(void)//主函数
{
MGraph *G;
G = (MGraph*)malloc(sizeof(MGraph));
if (G == NULL)
{
printf("memory allcation failed,goodbye");
exit(1);
}
CreatGraph(G);
Floyd(G);
system("pause");
return 0;
}
麻烦各位哥哥姐姐看看我的程序是哪里出现错误了?非常感谢啊~~~~^.^
#include<stdio.h>
#include<stdlib.h>
#define M 100
#define MAX 100
typedef struct
{
int adj;
int weight;
}AdjMatrix[MAX][MAX];
typedef struct
{
AdjMatrix arc;
int vexnum, arcnum;
}MGraph;
void CreatGraph(MGraph *);//函数申明
void Floyd(MGraph *);
void CreatGraph(MGraph *G)//构件图
{
int i, j,n, m;
printf("请输入边数和顶点数:");
scanf("%d %d",&G->arcnum,&G->vexnum);
for (i = 1; i <= G->vexnum; i++)//初始化图
{
for ( j = 1; j <= G->vexnum; j++)
{
G->arc[i][j].adj = G->arc[j][i].adj = 0;
}
}
for ( i = 1; i <= G->arcnum; i++)//输入边和权值
{
printf("\n请输入有边的2个顶点");
scanf("%d %d",&n,&m);
while(n < 0 || n > G->vexnum || m < 0 || n > G->vexnum)
{
printf("输入的数字不符合要求 请重新输入:");
scanf("%d%d",&n,&m);
}
G->arc[n][m].adj = G->arc[m][n].adj = 1;
getchar();
printf("\n请输入%d与%d之间的权值:", n, m);
scanf("%d",&G->arc[n][m].weight);
G->arc[m][n].weight=G->arc[n][m].weight;
}
}
void Floyd(MGraph *G)
{
int n,m,k,next;
int path[MAX][MAX];
for(n=1;n<=G->vexnum;n++)
for(m=1;m<=G->vexnum;m++)
{ path[n][m]=-1; }
for (k=1;k<=G->vexnum;k++)
for (n=1;n<=G->vexnum;n++)
for (m=1;m<=G->vexnum;m++)
if (n==m)
{
G->arc[n][m].weight=9999;
}
if (G->arc[n][m].weight>(G->arc[n][k].weight+G->arc[k][m].weight))
{
G->arc[n][m].weight=G->arc[n][k].weight+G->arc[k][m].weight;
path[n][m]=k;
}
for (n=1;n<=G->vexnum;n++)
for (m=1;m<=G->vexnum;m++)
{
if (n==m)
printf("从%d到%d没有路径\n",n,m);
else
{
printf("从%d到%d路径为:",n,m);
next=path[n][m];
do
{
printf("->%d",next);
next=path[next][m];
}while(next!=m);
printf("\t路径长度为:%d\n",G->arc[n][m].weight);
}
}
}
int main(void)//主函数
{
MGraph *G;
G = (MGraph*)malloc(sizeof(MGraph));
if (G == NULL)
{
printf("memory allcation failed,goodbye");
exit(1);
}
CreatGraph(G);
Floyd(G);
system("pause");
return 0;
}