#include<iostream>
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
using namespace std; int m,n,maxn=0x7fffffff;
int s[][]; bool visit[]={false};
queue<int>str; void BFS(int );
void DFS(int ); int main()
{
memset(s,0x7f,sizeof(s));
cin>>n>>m;
for(int i=;i<m;++i)
{
int x,y,z;
cin>>x>>y>>z;
s[x][y]=z;
}
memset(visit,false,sizeof(visit));
cout<<""<<"*****";
BFS();
cout<<endl<<"1*****";
memset(visit,false,sizeof(visit));
DFS();
} void BFS(int sum)
{
cout<<"-->"<<sum;
visit[sum]=true;
str.push(sum);
while(!str.empty())
{
int j=str.front();
str.pop();
for(int i=;i<=n;++i)
{
if(!visit[i]&&s[j][i]!=maxn)
{
cout<<"-->"<<i;
str.push(i);
visit[i]=true;
}
}
}
} void DFS(int sum)
{
int i;
cout<<"-->"<<sum;
visit[sum]=true;
for(int i=;i<=n;++i)
{
if(!visit[i]&&s[sum][i]!=maxn)
DFS(i);
}
}

图的遍历[DFS][BFS]的更多相关文章

  1. 图的数据结构的实现与遍历(DFS,BFS)

    //图的存储结构:const int MAXSIZE = 10;//邻接矩阵template<class T>class MGraph {public:    MGraph(T a[], ...

  2. 图的遍历DFS

    图的遍历DFS 与树的深度优先遍历之间的联系 树的深度优先遍历分为:先根,后根 //树的先根遍历 void PreOrder(TreeNode *R){ if(R!=NULL){ visit(R); ...

  3. 图的遍历——DFS和BFS模板(一般的图)

    关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...

  4. PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

    题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

  5. 图的遍历——DFS

    原创 图的遍历有DFS和BFS两种,现选用DFS遍历图. 存储图用邻接矩阵,图有v个顶点,e条边,邻接矩阵就是一个VxV的矩阵: 若顶点1和顶点5之间有连线,则矩阵元素[1,5]置1,若是无向图[5, ...

  6. 图的遍历——DFS(矩形空间)

    首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...

  7. 图的遍历 | 1076 bfs

    bfs踩了很多坑才写完.注意:出队时不做是否vis判断,但是要加上vis[出队顶点]=1 .入队时进行判断,并且也要 vis[入队顶点]=1 #include <stdio.h> #inc ...

  8. 图的遍历(bfs 和dfs)

    BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个 ...

  9. PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]

    题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...

随机推荐

  1. eth day05

    智能合约众筹实战 淘宝众筹,京东众筹 https://izhongchou.taobao.com/index.htm 分析商业模式 解决京东众筹的痛点 https://izhongchou.taoba ...

  2. shell及Python爬虫实例展示

    1.shell爬虫实例: [root@db01 ~]# vim pa.sh #!/bin/bash www_link=http://www.cnblogs.com/clsn/default.html? ...

  3. 官方文档 恢复备份指南七 Using Flashback Database and Restore Points

    本章内容: Understanding Flashback Database, Restore Points and Guaranteed Restore Points Logging for Fla ...

  4. POJ 3177 Redundant Paths & POJ 3352 Road Construction(双连通分量)

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  5. wpa_supplicant 初始化

    几个重要的结构体介绍: 1. struct wpa_interface --- Parameters for wpa_supplicant_add_iface(). wpa_interface对应网络 ...

  6. Packet filtering with Linux & NAT

    http://www.linuxfocus.org/ChineseGB/May2003/article289.shtml Gateway, Proxy-Arp 和 Ethernet Bridge ? ...

  7. DataBase -- JOIN

    SQL JOIN:用于根据两个或多个表中列的关系,从这些表中查数据. (为了得到完整数据,我们需要从两个或多个表中获取结果.) 例如W3School中列出的实例,使用如下语句: select Pers ...

  8. SRM710 div1 ReverseMancala(trick)

    题目大意, 给定一个有n个点的环,n不超过10,每个点上有一个权重 起始时权重将会给出,然后有2种操作 第一种操作是,选择一个位置i,获得权重w = a[i],把a[i]变成0,然后接下来在环上顺着走 ...

  9. 周记【距gdoi:105天】

    月考果然很可怕,跪得要死. 然后这周搞(被老师坑)去搞某个程序,我和蔡大神和kpm分工搞(结果最后我也只是变成全程嘴炮). 这周有点闷,明明想快乐点但还是…… 进度慢得要死,后缀数组略神的东西.模仿了 ...

  10. Flash by sshockwave [树dp]

    题目 给定一棵树,每个点有一个活动时间,长度为正整数$t_i$ 你需要安排每个点的活动时间什么时候开始什么时候结束,并且满足:任何一个时刻没有两个相邻的点都在活动 开始时刻为0,在以上条件下最小化所有 ...