校园导航系统
后面为测试数据
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#define Max 1002//最大节点数
#define INF 6666666
using namespace std;
bool mark;
bool visited[Max];
bool a[Max][Max];//两节点是否连通
int g[Max][Max];//存放图
int dist[Max];
string str[105]=
{
{"景点1:八景圆"},
{"景点2:九隆广场"},
{"景点3:青春广场"},
{"景点4:玉带桥"}, n
{"景点5:千米长廊"},
{"景点6:三元湖"},
{"景点7:科技馆"},
{"景点8:文科馆"},
{"景点9:综合楼花园"}
};
typedef struct node
{
int adjvex;//边的终点
int weight;//该边的权值
struct node *next;
} Edge; //边节点类型
typedef struct vnode
{
string data;//顶点信息
Edge *firstedge;
} Vertex; //表头节点
typedef Vertex Adjlist[Max];//邻接表类型
typedef struct ALGraph
{
Adjlist adjlist;//邻接表
int n,e;//图中顶点数n和边数e
} Graphic;
int N,E;//图中顶点数n和边数e
Graphic Create;//图
int s[Max];
int menu()
{
cout<<"**********欢迎使用校园导航系统**********"<<endl;
cout<<" 1.创建景点构成的无向图 "<<endl;
cout<<" 2.用邻接矩阵显示无向图 "<<endl;
cout<<" 3.查看某个景点详细信息 "<<endl;
cout<<" 4.查询两景点间最短路径 "<<endl;
// cout<<" 5.删除某一个景点 "<<endl;
// cout<<" 6.删除某两个景点间的边 "<<endl;
cout<<" 5.查看一个景点到其他景点的所有路线"<<endl;
cout<<" 6.退出该程序 "<<endl;
cout<<"请输入要进行的功能服务编号(1~8): "<<endl;
int num;
cin>>num;
return num;
}
void dfs(Graphic *G,int v1,int v2)
{
Edge *s;
s=G->adjlist[v1].firstedge;
visited[v1]=1;
if(a[v1][v2]==1)
mark=1;
while(s)
{
if(mark)return ;
if(visited[s->adjvex]==0)
dfs(G,s->adjvex,v2);
s=s->next;
}
}
/*void delete_vertex(Graphic *G,int v)//删除景点
{
Edge *s,*t,*r;
s=G->adjlist[v].firstedge;
while(s)
{
int u=s->adjvex;
bool f=false;
a[v][u]=a[u][v]=0;
g[v][u]=INF;
g[u][v]=INF;
t=s;
if(t->adjvex==u)
{
f=true;
G->adjlist[v].firstedge=t->next;
r=G->adjlist[u].firstedge;
if(r->adjvex==v)
{
G->adjlist[u].firstedge=r->next;
}
else
{
while(r)
{
if(r->next->adjvex==v)
{
r->next=r->next->next;
break;
}
else
r=r->next;
}
}
}
if(!f)
{
while(t)
{
if(t->next->adjvex==u)
{
t->next=t->next->next;
break;
}
else
t=t->next;
}
}
s=s->next;
}
G->adjlist[v].firstedge=NULL;
}
void delete_edge(Graphic *G,int v1,int v2)//删除边
{
Edge *s;
a[v1][v2]=a[v2][v1]=0;
g[v1][v2]=INF;
g[v2][v1]=INF;
s=G->adjlist[v1].firstedge;
if(s->adjvex==v2)
{
G->adjlist[v1].firstedge=s->next;
goto k;
}
while(s)
{
if(s->next->adjvex==v2)
{
s->next=s->next->next;
break;
}
else
s=s->next;
}
k:
;
s=G->adjlist[v2].firstedge;
if(s->adjvex==v1)
{
G->adjlist[v2].firstedge=s->next;
return ;
}
while(s)
{
if(s->next->adjvex==v1)
{
s->next=s->next->next;
break;
}
else
s=s->next;
}
}*/
void insert_edge(Graphic *G,int v1,int v2,int cost)//头插法
{
Edge *s;
a[v1][v2]=a[v2][v1]=1;
g[v1][v2]=g[v2][v1]=cost;
G->adjlist[v1].data=str[v1-1];
G->adjlist[v2].data=str[v2-1];
s=(Edge *)malloc(sizeof(Edge));
s->adjvex=v1;
s->weight=cost;
s->next=G->adjlist[v2].firstedge;
G->adjlist[v2].firstedge=s;
s=(Edge *)malloc(sizeof(Edge));
s->adjvex=v2;
s->weight=cost;
s->next=G->adjlist[v1].firstedge;
G->adjlist[v1].firstedge=s;
}
void CreateGraphic(Graphic *G)
{
cout<<"请输入景点数量:";
cin>>N;
G->n=N;
cout<<"请输入景点之间边的数量:";
cin>>E;
G->e=E;
cout<<"请依次输入边的两端点以及边的权值:"<<endl;
int i,j;
int v1,v2,cost;
for(i=1; i<=G->n; i++)
G->adjlist[i].firstedge=NULL;
for(i=1; i<=G->n; i++)
for(j=1; j<=G->n; j++)
a[i][j]=0;
for(i=1; i<=G->n; i++)
{
for(j=1; j<=G->n; j++)
{
g[i][j]=INF;
}
}
for(i=1; i<=G->e; i++)
{
cin>>v1>>v2>>cost;
insert_edge(G,v1,v2,cost);
}
cout<<"创建成功!"<<endl;
}
void show(Graphic *G)
{
int i,j;
for(i=1; i<=G->n; i++)
{
for(j=1; j<=G->n; j++)
{
cout<<setw(8)<<g[i][j];
}
cout<<endl;
}
}
void Dispath(Graphic *G,int path[],int v,int r)
{
int j,k;
int apath[Max+1],d;
if(s[r]==1&&r!=v)
{
printf("从顶点%d到顶点%d的最短路径长度为:%d\t路径为:",v,r,dist[r]);
d=0;
apath[d]=r;
k=path[r];
if(k==-1)
printf("无路径\n");
else
{
while(k!=v)
{
d++;
apath[d]=k;
k=path[k];
}
d++;
apath[d]=v;
for(j=d; j>=0; j--)
{
printf("%d",apath[j]);
if(j!=0)
printf("->");
}
printf("\n");
}
}
}
void Dijkstra(Graphic *G,int v,int r)
{
int path[Max];
int mindis,i,j,u;
for(i=1; i<=G->n; i++)
dist[i]=INF;
for(i=1; i<=G->n; i++)
{
dist[i]=g[v][i];//距离初始化
s[i]=0;//s[]置空
if(g[v][i]<INF&&g[v][i]>=0)
path[i]=v;
else if(g[v][i]==INF)
path[i]=-1;
}
s[v]=1;
path[v]=v;
for(i=1; i<=G->n; i++)
{
mindis=INF;
for(j=1; j<=G->n; j++)
{
if(s[j]==0&&dist[j]<mindis)
{
u=j;
mindis=dist[j];
}
}
s[u]=1;
for(j=1; j<=G->n; j++)
{
if(s[j]==0)
{
if(g[u][j]<INF&&g[u][j]>0&&dist[u]+g[u][j]<dist[j])
{
dist[j]=dist[u]+g[u][j];
path[j]=u;
}
}
}
}
Dispath(G,path,v,r);
}
void SHOW(Graphic *G,int v)
{
if(v<1||v>G->n)
cout<<"您输入的景点不存在!"<<endl;
else
cout<<G->adjlist[v].data<<endl;
}
void FindPath(Graphic *G,int u,int v,int path[],int d)
{
int w,i;
Edge *p;
d++;
path[d]=u;
visited[u]=1;
if(u==v&&d>=1)
{
for(i=0; i<=d; i++)
{
printf("%d",path[i]);
if(i!=d)
printf("->");
}
printf("\n");
}
p=G->adjlist[u].firstedge;
while(p!=NULL)
{
w=p->adjvex;
if(visited[w]==0)
FindPath(G,w,v,path,d);
p=p->next;
}
visited[u]=0;
}
/*void CreateGraphic(Graphic *G)
{
char ch;
int n,q;//节点数,指令数
int v1,v2;//边
int i,j,k;
cin>>n>>q;
for(i=0;i<=n;i++)
G->adjlist[i].firstedge=NULL;
for(i=0;i<=n;i++)
for(j=0;j<n;j++)
a[i][j]=0;
for(i=0;i<q;i++)
{
getchar();
cin>>ch>>v1>>v2;
if(v1==v2)
{
if(ch=='Q')
cout<<"Y"<<endl;
continue;
}
switch(ch)
{
case 'Q':
mark=0;
memset(visited,0,sizeof(visited));
dfs(G,v1,v2);
if(mark)
cout<<"Y"<<endl;
else
cout<<"N"<<endl;
break;
case 'D':
delete_vex(G,v1,v2);
break;
case 'I':
insert_vex(G,v1,v2);
break;
}
}
}*/
int main()
{
int Case=menu();
int v1,v2;
int path[105];
while(true)
{
switch(Case)
{
case 1:
CreateGraphic(&Create);
system("pause");
system("cls");
Case=menu();
break;
case 2:
show(&Create);
system("pause");
system("cls");
Case=menu();
break;
case 3:
cout<<"请输入要查询的景点的编号:"<<endl;
cin>>v1;
SHOW(&Create,v1);
system("pause");
system("cls");
Case=menu();
break;
case 4:
cout<<"请输入两景点的编号:";
cin>>v1>>v2;
mark=0;
memset(visited,0,sizeof(visited));
dfs(&Create,v1,v2);
if(mark)
{
Dijkstra(&Create,v1,v2);
}
else
cout<<"此两个景点之间没有路径!"<<endl;
system("pause");
system("cls");
Case=menu();
break;
/* case 5:
cout<<"请输入要删除的景点的编号:";
cin>>v1;
delete_vertex(&Create,v1);
cout<<"删除成功!"<<endl;
system("pause");
system("cls");
Case=menu();
break;
case 6:
cout<<"请输入要删除的边所附带的两景点的编号:";
cin>>v1>>v2;
delete_edge(&Create,v1,v2);
cout<<"删除成功!"<<endl;
system("pause");
system("cls");
Case=menu();
break;*/
case 5:
cout<<"请输入要查询的路径的两景点的编号:";
cin>>v1>>v2;
mark=0;
memset(visited,0,sizeof(visited));
dfs(&Create,v1,v2);
if(mark)
{
memset(visited,0,sizeof(visited));
FindPath(&Create,v1,v2,path,-1);
}
else
cout<<"此两个景点之间没有路径!"<<endl;
system("pause");
system("cls");
Case=menu();
break;
case 6:
system("cls");
cout<<endl<<endl<<endl;
cout<<" 谢谢您的使用,再见!"<<endl<<endl<<endl;
return 0;
default:
cout<<"您输入的功能编号不合法请重新输入!"<<endl;
system("pause");
system("cls");
Case=menu();
break;
}
}
return 0;
}
/*
1 5 100
5 2 200
2 3 50
1 3 100
4 9 200
9 8 700
3 8 500
1 4 200
9 6 500
8 7 100
7 6 100
0 0 0
*/
/*
1 5 100
5 2 200
2 3 50
1 3 100
1 4 200
9 8 700
9 6 500
8 7 100
7 6 100
0 0 0*/
/*
1 5 100
5 2 200
2 3 50
1 3 100
3 8 500
1 4 200
4 9 200
9 8 700
8 7 100
0 0 0*/