Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
来源:互联网 发布:淘宝4星怎么贷款 编辑:程序博客网 时间:2024/05/17 09:04
没有考虑到问题的本质,自己最初写的是最暴力的搜索,其实只是对左右两点和最短路上的点有关系。
#include<bits/stdc++.h>using namespace std;const int N=200000+10;int n,a[N],use[N],ans[N];vector<int>g[N];int main(){ while(~scanf("%d",&n)) { for(int i=0;i<=n;i++) g[i].clear(); for(int i=1;i<=n;i++) { scanf("%d",a+i); g[i].push_back(a[i]); if(i!=n) g[i].push_back(i+1); if(i!=1) g[i].push_back(i-1); } memset(ans,0xff,sizeof(ans)); queue<int>q; q.push(1); ans[1]=0; while(!q.empty()) { int k=q.front(); q.pop(); for(int i=0;i<g[k].size();i++) { int y=g[k][i]; if(ans[y]==-1||ans[y]>ans[k]+1) { ans[y]=ans[k]+1; q.push(y); } } } for(int i=1;i<=n;i++) printf("%d ",ans[i]); printf("\n"); } return 0;}
0 0
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2) B - Mike and Shortcuts
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)
- codeforces 689B Mike and Shortcuts (bfs)
- CodeForces 689B BFS-Mike and Shortcuts
- Codeforces #361 (Div. 2)B-Mike and Shortcuts(spfa最短路)
- Codeforces 689B. Mike and Shortcuts(BFS)
- Codeforces 689B Mike and Shortcuts DFS or BFS
- Codeforces Round #305 (Div. 2) B.Mike and Fun
- Codeforces Round #305 (Div. 2) B. Mike and Fun(水题)
- Codeforces Round #305 (Div. 2)B. Mike and Fun
- Codeforces Round #305 (Div. 2) C. Mike and Frog +B. Mike and Fun
- CodeForces 689B - Mike and Shortcuts
- ptrace应用系列-基础知识
- DropdownList绑定的两种方法
- iOS开发必读-GitHub 上Top100 的 Objective-C 项目
- 添物不花钱学JavaEE(基础篇) --HTML
- CodeIgniter框架源码笔记(3)——每次请求的总调度师傅:引导文件CodeIgniter.php
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- Layer2-10 Ether-channel以太网捆绑
- 一个节省空间的小技巧
- ntohs的一个简单实现(将网络流中用两个字节16进制表示的资源数(如DNS)和长度转换为整形)
- opencv contours的问题
- C#中的@符号用法
- 【leetcode】27. Remove Element
- 属性动画
- 介绍一个二次排序的小技巧(best coder27期1001jump jump jump)