第二次训练赛 B. Mike and Shortcuts-bfs
2019-02-03 22:45
16 查看
题目传送门:https://codeforces.com/problemset/problem/689/B
题目大意:有n个城市在一排,两两间距为1,每个城市可以用1的距离到一个给定的城市,问城市1到其他所有城市的最短距离。
思路:bfs。
比赛时脑抽写了个dijkstra,还wa了。。。
bfs的时候一定要搜到一个标记一个。。。
代码:
#include <bits/stdc++.h> #define ll long long using namespace std; const ll M=2e5+10; bool vis[M]; ll n,a[M],d[M]; void bfs(){ queue<ll> q; q.push(1); vis[1]=1; while(!q.empty()){ ll p=q.front(); q.pop(); if(p<n && !vis[p+1]) d[p+1]=d[p]+1,vis[p+1]=1,q.push(p+1); if(p>1 && !vis[p-1]) d[p-1]=d[p]+1,vis[p-1]=1,q.push(p-1); if(a[p] && !vis[a[p]]) d[a[p]]=d[p]+1,vis[a[p]]=1,q.push(a[p]); } } int main(){ scanf("%lld",&n); for(ll i=1;i<=n;i++) scanf("%lld",&a[i]); bfs(); for(ll i=1;i<=n;i++) printf("%lld ",d[i]); return 0; }
相关文章推荐
- CSU-ACM2017暑期训练6-bfs Mike and Shortcuts
- codeforces 689B B. Mike and Shortcuts(bfs)
- Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)
- Mike and Shortcuts CodeForces - 689B BFS
- 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【BFS】
- BFS-Mike and Shortcuts
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs
- Codeforces 689B. Mike and Shortcuts(BFS)
- CSU-ACM2017暑期训练6-bfs G - Mike and Shortcuts CodeForces - 689B
- CF——Mike and Shortcuts(BFS)
- Mike and Shortcuts (CF bfs)
- Codeforces #361 (Div. 2)B-Mike and Shortcuts(spfa最短路)
- codeforces 361 B - Mike and Shortcuts
- CodeForces 689 B. Mike and Shortcuts(SPFA)
- 第二次训练-B-Button Bashing(突破我对bfs的固有看法)
- Mike and Shortcuts
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- CodeForces 689B - Mike and Shortcuts