zoukankan      html  css  js  c++  java
  • 洛谷 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;
    }
    
  • 相关阅读:
    AESUtil_1
    ELK配置
    Centos7上安装docker
    Excel大批量数据导出
    Redis5.0.6安装完整步骤
    idea远程打断点
    [HNOI2016] 序列
    [TJOI2017] 异或和
    洛谷 P4933 大师
    洛谷 P1950 长方形_NOI导刊2009提高(2)
  • 原文地址:https://www.cnblogs.com/yinyuqin/p/15303084.html
Copyright © 2011-2022 走看看