目录

代码


代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ArcNode{
int to;
struct ArcNode *next;
int w;
}ArcNode;
typedef struct VertexNode
{
int data;
ArcNode *arc;
}VertexNode;
typedef struct Graph
{
int max;
VertexNode *vertex;
}Graph; //这是全局变量
int dream;
int find; //这个是手写的队列
typedef struct QNode{
int data;
struct QNode*next;
}QNode;
typedef struct Queue{
QNode*head, *tail;
}Queue;
Queue *Create_Queue()
{
Queue *Q = (Queue*)malloc(sizeof(Queue));
Q->tail = NULL;
Q->head = NULL;
return Q;
}
int Empty(Queue*Q)
{
if(Q->head)
return 0;
else
return 1;
}
void Push(Queue* Q,int x)
{
QNode*s = (QNode*)malloc(sizeof(QNode));
s->data = x;
s->next = NULL;
if(Empty(Q))
{
Q->head = s;
Q->tail = s;
}
else
{
Q->tail->next = s;
Q->tail = s;
}
}
int Top(Queue*Q)
{
return Q->head->data;
}
void Pop(Queue*Q)
{
QNode* d = Q->head;
Q->head = Q->head->next;
free(d);
}
Graph *Create(int n)
{
Graph *G = (Graph*)malloc(sizeof(Graph));
G->max = n;
G->vertex = (VertexNode*)calloc(n+1,sizeof(VertexNode));
for(int i = 0; i <= n; i++)
{
G->vertex[i].arc = NULL;
}
return G;
}
int Locate(Graph *G,int x)
{
int i = 1;
for(; i<=G->max; i++)
{
if(G->vertex[i].data == x)
break;
}
if(i > G->max)
return -1;
else
return i;
}
void Fill_VertexNode(Graph *G, int n)
{
for(int i = 1; i <= n; i++)
{
scanf("%d",&G->vertex[i].data);
}
}
void Add(Graph *G,int x, int y)
{
int a = Locate(G,x);
int b = Locate(G,y);
ArcNode *A = (ArcNode*)malloc(sizeof(ArcNode));
A->to = b;
A->next = G->vertex[a].arc;
G->vertex[a].arc = A;
}
void BFS(Graph* G,int * arr, int x)
{
if(arr[x])
return;
Queue *Q = Create_Queue();
Push(Q,x);
while(!Empty(Q))
{
int t = Top(Q);
Pop(Q);
if(G->vertex[t].data == dream)
{
find = 1;
return;
}
for(ArcNode *A = G->vertex[t].arc; A; A = A->next)
{
if(arr[A->to])
continue;
Push(Q,A->to);
}
}
}
int BFS_search(Graph*G, int x,int dre)
{
find = 0;
x = Locate(G,x);
dream = dre;
int *arr = (int *)calloc(G->max+1, sizeof(int));
for(int i = 0; i <= G->max; i++)
{
arr[i] = 0;
}
BFS(G,arr,x);
return find;
}
int main()
{
int begin, end;
int n,m;
scanf("%d%d",&n,&m);
Graph *G = Create(n);
Fill_VertexNode(G,n);
for(int i = 1; i <= m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
Add(G,x,y);
}
scanf("%d%d",&begin,&end);
int ret = BFS_search(G,begin,end);
if(ret) printf("yes");
else printf("no");
return 0;
}
/*
4 4
4 2 1 3
1 2
1 3
1 4
2 3
2 3 */

