bfs遍历图模板伪代码:

 bfs(u){            //遍历u所在的连通块
queue q;
//将u入队
inq[u] = true;
while (q非空){
//取出q的队首元素u进行访问
for (从u出发可达的所有的顶点v){
if (inq[v] == false){ //如果v未曾加入过队列
//将v入队;
inq[v] = true;
}
}
}
} BFSTraversal(G){ //遍历图G
for (G的所有顶点u){
if (inq[u] == false){
BFS(u);
}
}
}

邻接矩阵版:

 const int MAXV = ;

 const int INF = ;

 //邻接矩阵版
int n, G[MAXV][MAXV]; //n为顶点数,MAXV为最大顶点数
bool inq[MAXV] = { false };
void bfs(int u){ //遍历u所在的连通块
queue<int> q; //定义队列q
q.push(u); //将初识点u入队
inq[u] = true; //设置u已经被加入过队列
while (!q.empty()){ //只要队列非空
int u = q.front(); //取出队首元素
q.pop(); //将队首元素出队
for (int v = ; v < n; v++){
if (inq[v] == false && G[u][v] != INF){ //如果u的邻接点v未曾入过队列
q.push(v);
inq[v] = true;
}
}
}
} void BFSTraversal(){ //遍历图G
for (int u = ; u < n; u++){ //枚举所有顶点
if (inq[u] == false){ //如果u未曾加入过队列
bfs(u); //遍历u所在的连通块
}
}
}

邻接表版(顶点类型为非结构体):

 vector<int> Adj[MAXV];
int n;
bool inq[MAXV] = { false };
void bfs(int u){
queue<int> q;
q.push(u);
inq[u] = true;
while (!q.empty()){
int u = q.front(); ///取出队首元素
q.pop(); //将队首元素出队
for (int i = ; i < Adj[u].size(); i++){
int v = Adj[u][i];
if (inq[v] = false){
q.push(v); //将v入队
inq[v] = true; //标记v为已经被加入过的队列
}
}
} } void BFSTraversal(){
for (int u = ; u < n; u++){
if (inq[u] = false){
bfs(u);
}
}
}

邻接表版(顶点类型为结构体):

vector<Node> Adj[MAXV];
int n;
bool inq[MAXV] = { false };
void bfs(int u){
queue<Node> q;
Node start;
start.v = u, start.w = , start.layer = ;
q.push(start);
inq[u] = true;
while (!q.empty()){
Node topNode = q.front(); ///取出队首元素
q.pop(); //将队首元素出队
for (int i = ; i < Adj[u].size(); i++){
Node node = Adj[u][i];
node.layer = topNode.layer + ;
if (inq[node.v] = false){
q.push(node); //将v入队
inq[node.v] = true; //标记v为已经被加入过的队列
}
}
} } void BFSTraversal(){
for (int u = ; u < n; u++){
if (inq[u] = false){
bfs(u);
}
}
}

注意:当顶点的属性不只一种或者边权的意义不只一种时,如顶点的属性除了“当前点所拥有的的资源量”还可能有 “当前点在图中的层次”,如边权除了“距离”这一意义还有“花费”属性,而用不同的存储图的方式一般用不同的方式处理这些多出来的属性,如果采用邻接矩阵的方式存储图:一般用增加一维数组和二维数组来应对点属性和边权意义的增加,而如果采用邻接表的方式存储图,则一般采用定义一个结构体,在结构体中增加需要的点属性和边权属性。

题型实战:

              1076 Forwards on Weibo (30分)

Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can view and forward his/her post, which can then be forwarded again by their followers. Now given a social network, you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤1000), the number of users; and L (≤6), the number of levels of indirect followers that are counted. Hence it is assumed that all the users are numbered from 1 to N. Then N lines follow, each in the format:

M[i] user_list[i]

where M[i] (≤100) is the total number of people that user[i] follows; and user_list[i] is a list of the M[i] users that followed by user[i]. It is guaranteed that no one can follow oneself. All the numbers are separated by a space.

Then finally a positive K is given, followed by K UserID's for query.

Output Specification:

For each UserID, you are supposed to print in one line the maximum potential amount of forwards this user can trigger, assuming that everyone who can view the initial post will forward it once, and that only L levels of indirect followers are counted.

Sample Input:

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

Sample Output:

4
5

题目大意要求:以某点开始,统计它L层以内所有点的个数

代码:

 #include <stdio.h>
