1. 程式人生 > 其它 >H - Mike and Shortcuts --BFS

H - Mike and Shortcuts --BFS

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 ofnintersections numbered from1ton. Mike starts walking from his house located at the intersection number1and goes along some sequence of intersections. Walking from intersection numberi

to intersectionjrequires|i - j|units of energy. Thetotal energyspent by Mike to visit a sequence of intersectionsp1 = 1, p2, ..., pkis equal tounits of energy.

Of course, walking would be boring if there were no shortcuts. Ashortcutis a special path that allows Mike walking from one intersection to another requiring only1unit of energy. There are exactlyn

shortcuts in Mike's city, theithof them allows walking from intersectionito intersectionai(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 sequencep1 = 1, p2, ..., pkthen for each1 ≤ i < ksatisfyingp
i + 1 = apiandapi ≠ piMike will spendonly1unit of energyinstead of|pi - pi + 1|walking from the intersectionpito intersectionpi + 1. For example, if Mike chooses a sequencep1 = 1, p2 = ap1, p3 = ap2, ..., pk = apk - 1, he spends exactlyk - 1units 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 each1 ≤ i ≤ nMike is interested in finding minimum possible total energy of some sequencep1 = 1, p2, ..., pk = i.

Input

The first line contains an integern(1 ≤ n ≤ 200 000)— the number of Mike's city intersection.

The second line containsnintegersa1, a2, ..., an(i ≤ ai ≤ n,, describing shortcuts of Mike's city, allowing to walk from intersectionito intersectionaiusing only1unit of energy. Please note that the shortcuts don't allow walking in opposite directions (fromaitoi).

Output

In the only line printnintegersm1, m2, ..., mn, wheremidenotes the least amount of total energy required to walk from intersection1to intersectioni.

Examples

input

3
2 2 3

output

0 1 2

input

7
4 4 4 4 7 7 7

output

0 1 2 1 2 3 3

題目大意

      由於每段路徑(非捷徑)的長度為1,每走過一個長度單位所消耗的能量也是1,可以只考慮長度為一的路徑。這樣一來,所有路徑(包括捷徑)都可以一視同仁,
因為每次只前進一步,一步消耗一單位能量。可以採用BFS來邊走邊標記走到每一點所需的最少能量; 一條大路上的每個節點都有通向前面某個點的捷徑,捷徑只能向前走,求最短路程。注意捷徑雖然是單向的但是大路可以是雙向的,每次搜尋要判三種情況。 另BFS是適合找最短路,找到即最優;DFS是所有解果都會搜到

程式碼實現

#include <bits/stdc++.h>
using namespace std;
int arr[200005];  //每個位置i到1的距離
int shortt[200005];//儲存捷徑 i->shortt[i] 的距離為1
struct point{
    int now; //當前點位
    int step; //步數 即距離
};
int main(){
    int n;
    cin>>n;
    memset(arr,-1,sizeof(arr));
    for(int i=1;i<=n;i++)
        cin>>shortt[i];
    queue<point> q; 
    point start; start.now=1,start.step=0; //初始化資料 當前位置 1;距離 0.
    q.push(start);
    while(!q.empty()){ //BFS程式碼
          point cur;  //cur:下一個
          point temp=q.front(); //temp :當前
          int now=temp.now;
          int step=temp.step;
          q.pop();
        if(now+1<=n&&arr[now+1]==-1){ //向右走
            arr[now+1]=step+1;     //距離加一
            cur.now=now+1; cur.step=step+1; //將該點位push進佇列
            q.push(cur);
         }if(arr[shortt[now]]==-1){ //若在i這個位置又捷徑 直接走捷徑
            arr[shortt[now]]=step+1;  //距離加一
            cur.now=shortt[now];cur.step=step+1;
            q.push(cur);
        }if(now-1>1&&arr[now-1]==-1){//向左走
            arr[now-1]=step+1;
            cur.now=now-1; cur.step=step+1;
            q.push(cur);
        }
    }
    arr[1]=0; //1號點是0
    for(int i=1;i<=n;i++){
        cout<<arr[i]<<" ";
    }

    return 0;
}