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. cocos studio 中交互性

    cocos studio 属性->常规->交互性,勾选之后相当于代码中的setTouchEnabled(true)

  2. poj1042题解

    h [1,16] hours all_v=h*12 intervals n [2,25] lakes fi inital intervals fi-di*v v [0,all_v) 题意,做每件事情的 ...

  3. 843. n-皇后问题(dfs+输出各种情况)

    n-皇后问题是指将 n 个皇后放在 n∗n 的国际象棋棋盘上,使得皇后不能相互攻击到,即任意两个皇后都不能处于同一行.同一列或同一斜线上. 现在给定整数n,请你输出所有的满足条件的棋子摆法. 输入格式 ...

  4. ArrayList、LinkedList区别(jdk8)

    /** * jdk8 * ArrayList:底层动态数组实现(未初始化指定数组长度) * add():添加元素时,才初始化数组长度为10.容量不够时,动态扩容策略为: 原容量 + 原容量*0.5 * ...

  5. [USACO17DEC] Barn Painting - 树形dp

    设\(f[i][j]\)为\(i\)子树,当\(i\)为\(j\)时的方案数 #include <bits/stdc++.h> using namespace std; #define i ...

  6. springboot 扫描不到包 @SpringBootApplication 自动配置原理

    解决方案 在main类中增加注解 @ComponentScan("com.test.test.*") 扫描具体的包 @ComponentScan(basePackages = {& ...

  7. IntelliJ IDEA 2019.1.3 最新破解教程【最强 可用至2099年】

    本文包括最新[2019.1.2]安装 和[2018.3.2](推荐)安装 ①IntelliJ IDEA 2018.3.2安装永久安装[最强]  一. 在官网下载IDEA安装包  链接:https:// ...

  8. SIFT算法原理(3)-确定关键点的主方位,构建关键点描述符

    介绍官网:https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_sift_intro/py_sift_intro.html ...

  9. 顶级Kagglers的心得和技巧

    这是一篇笔记,课程来自Coursera上的How to Win a Data Science Competition: Learn from Top Kagglers 本篇可以算是顶级Kaggler大 ...

  10. python 多版本环境

    参考 https://www.cnblogs.com/---JoyceLiuHome/articles/7852871.html 安装 Anaconda集成化环境 https://www.anacon ...