【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 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 energyinstead 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.

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 

Note

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.

题目大意:

   有n个点,主人公在1号点,现在假设相邻点之间的距离为1(表述为),并且每个点都有一条到其他点的距离为1的捷径(可以是自己,也可以是相邻点,也可以是远处的点,但是这条边是单向边)。定义了一下两点之间最短路的定义,求1号点到其他点的最短距离是多少?

解题报告:

    话说啊作为cf div2的B题,考最短路过分了啊虽然是裸题,,但是作为B题,,代码超50行了过分了啊。。

AC代码:

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int MAX = 2e5 + 5; 
ll dis[MAX];
bool vis[MAX];
int n,cnt;
vector<ll> vv[MAX];
struct Point {int pos;ll c;Point(){}Point(int pos,ll c):pos(pos),c(c){}bool operator < (const Point & b) const{return c > b.c;}
} ;
void Dijkstra() {for(int i = 1; i<=n; i++) dis[i] = 0x3f3f3f3f3f3f;memset(vis,0,sizeof vis);dis[1] = 0;priority_queue<Point> pq;pq.push(Point(1,0));while(!pq.empty()) {Point cur = pq.top();pq.pop();if(vis[cur.pos]) continue;vis[cur.pos] = 1;int up = vv[cur.pos].size();for(int i = 0; i<up; i++) {int v = vv[cur.pos][i];if(vis[v]) continue;if(dis[v] > dis[cur.pos] + 1) {dis[v] = dis[cur.pos] + 1;pq.push(Point(v,dis[v]));}}}
}
int main()
{cin>>n;int tmp;memset(head,-1,sizeof head);for(int i = 1; i<=n; i++) {scanf("%d",&tmp);vv[i].pb(tmp);}for(int i = 1; i<=n; i++) {if(i != 1) vv[i].pb(i-1);if(i != n) vv[i].pb(i+1);}Dijkstra();for(int i = 1; i<=n; i++) {printf("%lld%c",dis[i],i == n ? '\n' : ' ');}return 0 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441409.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

sqlserver mysql分页_SQLServer与MySQL中分页查询sql语句示例

/***author blovedr*功能&#xff1a;SQLServer与MySQL中分页查询sql语句示例*日期&#xff1a; 2018年8月17日 10:58*注释&#xff1a; 学习数据库MySQL的点点记录&#xff0c; 谢谢网上各位大神分享经验与资料&#xff0c; 欢迎大神批评与交流。*/分页查询 2018.8.16 …

【51Nod - 1103】N的倍数 (思维,鸽巢原理也叫抽屉定理,求倍数问题取模)

题干&#xff1a; 一个长度为N的数组A&#xff0c;从A中选出若干个数&#xff0c;使得这些数的和是N的倍数。 例如&#xff1a;N 8&#xff0c;数组A包括&#xff1a;2 5 6 3 18 7 11 19&#xff0c;可以选2 6&#xff0c;因为2 6 8&#xff0c;是8的倍数。 Input 第1行…

java位操作_关于java按位操作运算

<1>.在了解位移之前&#xff0c;先了解一下正数和负数的二进制表示形式以及关系&#xff1a;举例15和-15&#xff1a;15 的原码&#xff1a; 00000000 00000000 00000000 00001111补码&#xff1a; 11111111 11111111 11111111 111100001 -15的原码&#xff1a;11111111 …

【51Nod - 1117 】聪明的木匠 (贪心,哈夫曼树,时光倒流)

题干&#xff1a; 一位老木匠需要将一根长的木棒切成N段。每段的长度分别为L1,L2,......,LN&#xff08;1 < L1,L2,…,LN < 1000&#xff0c;且均为整数&#xff09;个长度单位。我们认为切割时仅在整数点处切且没有木材损失。 木匠发现&#xff0c;每一次切割花费的体…

java生成16位随机数_java中如何产生一个16位数字组成的随机字符串?谢谢各位了...

展开全部方法如下&#xff1a;Random rannew random();boolean flagtrue;while(flag){int aran.nextInt(99999999);int bran.nextInt(99999999);long ca*100000000Lb;if(c>1000000000000000L&&c<9999999999999999L){flagfalse;c1c;String numString.valueOf(c1);…

java utf-8 gbk_Java 字符转码之UTF-8转为GBK/GB2312

java跟python类似的做法&#xff0c;在java中字符串的编码是java修改过的一种Unicode编码&#xff0c;所以看到java中的字符串&#xff0c;心理要默念这个东西是java修改过的一种Unicode编码的编码。packagestring;importjava.nio.charset.Charset;public classUTF82GBK {publi…

【CodeForces - 760B 】Frodo and pillows (二分题意,注意细节)

题干&#xff1a; n hobbits are planning to spend the night at Frodos house. Frodo has n beds standing in a row and m pillows (n ≤ m). Each hobbit needs a bed and at least one pillow to sleep, however, everyone wants as many pillows as possible. Of cour…

eclipse wsdl2java_使用Eclipse的wsdl2java工具

一、用Eclipse调用Axis的wsdl2java1.在eclipse里面新建一个项目或已有的项目&#xff1b;2.导入activation.jaraxis.jarcommons-discovery.jarcommons-logging-1.0.3.jarjaxrpc.jarsaaj.jarwsdl4j-1.5.2.jar包3右击你的工程&#xff0d;Run As&#xff0d;Run...&#xff0d;右…

【POJ - 2785】4 Values whose Sum is 0 (二分,折半枚举)

题干&#xff1a; The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a b c d 0 . In the following, we assume that all lists have the same…

java好的博客_推荐5个万博爆款Java开源博客,是我目前用过最好用的博客系统

1.OneBlog一个简洁美观、功能强大并且自适应的Java博客&#xff0c;使用springboot开发&#xff0c;前端使用Bootstrap&#xff0c;支持移动端自适应&#xff0c;配有完备的前台和后台管理功能。功能简介多种编辑器、自动申请友情链接、百度推送、评论系统、权限管理、SEO、实时…

【UVALive - 3126】Taxi Cab Scheme (二分图,最小路径覆盖)

题目大意&#xff1a; 有n个出车安排&#xff0c;一辆车能接到这个安排的条件是&#xff1a;1、这辆车第一次发车&#xff1b;2、这辆车接了上一个安排&#xff0c;回到这个安排的起点的时间正好是这个安排的前一分钟或者更早 解题报告&#xff1a; 建图然后跑最小路径覆盖。…

java await signal_Java中的await()/signal()用法

二、方法await()/signal()在JDK5.0以后&#xff0c;JAVA提供了新的更加健壮的线程处理机制&#xff0c;包括了同步、锁定、线程池等等&#xff0c;可以实现更小粒度上的控制。await()和signal()就是其中用来同步的两种方法&#xff0c;功能基本上和wait()/notify()相同&#xf…

【HDU - 1083 】Courses (二分图)

题干&#xff1a; Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:…

java 主备切换_keepalived 实现 Java 服务的高可用(主备切换)

前言本文要说的是基于 keepalived 实现两台服务器之间的主备切换&#xff0c;从而实现 Java 服务的高可用。keepalived 的原理不多做介绍&#xff0c;自行搜索了解&#xff0c;keepalived 的安装部署请参考 keepalived 的安装及使用 。个人建议不要沉迷于 死扣 和 理解 原理&am…

【HDU - 2444】The Accomodation of Students(二分图判断 + 匈牙利算法求最大匹配)

题干&#xff1a; There are a group of students. Some of them may know each other, while others dont. For example, A and B know each other, B and C know each other. But this may not imply that A and C know each other. Now you are given all pairs of studen…

最长上升子序列 java_最长上升子序列 O(nlogn)解法 (java)

最长递增子序列问题&#xff1a;在一列数中寻找一些数&#xff0c;这些数满足&#xff1a;任意两个数a[i]和a[j]&#xff0c;若i 设dp[i]表示以i为结尾的最长递增子序列的长度&#xff0c;则状态转移方程为&#xff1a; dp[i] max{dp[j]1}, 1<j 这样简单的复杂度为O(n^2)&a…

C++关于引用的注意事项 总结知识点

对函数的引用&#xff1a; #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #def…

中介者模式java_图解Java设计模式之中介者模式

智能家庭项目1)智能家庭包括各种设备&#xff0c;闹钟、咖啡机、电视机、窗帘等2)主人要看电视时&#xff0c;各个设备可以协同工作&#xff0c;自动完成看电视的准备工作&#xff0c;比如流程为 &#xff1a;闹铃响起 - 》咖啡机开始做咖啡 -》窗帘自动落下 -》电视机开始播放…

python 自动驾驶线性识别路段

python视觉库 OpenCV:OpenCV是一个开源的计算机视觉库,提供了丰富的图像处理和计算机视觉算法。它可以用于图像处理、对象识别、特征提取、图像分割等。 Matplotlib:Matplotlib是一个绘图库,可用于创建高质量的二维图表和绘图。它提供了类似于MATLAB的绘图接口,使用户可以…

【CodeForces - 202A】LLPS (思维,字符串)

题干&#xff1a; This problems actual name, "Lexicographically Largest Palindromic Subsequence" is too long to fit into the page headline. You are given string s consisting of lowercase English letters only. Find its lexicographically largest p…