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

B. Mike and Shortcuts

题目连接:

http://www.codeforces.com/contest/689/problem/B

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

3
2 2 3

Sample Output

0 1 2

Hint

题意

你从i到j的代价是abs(j-i)

现在每个点有一条路ai,可以使得i走到ai的花费为1

现在问你从1走到剩下的点的代价是多少

题解

直接bfs就好了,i就只走周围的两个点就好了

代码

#include<bits/stdc++.h>
using namespace std;

const int maxn = 2e5+7;
vector<int> E[maxn];
int a[maxn];
int d[maxn];
int vis[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<=n;i++)
        d[i]=1e9;
    queue<int>Q;
    Q.push(1);
    d[1]=0;
    while(!Q.empty()){
        int now=Q.front();
        Q.pop();
        if(now!=n&&d[now+1]>d[now]+1){
            d[now+1]=d[now]+1;
            Q.push(now+1);
        }
        if(now!=1&&d[now-1]>d[now]+1){
            d[now-1]=d[now]+1;
            Q.push(now-1);
        }
        if(d[a[now]]>d[now]+1){
            d[a[now]]=d[now]+1;
            Q.push(a[now]);
        }
    }
    for(int i=1;i<=n;i++)
        printf("%d\n",d[i]);
    printf("\n");
}
posted @ 2016-07-07 15:47  qscqesze  阅读(382)  评论(0编辑  收藏  举报