跳到正文

题解

题解:B4292 [蓝桥杯青少年组省赛 2022] 路线

#SUSPENDED#约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年2月28日
篇幅
约 1 分钟
题目
B4292

版本

  1. v1当前

思路

这就是简简单单的图吗。

建一个双向图,求点 nn 到所有点的距离。

从节点 nn 开始搜索,一直往下搜,就能求出了。

代码

C++
#include<bits/stdc++.h>
using namespace std;
vector<int>f[110];
int a[110],n,m,x,y;
bool b[110];
queue<int>q;
int main(){
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>x>>y;
		f[x].push_back(y);
		f[y].push_back(x);
	}
	memset(a,0xff,sizeof(a));
	a[n]=0,q.push(n),b[n]=1;
	while(q.size()){
		int x;
		x=q.front(),q.pop();
		for(int i=0;i<f[x].size();i++)
		if(b[f[x][i]]==0){
			q.push(f[x][i]);
			b[f[x][i]]=1;
			a[f[x][i]]=a[x]+1;
		}
	}
	for(int i=1;i<n;i++)
	cout<<a[i]<<" ";
}
原文