校园导航系统
后面为测试数据
#include
#include
#include
#include
#include
#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<<"**********欢迎使用校园导航系统**********"<>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<<"请依次输入边的两端点以及边的权值:"<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<<"创建成功!"<n; i++)
{
for(j=1; j<=G->n; j++)
{
cout<=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]=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]n; j++)
{
if(s[j]==0)
{
if(g[u][j]0&&dist[u]+g[u][j]G->n)
cout<<"您输入的景点不存在!"<adjlist[v].data<=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>ch>>v1>>v2;
if(v1==v2)
{
if(ch=='Q')
cout<<"Y"<>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<<"此两个景点之间没有路径!"<>v1;
delete_vertex(&Create,v1);
cout<<"删除成功!"<>v1>>v2;
delete_edge(&Create,v1,v2);
cout<<"删除成功!"<>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<<"此两个景点之间没有路径!"<