数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

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:

邻接矩阵表示图的广度优先搜索,借助队列实现。

 #include <iostream>
 #include <cstdio>
 #include <queue>
 #include <climits>

 using namespace std;

 #define MVN 110

 typedef struct AdjMatrix
 {
     int w;
     char *info;
 }AM;

 typedef struct MGraph
 {
     int vex[MVN];
     AM arc[MVN][MVN];
     int vexnum,arcnum;
     int k;
 }MG;

 void creat(MG &G)
 {
     int i,j,k;
     ;k<G.vexnum;k++)
     {
         G.vex[k]=k;
     }
     ;k<G.arcnum;k++)
     {
         scanf("%d %d",&i,&j);
         G.arc[i][j].w=G.arc[j][i].w=;
     }
 }

 void BFS(MG &G)
 {
     queue <int> Q;
     int i,k;
     bool visited[MVN]={false};
     ;k<G.vexnum;k++)
     {
         if(G.vex[k]==G.k)
             break;
     }
     Q.push(k);
     while(!Q.empty())
     {
         i=Q.front();
         Q.pop();
         if(!visited[i])
         {
             if(i==G.k)
                 printf("%d",G.vex[i]);
             else
                 printf(" %d",G.vex[i]);
             visited[i]=true;
             ;k<G.vexnum;k++)
             {
                 )
                     Q.push(k);
             }
         }
     }
     printf("\n");
 }

 int main()
 {
     int t;
     scanf("%d",&t);
     while(t--)
     {
         MG G={};
         scanf("%d %d %d",&G.vexnum,&G.arcnum,&G.k);
         creat(G);
         BFS(G);
     }
     ;
 }

 /***************************************************
 User name: ***
 Result: Accepted
 Take time: 0ms
 Take Memory: 344KB
 Submit time: 2016-11-13 18:54:57
 ****************************************************/

SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历的更多相关文章

  1. 数据结构之 图论---基于邻接矩阵的广度优先搜索遍历(输出bfs遍历序列)

    数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜索( ...

  2. SDUT-2124_基于邻接矩阵的广度优先搜索遍历

    数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一个无向连通图 ...

  3. 数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历 (SDUT 2141)

    #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...

  4. SDUT2141数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186 #include<cstdio> #include& ...

  5. 基于邻接矩阵的广度优先搜索遍历(BFS)

    题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186 #include<stdio.h> #incl ...

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

    数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Descript ...

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

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...

  8. SDUT OJ 3403 数据结构实验之排序六:希尔排序

    数据结构实验之排序六:希尔排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  9. SDUT OJ 1479 数据结构实验之栈:行编辑器

    数据结构实验之栈:行编辑器 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  一个简单的行编辑程序的功能是:接受用户从终端输入的程 ...

随机推荐

  1. liveusb-creator

    liveusb-creator The liveusb-creator is a cross-platform tool for easily installing live operating sy ...

  2. Puppet Agent/Master HTTPS Communications

    The agent/master HTTP interface is REST-like, but varies from strictly RESTful design in several way ...

  3. svn 同步脚本

    REPOS="$1"REV="$2"export LANG=en_US.UTF-8/usr/bin/svn update /home/wwwroot/yswif ...

  4. 关于snprintf的一个warning

    在编一段代码时用到snprintf,有个很奇怪的warning 编译提示: warning C4013: 'snprintf' undefined; assuming extern returning ...

  5. 回到顶部缓动效果代码 --- tween动画函数库

    function animateGoTop() { var top = $(document).scrollTop(); var end = 0; var dur = 500; var t = 0; ...

  6. 只有一个Service或Broadcast Reciver的android应用

    Service是android四大组件中与Activity最相似的组件,都可以代表可执行的程序. Service与Activity的区别在于:   (1).Service一直在后台运行,没有用户界面. ...

  7. Platform Invoke

    PInvoke 允许managed code 来调用在DLL中实施的unmanged function. Platform invoke relies on metadata to locate ex ...

  8. [Serializable]的应用--注册码的生成,加密和验证

    1.首先定义注册类RegisterEntity [Serializable] public class RegisterEntity { public string RegisterKey; publ ...

  9. 有关C,C++,C#, Java的图形图像处理类库 整理(未完待续)

    1.Java相关 1.1 Jzy3D Jzy3D 是一个Java的类库,用来绘制各种各样的三维图形,如下图所示: 下载地址:jzy3d-api,官网 1.2 Proscene 是一个用于创建交互式3D ...

  10. 为何你的php代码没有写结束标签

    PHP闭合标签"?>"在PHP中对PHP的分析器是可选的.但是,如果使用闭合标签,任何由开发者,用户, 或者FTP应用程序插入闭合标签后面的空格都有可能会引起多余的输出.ph ...