CodeForces 689B Mike and Shortcuts

来源:互联网 发布:西安软件公寓申请 编辑:程序博客网 时间:2024/09/21 08:18
B. Mike and Shortcuts
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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, i and 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<=n<=200000),任意两个节点的距离为这两点的序号之差绝对值。且每个点都会有一个捷径,通过该捷径到达对应点的代价为1,求每个点到点1的最小距离。
思路:图比较大,不能直接存,要优化,当然不能用邻接矩阵存,要用邻接表,再用spfa求最短路 ,另外由于路径的特殊性只需要存1->2->3->4.....的最短路。
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>#define INF 0xfffffff#define MAX 200000using namespace std;struct node{int u,v,w,next;}edge[MAX*3];int head[MAX*2];int vis[MAX*2];int dis[MAX*2];int num=0;void add(int u,int v,int w){edge[num].u=u;edge[num].v=v;edge[num].w=w;edge[num].next=head[u];head[u]=num++;}void spfa(){memset(vis,0,sizeof(vis));int k,i,j;queue<int>q;q.push(1);vis[1]=1;dis[1]=0;while(!q.empty()){k=q.front();q.pop();vis[k]=0;for(i=head[k];i!=-1;i=edge[i].next){int temp=dis[edge[i].u]+edge[i].w;if(temp<dis[edge[i].v]){dis[edge[i].v]=temp;if(vis[edge[i].v]==0){q.push(edge[i].v);vis[edge[i].v]=1;}}}}}int main(){int n,i,a;while(scanf("%d",&n)!=EOF){memset(head,-1,sizeof(head));for(i=1;i<=n;i++)dis[i]=INF;for(i=1;i<=n;i++){scanf("%d",&a);if(a!=i)add(i,a,1);}for(i=2;i<=n;i++){add(i-1,i,1);add(i,i-1,1);}spfa();for(i=1;i<n;i++)printf("%d ",dis[i]);printf("%d\n",dis[i]);}return 0;}


0 0
原创粉丝点击