题目链接:

B. Mike and Shortcuts

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bit by doing some sight seeing in the city.

City consists of n intersections numbered from 1 to n. Mike starts walking from his house located at the intersection number 1 and goes along some sequence of intersections. Walking from intersection number i to intersection j requires |i - j| units of energy. The total energy spent by Mike to visit a sequence of intersections p1 = 1, p2, ..., pk is equal to  units of energy.

Of course, walking would be boring if there were no shortcuts. A shortcut is a special path that allows Mike walking from one intersection to another requiring only 1 unit of energy. There are exactly n shortcuts in Mike's city, the ith of them allows walking from intersection ito intersection ai (i ≤ ai ≤ ai + 1) (but not in the opposite direction), thus there is exactly one shortcut starting at each intersection. Formally, if Mike chooses a sequence p1 = 1, p2, ..., pk then for each 1 ≤ i < k satisfying pi + 1 = api and api ≠ pi Mike will spend only 1unit of energy instead of |pi - pi + 1| walking from the intersection pi to intersection pi + 1. For example, if Mike chooses a sequencep1 = 1, p2 = ap1, p3 = ap2, ..., pk = apk - 1, he spends exactly k - 1 units of total energy walking around them.

Before going on his adventure, Mike asks you to find the minimum amount of energy required to reach each of the intersections from his home. Formally, for each 1 ≤ i ≤ n Mike is interested in finding minimum possible total energy of some sequence p1 = 1, p2, ..., pk = i.

Input
 

The first line contains an integer n (1 ≤ n ≤ 200 000) — the number of Mike's city intersection.

The second line contains n integers a1, a2, ..., an (i ≤ ai ≤ n , , describing shortcuts of Mike's city, allowing to walk from intersection i to intersection ai using only 1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (from ai to i).

Output
 

In the only line print n integers m1, m2, ..., mn, where mi denotes the least amount of total energy required to walk from intersection 1 to intersection i.

Examples
 
input
3
2 2 3
output
0 1 2 
input
5
1 2 3 4 5
output
0 1 2 3 4 
input
7
4 4 4 4 7 7 7
output
0 1 2 1 2 3 3 

题意:

点i与点j的距离为|i-j|,而且第i个点到a[i]的距离为1,问第1个点到其他所有点的最短距离;

思路:

bfs的水题,对于一个点i,距它为1的点有i-1,i+1,a[i],这样bfs就好了;

AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e5+;
const int maxn=;
const double eps=1e-; int n,a[N],vis[N],dis[N];
queue<int>qu;
void bfs()
{
qu.push();
vis[]=;
while(!qu.empty())
{
int fr=qu.front();
qu.pop();
if(!vis[fr+])
{
vis[fr+]=;
dis[fr+]=dis[fr]+;
qu.push(fr+);
}
if(fr->&&!vis[fr-])
{ vis[fr-]=;
dis[fr-]=dis[fr]+;
qu.push(fr-); }
if(!vis[a[fr]])
{
vis[a[fr]]=;
dis[a[fr]]=dis[fr]+;
qu.push(a[fr]);
}
}
}
int main()
{
read(n);
For(i,,n)read(a[i]);
bfs();
For(i,,n)printf("%d ",dis[i]); return ;
}

codeforces 689B B. Mike and Shortcuts(bfs)的更多相关文章

  1. Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  2. CodeForces 689B Mike and Shortcuts (BFS or 最短路)

    题目链接:http://codeforces.com/problemset/problem/689/B 题目大意: 留坑 明天中秋~

  3. codeforces 361 B - Mike and Shortcuts

    原题: Description Recently, Mike was very busy with studying for exams and contests. Now he is going t ...

  4. CodeForces 689B Mike and Shortcuts (bfs or 最短路)

    Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...

  5. Codeforces 689B. Mike and Shortcuts SPFA/搜索

    B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...

  6. Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)

    B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  7. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  8. Codeforces Round #361 (Div. 2) B bfs处理

    B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  9. Codeforces 547C/548E - Mike and Foam 题解

    目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...

随机推荐

  1. zoj 2886 Look and Say

    Look and Say Time Limit: 2 Seconds      Memory Limit: 65536 KB The look and say sequence is defined ...

  2. POJ3321Apple Tree【dfs 树状数组】

    题目大意:一棵树(不一定是二叉树!!),树的节点上本来都有一个苹果,要求完成以下操作: 1.指定某个节点,如果这个节点原本有苹果则拿去,如果没有苹果则填上一个苹果 2.询问某个节点以及其子树一共有多少 ...

  3. bzoj2277 [Poi2011]Strongbox

    2277: [Poi2011]Strongbox Time Limit: 60 Sec  Memory Limit: 32 MBSubmit: 498  Solved: 218[Submit][Sta ...

  4. spoj 839 最小割+二进制

    #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...

  5. git push ‘No refs in common and none specified’doing nothing问题解决

    git push ‘No refs in common and none specified’doing nothing问题解决 输入git push origin master即可解决问题

  6. Cooking Schedule Problem Code: SCHEDULE(优先队列)

    Cooking Schedule Problem Code: SCHEDULE Chef is a well-known chef, and everyone wishes to taste his ...

  7. 登录页面练习servlet

    登录练习: 1创建登录页面 创建servlet进行登录页面请求 2点击登录完成操作 浏览器发送请求到服务器(用户信息+其他数据 )3服务器调用对应的servlet进行处理 设置响应编码格式 获取请求信 ...

  8. 实验二:编写输出“Hello word!”

    一:编写输出“Hello word!” 1.运行eclipse,在project name中输入要创建的项目名称. 2.创建java类,点击File->New->Class,在弹出窗口中N ...

  9. Python的描述符

    1.描述符的定义 描述符是与特定属性互相绑定的一种协议,通过方法被触发修改属性,这些方法包括__get__(),__set__(),__delete__().将这些方法定义在类中,即可实现描述符 2. ...

  10. Eclipse中Maven运行项目时在Console中无日志出现的问题解决

    这是由于工作空间损坏造成的,比如重装JDK后,或者重装Maven后这些问题.解决方法如下: 1.删除现有工作空间,重新选择一个新的. 2.重置Workspece. 3.可能是Maven版本太新导致的, ...