#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=8;
int mapp[N][N];
int path[N][N];
int short0[N][N];
void path0(int n);
void path_foot();
void path_car();
int judge();
void floyd();
void se(int x,int y);
void all_road(int a,int b,int c);
void menu1()
{
int n;
while (1)
{
scanf("%d",&n);
if(n==1)
path0(n);
if (n==2)
path0(n);
else if (n==3)
path0(n);
else if (n==5)
{
system("cls");
exit(0);
}
else
printf("\t输入编号无效,请重新输入:");
}
}
void path_foot()
{
memset(mapp,M,sizeof(mapp));
for (int i=0; i<=N; i++)
mapp[i][i]=0;
mapp[1][2]=mapp[2][1]=100;
mapp[1][3]=mapp[3][1]=200;
mapp[1][6]=mapp[6][1]=60;
mapp[2][3]=mapp[3][2]=150;
mapp[2][4]=mapp[4][2]=70;
mapp[2][5]=mapp[5][2]=70;
mapp[3][5]=mapp[5][3]=100;
mapp[3][7]=mapp[7][3]=110;
mapp[4][5]=mapp[5][4]=70;
mapp[4][7]=mapp[7][4]=80;
}
void floyd()
{
memset(short0,0,sizeof short0);
for (int i=1; i<=N; i++)
{
for (int j=1; j<=N; j++)
path[i][j]=mapp[i][j];
}
for (int k=1; k<=N; k++)
for (int i=1; i<=N; i++)
for (int j=1; j<=N; j++)
if (path[i][j]>path[i][k]+path[k][j])
{
path[i][j]=path[i][k]+path[k][j];
short0[i][j]=k;
short0[j][i]=k;
}
}
void se(int x,int y)
{
int a=x,b=y;
printf("\t查询的最短路径是;\n");
if (path[x][y]!=inf)
{
if(x<y)
{
printf("\t%d",b);
while (short0[x][y])
{
printf("<-%d",short0[x][y]);
if (x<y)
y=short0[x][y];
else
x=short0[y][x];
}
printf("<-%d\n\n",a);
}
else
{
printf("-->%d",a);
while (short0[x][y])
{
printf("->%d",short0[x][y]);
if (x<y)
y=short0[x][y];
else
x=short0[y][x];
}
printf("->%d\n\n",b);
}
printf("\t%d到%d的最短距离为:%d",a,b,path[a][b]);
}
else
printf("\t%d和%d之间不存在通路!",a,b);
}
int ans;
int stack0[20];
int record[20];
int v[15];
int top;
int max0;
void all_road(int a,int b,int c)
{
if (top<0)
return ;
for (int i=1; i<=N; i++)
{
if (!v[i]&&mapp[a][i]>0&&mapp[a][i]<M)
{
stack0[top++]=i;
stack0[0]+=mapp[a][i];
v[i]=1;
if (i==b)
{
if ((max0<top)||(max0==top&&stack0[0]<record[0]))
{
max0=top;
for (int j=1; j<top; j++)
record[j]=stack0[j];
record[0]=stack0[0];
}
if (c==2)
{
for (int j=1; j<top; j++)
{
if (j==1)
printf("\t%d",stack0[j]);
else
printf("-->%d",stack0[j]);
}
printf("\n");
ans++;
}
}
else
all_road(i,b,c);
top--;
stack0[0]-=mapp[a][i];
v[i]=0;
}
}
}
void path0(int m)
{
char ju;
int x,y;
path_foot();;
while (1)
{
printf("\t请输入将要查询的两个景点的编号(x y);");
while (1)
{
scanf("%d %d",&x,&y);
getchar();
if ((x<1||x>7)||(y<1||y>7))
printf("\t输入的景点编号无效请重新输入:");
else break;
}
if (m==1)
{
floyd();
se(x,y);
}
else if (m==2)
{
memset(v,0,sizeof(v));
v[x]=1;
top=2;
stack0[1]=x;
ans=0;
all_road(x,y,2);
printf("\t景点 %d 到景点 %d 之间存在 %d 条线路\n",x,y,ans);
}
else if (m==3)
{
memset(v,0,sizeof(v));
v[x]=1;
top=2;
stack0[1]=x;
max0=0;
all_road(x,y,3);
printf("\t经过景点最多中最短的路径的线路为:\n");
for (int j=1; j<max0; j++)
{
if (j==1)
printf("\t%d",record[j]);
else
printf("-->%d",record[j]);
}
printf("\n\t此路径长度为:%d\n",record[0]);
floyd();
se(x,y);
}
printf("\n\t是否继续查询,继续请输入Y或者y:");
scanf("%c",&ju);
if (ju=='Y'||ju=='y')
continue;
else
menu1();
}
return ;
}
int main()
{
menu1();
return 0;
}