标签:index 数据结构 problem slist status creat use res 空格
1 6 7 0 0 3 0 4 1 4 1 5 2 3 2 4 3 5
0 3 4 2 5 1
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