链式前向星BFS
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5399068
采用链式前向星的BFS:
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL; const int maxN = + ;
const int maxM = + ;
int head[maxN];
int visited[maxN];
int N, M; struct EdgeNode
{
int to;
int w;
int next;
};
EdgeNode Edges[maxM]; void BFS(int x)
{
memset(visited, , sizeof(visited));
queue<int> initQueue;
for(int i = ; i <= N; i++)
{
if( !visited[i] )
{
visited[i] = ;
initQueue.push( i );
while( !initQueue.empty() )
{
i = initQueue.front(); //取得对头的节点,一定是下次待扩展的节点
initQueue.pop();//队顶元素出队(出队代表着访问到了这个节点)
//遍历与当前节点相连的节点
for(int j = head[i]; j != -; j = Edges[j].next)
{
//如果没被访问,入队
if( !visited[ Edges[j].to ])
{
visited[ Edges[j].to ] = ;
initQueue.push( Edges[j].to );
}
}
}
}
}
} int main()
{
freopen("input.txt", "r", stdin);
memset(head, -, sizeof(head));
cin >> N >> M;
for(int i = ; i <= M; i++)
{
int x, y ,z;
cin >> x >> y >> z;
Edges[i].to = y;
Edges[i].w = z;
Edges[i].next = head[x];
head[x] = i;
}
memset(visited, , sizeof(visited));
BFS();
return ;
}
链式前向星BFS的更多相关文章
- zzuli 2130: hipercijevi 链式前向星+BFS+输入输出外挂
2130: hipercijevi Time Limit: 1 Sec Memory Limit: 128 MB Submit: 595 Solved: 112 SubmitStatusWeb B ...
- poj-1459-最大流dinic+链式前向星-isap+bfs+stack
title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...
- 链式前向星存树图和遍历它的两种方法【dfs、bfs】
目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...
- 链式前向星写法下的DFS和BFS
Input 5 7 1 2 2 3 3 4 1 3 4 1 1 5 4 5 output 1 5 3 4 2 #include<bits/stdc++.h> using namespace ...
- zzuli 2131 Can Win dinic+链式前向星(难点:抽象出网络模型+建边)
2131: Can Win Time Limit: 1 Sec Memory Limit: 128 MB Submit: 431 Solved: 50 SubmitStatusWeb Board ...
- 网络流dinic模板,邻接矩阵+链式前向星
//这个是邻接矩阵的#include<iostream> #include<queue> #include<string.h> #include<stdio. ...
- Pants On Fire(链式前向星存图、dfs)
Pants On Fire 传送门:链接 来源:upc9653 题目描述 Donald and Mike are the leaders of the free world and haven't ...
- 链式前向星+SPFA
今天听说vector不开o2是数组时间复杂度常数的1.5倍,瞬间吓傻.然后就问好的图表达方式,然后看到了链式前向星.于是就写了一段链式前向星+SPFA的,和普通的vector+SPFA的对拍了下,速度 ...
- 单元最短路径算法模板汇总(Dijkstra, BF,SPFA),附链式前向星模板
一:dijkstra算法时间复杂度,用优先级队列优化的话,O((M+N)logN)求单源最短路径,要求所有边的权值非负.若图中出现权值为负的边,Dijkstra算法就会失效,求出的最短路径就可能是错的 ...
随机推荐
- shell脚本批量下载资源并保留路径
示例资源列表 如url.txt: http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png http://su.bdimg.co ...
- soapUI的简单使用(webservice接口功能测试)
1.soapUI支持什么样的测试? 功能测试.性能测试.负载.回归测试等,它不仅仅可以测试基于 SOAP 的 Web 服务,也可以测试 REST 风格的 Web 服务. 1.SoapUI安装注意事项 ...
- UnitOfWork知多少 【转】
原文链接:https://www.cnblogs.com/sheng-jie/p/7416302.html 1. 引言 Maintains a list of objects affected by ...
- (转)KlayGE游戏引擎 :高效的GBUFFER管理方式
转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=3304 个顶点.这样的数据对GPU来说是很头疼的.所以引擎往往需要在Buffer上做一些工作来 ...
- sklearn中predict()与predict_proba()用法区别
predict是训练后返回预测结果,是标签值. predict_proba返回的是一个 n 行 k 列的数组, 第 i 行 第 j 列上的数值是模型预测 第 i 个预测样本为某个标签的概率,并且每一行 ...
- django的聚合函数和aggregate、annotate方法使用
支持聚合函数的方法: 提到聚合函数,首先我们要知道的就是这些聚合函数是不能在django中单独使用的,要想在django中使用这些聚合函数,就必须把这些聚合函数放到支持他们的方法内去执行.支持聚合函数 ...
- A Neural Algorithm of Artistic Style
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/53931536 1. 资源 Paper: ...
- poj 1459 网络流问题`EK
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24930 Accepted: 12986 D ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 x: 十进制到二进制的转换
http://acm.ocrosoft.com/problem.php?cid=1316&pid=49 题目描述 把十进制到二进制的转换. 输入 234 输出 11101010 样例输入 23 ...
- 六、vue侦听属性
$watch 实际上无论是 $watch 方法还是 watch 选项,他们的实现都是基于 Watcher 的封装.首先我们来看一下 $watch 方法,它定义在 src/core/instance/s ...