基于图的广度优先搜索策略(耿7.11)--------西工大noj.20
目录


代码
#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的更多相关文章
- 基于图的深度优先搜索策略(耿7.10)--------西工大noj
代码 代码 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct ...
- 以三元组表为存储结构实现矩阵相加(耿5.7)----------西工大 noj
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...
- DCOS实践分享(1):基于图形化模型设计的应用容器化实践
2015年11月29日,Mesos Meetup 第三期 - 北京技术沙龙成功举行.本次活动由数人科技CTO 肖德时 和 Linker Networks 的 Sam Chen 一起组织发起. 在这次m ...
- TF-IDF计算方法和基于图迭代的TextRank
文本处理方法概述 说明:本篇以实践为主,理论部分会尽量给出参考链接 摘要: 1.分词 2.关键词提取 3.主题模型(LDA/TWE) 4.词的两种表现形式(词袋模型和分布式词向量) 5.关于文本的特征 ...
- 【GCN】图卷积网络初探——基于图(Graph)的傅里叶变换和卷积
[GCN]图卷积网络初探——基于图(Graph)的傅里叶变换和卷积 2018年11月29日 11:50:38 夏至夏至520 阅读数 5980更多 分类专栏: # MachineLearning ...
- SDUT-2124_基于邻接矩阵的广度优先搜索遍历
数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一个无向连通图 ...
- 图的广度优先遍历算法(BFS)
在上一篇文章我们用java演示了图的数据结构以及图涉及到的深度优先遍历算法,本篇文章将继续演示图的广度优先遍历算法.广度优先遍历算法主要是采用了分层的思想进行数据搜索.其中也需要使用另外一种数据结构队 ...
- 基于.NetCore开发博客项目 StarBlog - (11) 实现访问统计
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...
- 图的广度优先搜索(BFS)
把以前写过的图的广度优先搜索分享给大家(C语言版) #include<stdio.h> #include<stdlib.h> #define MAX_VERTEX_NUM 20 ...
随机推荐
- nslookup:command not found的解决办法
nslookup:command not found的解决办法 通过nslookup查看DNS记录,在这里遇到了一个小插曲,nslookup:command not found(未找到命令),是因为新 ...
- 渗透:Nmap
Nmap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包. nmap是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端.确定哪些服务运行在哪些连接端,并且推断计算 ...
- sqlserver 插入 更新 删除 语句中的 output子句
官方文档镇楼: https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/ms177564(v=sql.100) 从 ...
- 选择ERP频频踩雷?国内外ERP有差异,突破ERP软件单一性是关键
信息化日新月异的蓬勃发展,导致企业在选择ERP软件时频频踩雷.企业如何选择出一个适合自己的ERP软件系统呢?是选择国外知名公司的ERP软件产品,还是选择国内性价比高的ERP软件产品呢,小编就带大家了解 ...
- Java变量, 常量和作用域
目录 变量 作用域 局部变量 实例变量 类变量 常量 命名规范 视频课程 变量 变量就是可以变化的量 Java是一种强类型的语言, 每个变量都必须声明其类型 Java变量是程序中最基本的存储单元, 其 ...
- 《HALCON数字图像处理》第六章笔记
目录 第六章 图像增强 图像增强的概念和分类 灰度变换 直方图处理 图像的平滑 图像的锐化 图像的彩色增强 我在Gitee上建了个仓库,会将学习书本的时候打的一些代码上传上去,笔记中所有代码都在仓库里 ...
- Groovy基础语法
Groovy 基础语法 变量定义 1.支持动态类型,使用def关键字定义变量 // Java中定义变量的方式 int age = 18; String name = "张三"; / ...
- Linux文本搜索及截取操作
Linux文本搜索及截取操作 cat 查看 grep 搜索 awk 截取 查看dna-server.xml 文件的内容 [root@localhost servers]# cat cwag9002/w ...
- 在jupyternotebook中写C/C++
在jupyter notebook中写C/C++,最大的好处就是不用写main()函数,直接调用写好的函数即可执行. #include<stdio.h> int sum(int a,int ...
- 重学ES系列之Set实现数组去重、交集、并集、差集
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...