Test instructions: The shortest path from 1 to all points is obtained.
Analysis: Set two point u,v in the diagram, then dis[u][v] = ABS (U-V) or dis[u][v] = 1.
In fact, in a different perspective, we can find that each exercise consumes 1 of the energy. The energy consumed by each unit of time movement is 1.
There are three transitions in the state: 1, one step forward. 2, have to take a step. 3, take the shortcut.
/************************************************author:D arktongcreated TIME:2016/7/7 16:09:39File name:b . cpp*************************************************/#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<cmath>#include<cstdlib>#include<ctime>#defineINF 0x3f3f3f3f#defineESP 1e-9typedefLong LongLL;using namespacestd;Const intMAXN =200000+Ten;intD[MAXN], SH[MAXN], VIS[MAXN], N;voidsolve () {memset (d, INF,sizeof(d)); d[1]=0; Queue<int>Q; Q.push (1); vis[1]=vis[0]=1; while(!Q.empty ()) { intt =Q.front (); Q.pop (); if(d[t+1]>d[t]+1) {d[t+1]=d[t]+1; if(!vis[t+1]) Q.push (t+1); Vis[t+1]=1; } if(d[sh[t]]>d[t]+1) {D[sh[t]]=d[t]+1; if(!Vis[sh[t]] Q.push (sh[t]); Vis[sh[t]]=1; } if(d[t-1]>d[t]+1) {d[t-1]=d[t]+1; if(!vis[t-1]) Q.push (t1); Vis[t-1]=1; } }}intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);scanf"%d", &N); for(intI=1; i<=n;++i) scanf ("%d", &Sh[i]); Solve (); for(intI=1; i<=n;++i) printf ("%d%c", D[i], i==n?'\ n':' '); return 0;}
Codeforces 689b-mike and shortcuts