关于弗洛依德算法的一个程序的问题
这个程序运行起来总是报错,大家帮我看看呗。谢谢啊。我调了好长时间怎么也没弄出来。#include <stdio.h>
#include <stdlib.h>
#define MAX 100
#define INFINITY 9999
#define MAXSIZE 500000
#include <time.h>
#include <windows.h>
#include <dos.h>
typedef bool PathMatrix[MAX+1][MAX+1][MAX+1];
typedef int DistanceMatrix[MAX+1][MAX+1];
typedef struct
{
int vexnum,arcnum;
char vexs[MAX+1];
int arcs[MAX+1][MAX+1];
}MGraph;
//生成随机数做为权值
void sjsge(int arcnum)
{
int *Arr;
srand(unsigned(time(0)));/*每次生成的数字都不一样*/
FILE *fp = fopen("flyd.txt", "w");
if (fp==NULL)
{
exit(1);
}
Arr = (int *) malloc(MAXSIZE * sizeof(int));
for (int i=0;i<arcnum;i++)/*产生随机数*/
Arr[i]=rand()%50+1;//生成的数大于1小于50
for (i=0;i<arcnum;i++)
{
fprintf(fp, "%d",Arr[i]);
fprintf(fp, " ");
}
// fclose(fp);
}
//生成随机权值
void CreateDN(MGraph &G,int m)
{
int i,j;
//int sjs;
int test[100];
int cnt=0;
G.arcnum=m;
G.vexnum=m+10;
sjsge(G.arcnum);
FILE *fp1;
fp1=fopen("flyd.txt", "r");
for(i = 0 ; i < G.arcnum ; i++)
{
fscanf(fp1,"%d\t",&test[i]);
}
for(int k=0;k<G.vexnum;k++)
for(int l=0;l<G.vexnum;l++)
{
if(k==l)
{
G.arcs[k][l]=INFINITY;
//printf("l=j%d,%d\n",i,j);
}
else{
G.arcs[k][l]=0;
//printf("i!=j%d,%d\n",i,j);
}
}
FILE *fp2 = fopen("quanzhi.txt","w");
if (fp2==NULL)
{
exit(1);
}
srand((unsigned long)time(0));
while(cnt<G.arcnum)
{
i=rand()%G.vexnum;
j=rand()%G.vexnum;
while(G.arcs[i][j])//初始值必须设为0,否则循环不执行
{
i=rand()%G.vexnum;
j=rand()%G.vexnum;
}
G.arcs[i][j]=test[cnt++];
fprintf(fp2,"%d\t",i);
fprintf(fp2,"%d\t",j);
fprintf(fp2,"%d\n",G.arcs[i][j]);
}
for(i=0;i<G.vexnum;i++)
for(j=0;j<G.vexnum;j++)
{
if(G.arcs[i][j]==0)
G.arcs[i][j]=INFINITY;
}
}
long ShortestPath_FLOYD(MGraph G,PathMatrix &P,DistanceMatrix &D)
{
int v,w,u,i;
long time1;
long time2;
long t;
time1=GetTickCount();
for(v=0;v<G.vexnum;++v)
for(w=0;w<G.vexnum;++w)
{
D[v][w]=G.arcs[v][w];
for(u=0;u<G.vexnum;++u)
P[v][w][u]=false;
if (D[v][w]<INFINITY)
{
P[v][w][v]=true;
P[v][w][w]=true;
}
}
for(u=0;u<G.vexnum;++u)
for(v=0;v<G.vexnum;++v)
for(w=0;w<G.vexnum;++w)
{
if(v==w)
printf("自身不可达\n");
else if(D[v][u]+D[u][w]<D[v][w])
{
D[v][w]=D[v][u]+D[u][w];
for(i=0;i<G.vexnum;++i)
P[v][w][i]=P[v][u][i]||P[u][w][i];
}
}
time2=GetTickCount();
t=time2-time1;
return t;
}
void main()
{
int m=20;
long t;
FILE *fp=fopen("123.xls","w");
if(fp==NULL)
{
exit(1);
}
fprintf(fp,"%s\t","顶点数");
fprintf(fp,"%s\n","花费时间");
while(m<100)
{
MGraph G;
CreateDN(G,m);
PathMatrix p;
DistanceMatrix D;
t=ShortestPath_FLOYD(G,p,D);
printf("t的值是%ld",t);
fprintf(fp,"%d\t",m);
fprintf(fp,"%ld\n",t);
m=m+10;
}
}