基于图的广度优先搜索策略(耿7.11)--------西工大noj.20的更多相关文章

  1. 基于图的深度优先搜索策略(耿7.10)--------西工大noj

    ​ 代码 代码 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct ...

  2. 以三元组表为存储结构实现矩阵相加(耿5.7)----------西工大 noj

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  3. DCOS实践分享(1):基于图形化模型设计的应用容器化实践

    2015年11月29日,Mesos Meetup 第三期 - 北京技术沙龙成功举行.本次活动由数人科技CTO 肖德时 和 Linker Networks 的 Sam Chen 一起组织发起. 在这次m ...

  4. TF-IDF计算方法和基于图迭代的TextRank

    文本处理方法概述 说明:本篇以实践为主,理论部分会尽量给出参考链接 摘要: 1.分词 2.关键词提取 3.主题模型(LDA/TWE) 4.词的两种表现形式(词袋模型和分布式词向量) 5.关于文本的特征 ...

  5. 【GCN】图卷积网络初探——基于图(Graph)的傅里叶变换和卷积

    [GCN]图卷积网络初探——基于图(Graph)的傅里叶变换和卷积 2018年11月29日 11:50:38 夏至夏至520 阅读数 5980更多 分类专栏: # MachineLearning   ...

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

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

  7. 图的广度优先遍历算法(BFS)

    在上一篇文章我们用java演示了图的数据结构以及图涉及到的深度优先遍历算法,本篇文章将继续演示图的广度优先遍历算法.广度优先遍历算法主要是采用了分层的思想进行数据搜索.其中也需要使用另外一种数据结构队 ...

  8. 基于.NetCore开发博客项目 StarBlog - (11) 实现访问统计

    系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...

  9. 图的广度优先搜索(BFS)

    把以前写过的图的广度优先搜索分享给大家(C语言版) #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 ...

随机推荐

  1. 「Python实用秘技08」一行代码解析地址信息

    本文完整示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/PythonPracticalSkills 这是我的系列文章「Python实用秘技」的第8期 ...

  2. 新鲜出炉:appium2.0+ 单点触控和多点触控新的解决方案

    在 appium2.0 之前,在移动端设备上的触屏操作,单手指触屏和多手指触屏分别是由 TouchAction 类,Multiaction 类实现的. 在 appium2.0 之后,这 2 个方法将会 ...

  3. html单页面通过cdn引入element-ui组件样式不显示问题

    html单页面通过cdn引入element-ui组件样式不显示问题 必须先引入vue,再通过cdn引入element,否则element-ui组件与样式无效. <!DOCTYPE html> ...

  4. EF Core 的关联查询

    0 前言 本文会列举出 EF Core 关联查询的方法: 在第一.二.三节中,介绍的是 EF Core 的基本能力,在实体中配置好关系,即可使用,且其使用方式,与编程思维吻合,是本文推荐的方式. 第四 ...

  5. 手把手教你 bash中给变量赋值时 ' 和 " 和 ` 和 $() 的使用

    1.赋值指令 var='变量内容' var="变量内容" var=`command` var=$(command) var=变量内容 2.格式要求 =两边不能有空白字符 错误示例 ...

  6. 运维:DevSecOps

    什么是DevSecOps DevSecOps 是一场关于 DevOps 概念实践或艺术形式的变革.DevOps之父Patrick Debios 强调:"DevOps2.0时代应首先解决人的问 ...

  7. ZIP压缩输入/输出

    学习内容: 一.压缩文件 1.利用ZipOutputStream类对象,可将文件压缩. 2.ZipOutputStream类构造方法:ZipOutputStream(OutputStream out) ...

  8. Grafana+Prometheus 搭建 JuiceFS 可视化监控系统

    作为承载海量数据存储的分布式文件系统,用户通常需要直观地了解整个系统的容量.文件数量.CPU 负载.磁盘 IO.缓存等指标的变化. JuiceFS 没有重复造轮子,而是通过 Prometheus 兼容 ...

  9. OpenWrt 20.02.2 小米路由器3G配置CP1025网络打印

    家里的施乐 CP116w 工作快五年了终于罢工了. 黑粉报错, 自己也不会拆, 只能搁置了. 后来换了个 HP CP1025. 这个打印机也不错, 墨盒便宜没什么废粉, 就是启动慢一点, 而且 -- ...

  10. 数组——JavaSE基础

    数组 数组初始化 public class ArrayDemo02 { public static void main(String[] args) { // 静态初始化 int[] a = {1, ...