Codeforces - Lunar New Year and a Wander

题目链接:Codeforces - Lunar New Year and a Wander


每次肯定选择当前能走的最小的节点走。

因为可以回退。所以直接每次把能到的点全部放到堆里面,每次取最小的即可。


AC代码:

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e5+10;
int n,m,vis[N];	priority_queue<int,vector<int>,greater<int> > q;
vector<int> g[N];
inline void add(int a,int b){g[a].push_back(b),g[b].push_back(a);}
signed main(){
	cin>>n>>m;	q.push(1);	vis[1]=1;
	for(int i=1,a,b;i<=m;i++)	scanf("%d %d",&a,&b),add(a,b);
	while(q.size()){
		int u=q.top();	q.pop();	printf("%d ",u);
		for(auto to:g[u])	if(!vis[to])	vis[to]=1,q.push(to);
	}
	return 0;
}
发布了809 篇原创文章 · 获赞 246 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/weixin_43826249/article/details/105273937