head 记录了从每个节点出发的第一条边在 ver 和 edge 数组中的储存位置
ver 记录了每条边的终点
edge 记录每条边的边权
next 模拟了链表指针

//存图
void add(int x, int y, int z){  //x:起点 y:终点 z:权值
	ver[++tot] = y;
	edge[tot] = z;
	next[tot] = head[x];
	head[x] = tot;
}
//bfs 遍历
void bfs(){
	queue <int> q;
	q.push(1); d[1] = 1;
	while (q.size()){
		int x = q.front(); q.pop();
		for (int i = head[x]; i; i = Next[i]){
			int y = ver[i];
			if (d[y]) continue;
			d[y] = d[x] + 1;
			w[y] = w[x] + 1;
			q.push(y);
		}
	}
}

应用:
https://www.cnblogs.com/Hamine/p/15774678.html J题可以用链式前向星+bfs

posted on 2022-01-07 16:47  Hamine  阅读(80)  评论(0)    收藏  举报