graph_bfs(数组)

#include <iostream>
#include <string.h>
using namespace std;
const int MAX_N = 100;
const int MAX_M = 10000;
struct edge {
    int v, next;
} e[MAX_M];
int p[MAX_N], eid;
void init() {
    memset(p, -1, sizeof(p));
    eid = 0;
}
void insert(int u, int v) {
    e[eid].v = v;
    e[eid].next = p[u];
    p[u] = eid++;
}
bool vst[MAX_N];
int q[MAX_N],l,r;
void bfs(int v)
{
	memset(vst,0,sizeof(vst));
	l=0,r=-1;
	q[++r]=v;
	vst[v]=1;
	while(l<=r)
	{
		int u=q[l++];
		cout<<"visiting"<<u<<endl;
		for(int i=p[u];i+1;i=e[i].next)
		{
			if(!vst[e[i].v])
			{
				vst[e[i].v]=1;
				q[++r]=e[i].v;
			}
		} 
	}
	
 } 

猜你喜欢

转载自blog.csdn.net/amf12345/article/details/89076765