Mike and Shortcuts

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121333#problem/F

Description

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 i to 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 1 unit of energy instead of |pi - pi + 1| walking from the intersection pi to intersection pi + 1. For example, if Mike chooses a sequence p1 = 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.

Sample Input

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

Hint

In the first sample case desired sequences are:

1: 1; m1 = 0;

2: 1, 2; m2 = 1;

3: 1, 3; m3 = |3 - 1| = 2.

In the second sample case the sequence for any intersection 1 < i is always 1, i and mi = |1 - i|.

In the third sample case — consider the following intersection sequences:

1: 1; m1 = 0;

2: 1, 2; m2 = |2 - 1| = 1;

3: 1, 4, 3; m3 = 1 + |4 - 3| = 2;

4: 1, 4; m4 = 1;

5: 1, 4, 5; m5 = 1 + |4 - 5| = 2;

6: 1, 4, 6; m6 = 1 + |4 - 6| = 3;

7: 1, 4, 5, 7; m7 = 1 + |4 - 5| + 1 = 3.

题意:

图中任意两点之间距离为abs(i-j),每个点有且仅有一条捷径a[i]并且距离为1;
求从1开始遍历所有点的最小代价;

题解:

只考虑相邻两点和捷径这三条长度为1的边,最短路即可;
或者直接考虑上述三条边跑一遍bfs.
(以下代码为bfs法)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<set>
#include<list>
#define LL long long
#define maxn 210000
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);
using namespace std;int n;
queue<int> myq;
int dis[maxn];
int a[maxn];int main(int argc, char const *argv[])
{//IN;while(scanf("%d",&n) != EOF){for(int i=1; i<=n; i++) scanf("%d", &a[i]);fill(dis, dis+maxn, inf);while(!myq.empty()) myq.pop();myq.push(1); dis[1] = 0;while(!myq.empty()) {int cur = myq.front(); myq.pop();if(cur+1<=n && dis[cur+1] > dis[cur]+1){dis[cur+1] = dis[cur]+1;myq.push(cur+1);}if(cur-1>0 && dis[cur-1] > dis[cur]+1){dis[cur-1] = dis[cur]+1;myq.push(cur-1);}if(dis[a[cur]] > dis[cur]+1){dis[a[cur]] = dis[cur]+1;myq.push(a[cur]);}}for(int i=1; i<=n; i++)printf("%d ", dis[i]);;}return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5693308.html

CodeForces 689B Mike and Shortcuts (bfs or 最短路)相关推荐

  1. codeforces 689B Mike and Shortcuts 最短路

    题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1.问1到各个点之间的最短距离. 题目思路:SPFA求最短路 #incl ...

  2. 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 ...

  3. 【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa)

    题干: Recently, Mike was very busy with studying for exams and contests. Now he is going to chill a bi ...

  4. Codeforces D. Fair 多源BFS求最短路

    点击打开链接 D. Fair time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  5. CodeForces - 798B Mike and strings

    B. Mike and strings time limit per test2 seconds memory limit per test256 megabytes inputstandard in ...

  6. CSU 1259 bfs找最短路

    题目大意: 不想介绍,题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1259 bfs求最短路. 这里因为2-9,到达同样的点不计步数,那我 ...

  7. *【ZOJ - 3781】Paint the Grid Reloaded(dfs求连通块缩点,bfs求最短路,建图技巧)

    题干: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...

  8. UVa 816 (BFS求最短路)

    /*816 - Abbott's Revenge ---代码完全参考刘汝佳算法入门经典 ---strchr() 用来查找某字符在字符串中首次出现的位置,其原型为:char * strchr (cons ...

  9. Codeforces 540C Ice Cave (BFS)

    题目:http://codeforces.com/problemset/problem/540/C 代码: #include<stdio.h> #include<string.h&g ...

最新文章

  1. python壁纸程序代码_python设置windows桌面壁纸的实现代码
  2. AndoridSQLite数据库开发基础教程(9)
  3. List、Vector、ArraryList、LinkedList
  4. java程序错误类型及异常处理
  5. HDU 5908 Abelian Period 暴力
  6. SSH框架配置及Maven使用
  7. 微软 azure_在Microsoft Azure上运行Eclipse MicroProfile
  8. LeetCode 950. 按递增顺序显示卡牌(deque)
  9. 通用mapper 如何处理多表条件查询通过list封装(一对多)
  10. .container的应用
  11. Verilog HDL组合逻辑与时序逻辑区别
  12. 华为手机浏览器不支持PUT提交方式的解决方案
  13. idea怎么集成svn服务端,使用Mac自带svn搭建服务器,并使用idea进行连接(示例代码)...
  14. 产品经理|竞品分析(附《竞品分析报告》模板)
  15. URL重定向(跳转)漏洞
  16. 小白的倔强-NPN和PNP三极管的使用区别以及简单检测
  17. 复习汇总vue知识点
  18. 一直空中三角测量计算失败
  19. html完整语句,html语句
  20. python心形曲线和马鞍面_心形线与马鞍面

热门文章

  1. Redis专题-底层数据结构与使用场景
  2. linux制作ext2磁盘镜像,linux--创建镜像挂载
  3. show status 优化mysql_mysql优化--show status
  4. 如何运用python爬游戏皮肤_Python爬虫实战之 爬取王者荣耀皮肤
  5. 从消息到数据湖:看 Apache RocketMQ、Hudi、Kyuubi 最新进展
  6. 面对不可避免的故障,我们造了一个“上帝视角”的控制台
  7. 如何轻松学习 Kubernetes?
  8. Java主方法引用传递_java方法中的参数传递是值传递还是引用传递(转)
  9. 主机大师linux,113资讯网(www.113p.cn)评测:护卫神·主机大师 (Linux版)
  10. 多节锂电串联保护板ic_BMS电池管理系统与锂电池保护板的区别