链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121473#problem/B

                                                       Mike and Shortcuts
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 ≠ piMike 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 1to 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.

题意:

n个城市排成一排,起点是第一个城市,每次可以向左或者向右走,到下一个城市消耗一个单位能量,每个城市有一个捷径可以直接花一个单位能量到达某一城市。问,到达这n个城市所要花费的最少的能量

题解:

求最短路径,用BFS,要注意的是只需要搜旁边两个城市,即,i+1,i-1.

源代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int a[200010];
int vis[200010];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    memset(vis,-1,sizeof(vis));
    vis[1]=0;
    queue<int> q;
    q.push(1);
    while(!q.empty())
    {
        int s=q.front();
        q.pop();
        for(int i=-1;i<=1;i++)
        {
            if(i==0)
                continue;
            int r=s+i;
            if(r>=1&&r<=n&&vis[r]==-1)
            {
                vis[r]=vis[s]+1;
                q.push(r);
            }
        }
        int d=a[s];
        if(vis[d]==-1)
        {
            vis[d]=vis[s]+1;
            q.push(d);
        }
    }
    printf("%d",vis[1]);
    for(int i=2;i<=n;i++)
        printf(" %d",vis[i]);
    printf("\n");
    return 0;

 

posted on 2016-07-12 07:54  尘埃。  阅读(255)  评论(0编辑  收藏  举报