Mike and Shortcuts (CF bfs)

来源:互联网 发布:淘宝降价提醒"管理器 编辑:程序博客网 时间:2024/05/16 16:07

问题描述:
B. Mike and Shortcuts
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
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
Note
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个城市,从i城市走到j城市需要的步数是|i-j|。
每个城市都有一条捷径,走捷径的话所需要的步数是1;
问从1开始走向这n个城市需要的最小花费是多少。

思路分析:

这个城市特别多,每个城市又是相通的。所以整个地图会特别大。但是有很多是没有用的。
如果不考虑捷径的话,除1以外的其他节点直接到节点1的距离和通过他的前继到达节点1的距离是一样的,利用这个特殊性,我们就可以省去大部分边。
那么这样的话,每个点只有3条路。通向下一点,上一点和捷径相通的一点。
bfs一下就好了。

ac代码:

#include<bits/stdc++.h>using namespace std;#define N 2000005int T,n,m;int a[N];int ans[N];bool vis[N];struct ww{    int num;    int pos;};queue<ww> Q;void bfs(){    while(!Q.empty())    {        ww v;        v=Q.front();        ans[v.num]=v.pos;        Q.pop();        ww x,y,z;        x.num=v.num+1;        x.pos=v.pos+1;        y.num=a[v.num];        y.pos=v.pos+1;        z.num=v.num-1;        z.pos=v.pos+1;        if(x.num>0 && x.num<=T && !vis[x.num])        {            vis[x.num]=1;            Q.push(x);        }        if(y.num>0 && y.num<=T && !vis[y.num])        {            vis[y.num]=1;            Q.push(y);        }        if(z.num>0 && z.num<=T && !vis[z.num])        {            vis[z.num]=1;            Q.push(z);        }    }}int main(){    while(~scanf("%d",&T))    {        memset(vis,0,sizeof(vis));        memset(ans,0,sizeof(ans));        int i,j,k;        for(i=1;i<=T;i++)            scanf("%d",&a[i]);        ww v;        v.pos=0;        v.num=1;        vis[1]=1;        Q.push(v);        bfs();        cout<<ans[1];        for(i=2;i<=T;i++)            cout<<" "<<ans[i];        cout<<endl;    }    return 0;}
0 0
原创粉丝点击