CSU-ACM2017暑期训练6-bfs G

来源:互联网 发布:四川广电网络招聘 编辑:程序博客网 时间:2024/05/16 15:46

题目:

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

Example
Input
32 2 3
Output
0 1 2 
Input
51 2 3 4 5
Output
0 1 2 3 4 
Input
74 4 4 4 7 7 7
Output
0 1 2 1 2 3 3 
Note

In the first sample case desired sequences are:

1: 1m1 = 0;

2: 1, 2m2 = 1;

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

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

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

1: 1m1 = 0;

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

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

4: 1, 4m4 = 1;

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

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

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



              题目大意:给你n个点,每个点到另一个点的消耗为两点坐标的差的绝对值。每个点还有一条到另一个点的绝对值,从这个点到那个点消耗为1,捷径是单向的,问你从1到n每个点到1这个点的最少消耗

             思路:相当于对一个图的每个点求他们到点1的单源最短距离。这个图中的路就是每个点比如点i到i-1和i+1有一条长为1的路,还有一条到a[i]的路长也为1.于是这题就变成了直接bfs搜从点1到各个点的最短距离


代码:


#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int INF = 1000000000;const int maxn = 222222;int T,n,m;int a[222222];int d[222222];int vis[222222];queue<int>q;void bfs(int x){    q.push(x);    while(!q.empty())    {        int t=q.front();        q.pop();        if(vis[t]==1)            continue;        vis[t]=1;        for(int i=-1;i<=1;i++)        {            if(i==0)            {                if(d[t]+1<d[a[t]])                    d[a[t]]=d[t]+1;                q.push(a[t]);            }            else            {                if(t+i>0&&t+i<=n&&d[t]+1<d[t+i])                {                    d[t+i]=d[t]+1;                    q.push(t+i);                }            }        }    }}int main(){    scanf("%d",&n);    for(int i=0;i<=n;i++)        d[i]=INF;    d[1] = 0;    for(int i=1;i<=n;i++)    {        scanf("%d",&a[i]);    }    bfs(1);    printf("%d",d[1]);    for(int i=2;i<=n;i++)        printf(" %d",d[i]);    return 0;}


原创粉丝点击