图论知识小结3-BFS的数组模拟实现

#include <bits/stdc++.h>
using namespace std;
const int MAX_EDGE = 10000;
const int MAX_VERTICES = 100;
struct Edge{
	int len, v, last;
} edge[MAX_EDGE];
int eid;
bool vst[MAX_VERTICES];
int latest_edge_of_u[MAX_EDGE];
void init(){
	memset(latest_edge_of_u, -1 , sizeof(latest_edge_of_u));
	for(i = 0 ; i < MAX_VERTICES ; i++){
		vst[i] = false;
	}
	eid = 0;
} 
void insert(int w, int u, int v){
	if(w){
		edge[eid].v = v;
		edge[eid].len = w;
		edge[eid].last = latest_edge_of_u[eid];
		latest_edge_of_u[eid] = eid++;
	}
}
void bfs(int start){
	queue <int> q;
	vst[start] = true;
        previsit(start);
	q.push(start);
	int curr;
	while(!q.empty()){
		curr = q.front();
		q.pop();
		for(eid = latest_edge_of_u[curr] ; eid != -1 ; eid = egde[eid].last){
			if(!vst[edge[eid].v]]){
				vst[edge[eid].v] = true;
                       q.push(edge[eid].v);
			}
		}
	}
}
int main(){
	init();
	//
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/chenhanxuan1999/article/details/84862232