洛谷 P1346 电车(双端队列广搜)

传送门


解题思路

水题一个。
数据范围可以Floyd水过去。
但是苏轼告诉我们:

守其初心,始终不变。

屈原告诉我们:

虽九死其犹未悔。

所以我用了O(n+m)的搜索。
其实这叫做双端队列广搜,碰到边权为0放到队列首,边权为1放到队列尾。
但我没学过,就用了dfs+bfs结合体水过去了。

AC代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=105;
queue<int> q;
int n,s,t,vis[maxn],to[maxn][maxn],cnt[maxn],dis[maxn];
void dfs(int u){
	if(cnt[u]&&!vis[to[u][1]]){
		dis[to[u][1]]=dis[u];
		vis[to[u][1]]=1;
		dfs(to[u][1]);
	}
	for(int i=1;i<=cnt[u];i++){
		if(vis[to[u][i]]) continue;
		dis[to[u][i]]=dis[u]+1;
		vis[to[u][i]]=1;
		q.push(to[u][i]);
	}
}
int main(){
	ios::sync_with_stdio(false);
	memset(dis,-1,sizeof(dis));
	cin>>n>>s>>t;
	for(int i=1;i<=n;i++){
		cin>>cnt[i];
		for(int j=1;j<=cnt[i];j++) cin>>to[i][j];
	}
	q.push(s);
	vis[s]=1;
	dis[s]=0;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		if(cnt[u]&&!vis[to[u][1]]){
			dis[to[u][1]]=dis[u];
			vis[to[u][1]]=1;
			dfs(to[u][1]);
		}
		for(int i=1;i<=cnt[u];i++){
			if(vis[to[u][i]]) continue;
			dis[to[u][i]]=dis[u]+1;
			vis[to[u][i]]=1;
			q.push(to[u][i]);
		}
	}
	cout<<dis[t];
    return 0;
}
原文地址:https://www.cnblogs.com/yinyuqin/p/15303084.html