码迷,mamicode.com
首页 > 其他好文 > 详细

SDUT 2142 【TEST】数据结构实验之图论二:基于邻接表的广度优先搜索遍历

时间:2016-11-06 02:22:02      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:index   数据结构   problem   slist   status   creat   use   res   空格   

数据结构实验之图论二:基于邻接表的广度优先搜索遍历

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索(BFS)遍历,输出从某个顶点出发的遍历序列。(同一个结点的同层邻接点,节点编号小的优先遍历)

Input

输入第一行为整数n(0< n <100),表示数据的组数。
对于每组数据,第一行是三个整数k,m,t(0<k<100,0<m<(k-1)*k/2,0< t<k),表示有m条边,k个顶点,t为遍历的起始顶点。 
下面的m行,每行是空格隔开的两个整数u,v,表示一条连接u,v顶点的无向边。

Output

输出有n行,对应n组输出,每行为用空格隔开的k个整数,对应一组数据,表示BFS的遍历结果。

Example Input

1
6 7 0
0 3
0 4
1 4
1 5
2 3
2 4
3 5

Example Output

0 3 4 2 5 1

Hint

用邻接表存储。
 

DQE:

在图的正式实验开始前尝试一下,题目来自练习题,代码作为【尝试练习】可能不完全标准。
本题数据结构较为复杂,过程可能写的有些麻烦,目前仅作尝试
 
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <queue>
  4 
  5 using namespace std;    /*【尝试练习】*/
  6 
  7 #define MVN 110
  8 
  9 typedef struct ArcNode
 10 {
 11     int adji;
 12     ArcNode *nextarc;
 13     char *info;
 14 }AN;
 15 
 16 typedef struct VNode
 17 {
 18     int x;
 19     AN *arcp;
 20 }VN;
 21 
 22 typedef struct ALGraph
 23 {
 24     VN vex[MVN];
 25     int vexnum,arcnum;
 26     int k;
 27 }ALG;
 28 
 29 void creat(ALG &G)
 30 {
 31     int i,j,k;
 32     for(i=0;i<G.vexnum;i++)
 33     {
 34         G.vex[i].x=i;
 35         G.vex[i].arcp=NULL;
 36     }
 37     for(k=0;k<G.arcnum;k++)
 38     {
 39         scanf("%d %d",&i,&j);
 40         AN *an=new AN,*tan=new AN;
 41         an->adji=j;
 42         an->info=NULL;
 43         an->nextarc=NULL;
 44 
 45         tan->adji=i;
 46         tan->info=NULL;
 47         tan->nextarc=NULL;
 48         if(!G.vex[i].arcp)
 49             G.vex[i].arcp=an;
 50         else
 51         {
 52             AN *p=G.vex[i].arcp;
 53             while(p->nextarc)
 54                 p=p->nextarc;
 55             p->nextarc=an;
 56         }
 57 
 58         if(!G.vex[j].arcp)
 59             G.vex[j].arcp=tan;
 60         else
 61         {
 62             AN *p=G.vex[j].arcp;
 63             while(p->nextarc)
 64                 p=p->nextarc;
 65             p->nextarc=tan;
 66         }
 67     }
 68 }
 69 
 70 void BFS(ALG &G)
 71 {
 72     queue <int> Q;
 73     int i,m=0;
 74     int out[MVN];
 75     bool visited[MVN]={false};
 76     for(i=0;i<G.vexnum;i++)
 77     {
 78         if(G.vex[i].x==G.k)
 79             break;
 80     }
 81     Q.push(i);
 82     while(!Q.empty())
 83     {
 84         i=Q.front();
 85         Q.pop();
 86         if(!visited[i])
 87         {
 88             out[m++]=G.vex[i].x;
 89             visited[i]=true;
 90             AN *p=G.vex[i].arcp;
 91             while(p)
 92             {
 93                 Q.push(p->adji);
 94                 p=p->nextarc;
 95             }
 96         }
 97     }
 98     for(i=0;i<G.vexnum-1;i++)
 99     {
100         printf("%d ",out[i]);
101     }
102     printf("%d\n",out[G.vexnum-1]);
103 }
104 
105 int main()
106 {
107     ALG G;
108     int n;
109     scanf("%d",&n);
110     while(n--)
111     {
112         scanf("%d %d %d",&G.vexnum,&G.arcnum,&G.k);
113         creat(G);
114         BFS(G);
115     }
116     return 0;
117 }
118 
119 /***************************************************
120 User name: ***
121 Result: Accepted
122 Take time: 0ms
123 Take Memory: 160KB
124 Submit time: 2016-11-06 00:09:22
125 ****************************************************/

 

SDUT 2142 【TEST】数据结构实验之图论二:基于邻接表的广度优先搜索遍历

标签:index   数据结构   problem   slist   status   creat   use   res   空格   

原文地址:http://www.cnblogs.com/Mimick/p/6034491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!