#include <queue>
#include <vector>
#include <string.h>
using namespace std; // 邻接矩阵版
const int maxv = ; int n, G[maxv][maxv] = { }; // n 为顶点数
bool inq[maxv] = { false }; // 如果对应下标的值为true, 则表示i已经被访问过了
int l, k; // 层数和查询数量 //struct Node{
// int v, layer;
//}; int layer[maxv] = { }; int BFS(int u){
int ans = ;
queue<int> q;
layer[u] = ;
q.push(u);
inq[u] = true;
while (!q.empty()){
int top = q.front();
q.pop();
for (int v = ; v <= n; v++){
if (G[top][v] != && inq[v] == false && layer[top] < ){
layer[v] = layer[top] + ;
inq[v] = true;
q.push(v);
ans++;
}
}
}
return ans;
} int main()
{
// 输入数据
// freopen("in.txt", "r", stdin);
scanf("%d %d", &n, &l);
int n2;
for (int v = ; v <= n; v++){
// 有向图,且逆着存储数据
scanf("%d", &n2);
int u;
for (int j = ; j < n2; j++){
scanf("%d", &u);
G[u][v] = ;
} } // 从不同的起点开始遍历图,返回一个点赞量
scanf("%d", &k);
for (int i = ; i < k; i++){
// 将inq数组初始化
memset(inq, false, sizeof(inq));
memset(layer, , sizeof(layer));
int u;
scanf("%d", &u);
int maxForwards = BFS(u);
printf("%d\n", maxForwards);
} // fclose(stdin);
return ;
}

图的bfs遍历模板(邻接矩阵存储和邻接表存储)的更多相关文章

  1. PTA 邻接表存储图的广度优先遍历(20 分)

    6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...

  2. PTA 邻接表存储图的广度优先遍历

    试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ) 其中LGraph是邻接表存储的 ...

  3. 数据结构(11) -- 邻接表存储图的DFS和BFS

    /////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...

  4. 邻接表存储图,DFS遍历图的java代码实现

    import java.util.*; public class Main{ static int MAX_VERTEXNUM = 100; static int [] visited = new i ...

  5. 数据结构之---C语言实现图的邻接表存储表示

    // 图的数组(邻接矩阵)存储表示 #include <stdio.h> #include <stdlib.h> #include <string.h> #defi ...

  6. 图->存储结构->邻接表

    文字描述 邻接表是图的一种链式存储结构.在邻接表中,对图中每个顶点建立一个单链表,第i个单链表的结点表示依附顶点vi的边(对有向图是指以顶点vi为尾的弧).单链表中的每个结点由3个域组成,其中邻接点域 ...

  7. 图的邻接表存储表示(C)

    //---------图的邻接表存储表示------- #include<stdio.h> #include<stdlib.h> #define MAX_VERTEXT_NUM ...

  8. 图的邻接表存储 c实现

    图的邻接表存储 c实现 (转载) 用到的数据结构是 一个是顶点表,包括顶点和指向下一个邻接点的指针 一个是边表, 数据结构跟顶点不同,存储的是顶点的序号,和指向下一个的指针 刚开始的时候把顶点表初始化 ...

  9. DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储

    题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...

随机推荐

  1. puthon 字典的 .update() 方法

    1.可以直接更新字典 2.也可以用等号连接,更新一个可迭代对象.

  2. geoserver发布地图服务

    1.      Geoserver启动 blog.csdn.net 2014-09-18 20:30 Geoserver是著名的开源GIS软件之一.也是项目中常用的地图服务软件.基于geoserver ...

  3. Selenium3+python自动化014-自动化测试框架的作用

    1.能够有效组织和管理测试脚本 2.进行数据驱动或者关键字驱动的测试 3.将基础的测试代码进行封装,降低测试脚本编写的复杂性和重复性 4.提高测试脚本维护和修改的效率 5.自动执行测试脚本,并自动发布 ...

  4. 0级搭建类004-中标麒麟 Linux 安装 (V7.0) 公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  5. python 集合运算交集&并集&差集

    差集>>> #两个列表的差集3 >>> ret3 = list(set(a) ^ set(b)) #两个列表的差集 >>> ret4=list(s ...

  6. C++中public、protected、private的区别(转载)

    转载:https://blog.csdn.net/vanturman/article/details/79393317   首先记住一点:派生类能且只能访问基类的public和protected成员! ...

  7. CSS之 元素显示隐藏,用户界面样式,文本溢出隐藏,精灵技术,三角形

    元素的显示与隐藏 display 显示 display 设置或检索对象是否及如何显示 display: none; 隐藏对象 display: block; 除了转换为块级元素, 同时还有显示元素的意 ...

  8. PDF体检报告

    //Title: 个人健康管理分析报告 using System; using System.Collections.Generic; using System.Linq; using System. ...

  9. Educational Codeforces Round 81 (Rated for Div. 2) C. Obtain The String

    题目链接:http://codeforces.com/contest/1295/problem/C 题目:给定字符串s,t.  给定一个空串z,需要按照规则把z构造成 string z == stri ...

  10. MongoDB批量更新和批量插入的方式

    最近,在调试代码中发现向MongoDB插入或者更新文档记录时若是多条的话都是采用for循环操作的,这样的处理方式会造成数据操作耗时,不符合批量处理的原则:对此,个人整理了一下有关MongoDB的批量更 ...