c.arcs[j][i].adj=c.arcs[i][j].adj;
FILE*pFile;
pFile=fopen("myfile.txt","w");
fwrite(c.vexs[0].name,2,3,pFile);
fwrite(c.vexs[0].introduction,2,11,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[1].name,2,6,pFile);
fwrite(c.vexs[1].introduction,2,12,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[2].name,2,5,pFile);
fwrite(c.vexs[2].introduction,2,15,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[3].name,2,5,pFile);
fwrite(c.vexs[3].introduction,2,10,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[4].name,2,4,pFile);
fwrite(c.vexs[4].introduction,2,11,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[5].name,2,5,pFile);
fwrite(c.vexs[5].introduction,2,10,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[6].name,2,4,pFile);
fwrite(c.vexs[6].introduction,2,8,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[7].name,2,5,pFile);
fwrite(c.vexs[7].introduction,2,11,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[8].name,2,6,pFile);
fwrite(c.vexs[8].introduction,2,6,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[9].name,2,4,pFile);
fwrite(c.vexs[9].introduction,2,17,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[10].name,2,5,pFile);
fwrite(c.vexs[10].introduction,2,11,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[11].name,2,6,pFile);
fwrite(c.vexs[11].introduction,2,5,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[12].name,2,4,pFile);
fwrite(c.vexs[12].introduction,2,10,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[13].name,2,4,pFile);
fwrite(c.vexs[13].introduction,2,6,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[14].name,2,7,pFile);
fwrite(c.vexs[14].introduction,2,9,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[15].name,2,4,pFile);
fwrite(c.vexs[15].introduction,2,7,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[16].name,2,10,pFile);
fwrite(c.vexs[16].introduction,2,6,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[17].name,2,4,pFile);
fwrite(c.vexs[17].introduction,2,7,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[18].name,2,5,pFile);
fwrite(c.vexs[18].introduction,2,14,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[19].name,2,4,pFile);
fwrite(c.vexs[19].introduction,2,6,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[20].name,2,8,pFile);
fwrite(c.vexs[20].introduction,2,6,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[21].name,2,8,pFile);
fwrite(c.vexs[21].introduction,2,6,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[22].name,2,5,pFile);
fwrite(c.vexs[22].introduction,2,9,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[23].name,2,12,pFile);
fwrite(c.vexs[23].introduction,2,11,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[24].name,2,7,pFile);
fwrite(c.vexs[24].introduction,2,6,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[25].name,2,7,pFile);
fwrite(c.vexs[25].introduction,2,7,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[26].name,2,4,pFile);
fwrite(c.vexs[26].introduction,2,6,pFile);
fwrite("\r\n",2,1,pFile);
fwrite(c.vexs[27].name,2,4,pFile);
fwrite(c.vexs[27].introduction,2,6,pFile);
fwrite("\r\n",2,1,pFile);
fclose(pFile);
returnc;
}//initgraph
//
(2)查找景点在图中的序号
intlocatevex(mgraphc,intv)
{
inti;
for(i=0;iif(v==c.vexs[i].position)
returni;//找到,返回顶点序号i
return-1;//否则,返回-1
}
//(3)、(4)求两景点间的所有路径
//(3)打印序号为m,n景点间的长度不超过8个景点的路径
voidpath(mgraphc,intm,intn,intk)
{
ints,x=0;
intt=k+1;//t记载路径上下一个中间顶点在d[]数组中的下标
if(d[k]==n&&k<8)//d[k]存储路径顶点。
若d[k]是终点n且景点个数<8,则输出该路径
{//递归出口,找到一条路径
for(s=0;sprintf("%s--->",c.vexs[d[s]].name);//输出该路径。
s=0时为起点m
printf("%s",c.vexs[d[s]].name);//输出最后一个景点名(即顶点n的名字,此时s==k)
printf("\n\n");
}
else
{
s=0;
while(s{
if((c.arcs[d[k]][s].adj顶点m到顶点s有边,且未被访问
{
visited[s]=1;
d[k+1]=s;//存储顶点编号s至d[k+1]中
path(c,m,n,t);//求从下标为t=k+1的第d[t]个顶点开始的路径(递归调用),同时打印出一条m至n的路径
visited[s]=0;//将找到的路径上顶点的访问标志重新设置为0,以用于试探新的路径
}
s++;//试探从下一个顶点s开始是否有到终点的路径
}//endwhile
}//endelse
}//endpath
//(4)打印两景点间的景点个数不超过8的所有路径。
调用(3)
intallpath(mgraphc)
{
intk,i,j,m,n;
printf("\n\n请输入你要查询的两个景点编号:
\n\n");
scanf("%d%d",&i,&j);
printf("\n\n");
m=locatevex(c,i);//调用
(2),确定该顶点是否存在。
若存在,返回该顶点编号
n=locatevex(c,j);
d[0]=m;//存储路径起点m(intd[]数组是全局变量)
for(k=0;kvisited[k]=0;
visited[m]=1;//第m个顶点访问标志设置为1
path(c,m,n,0);//调用(3)。
k=0,对应起点d[0]==m。
k为d[]数组下标
return1;
}
//(5)用迪杰斯特拉算法,求出一个景点到其他景点间的最短路径,并打印
voidshortestpath_dij(mgraphc)
{
//迪杰斯特拉算法,求从顶点v0到其余顶点的最短路经及其带权长度d[v]
//若p[v][w]为1,则w是从v0到v的最短路经上的顶点
//final[v]类型用于设置访问标志
intv,w,i,min,t=0,x,flag=1,v0;//vo为起始景点的编号
intfinal[35],d[35],p[35][35];
printf("\n请输入一个起始景点的编号:
");
scanf("%d",&v0);
printf("\n\n");
while(v0<0||v0>c.vexnum)
{
printf("\n你所输入的景点编号不存在\n");
printf("请重新输入:
");
scanf("%d",&v0);
}//while
for(v=0;v{
final[v]=0;//初始化各顶点访问标志
d[v]=c.arcs[v0][v]