CodeForces 689B BFS-Mike and Shortcuts

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.

题意:


存在从1到n个结点,从i结点到j结点的距离为j-i(j >= i),同时存在一些捷径,如果捷径从i到j,则i、j两点的距离为1,从1结点出发,求到其他结点的最小长度。


题解:


深度优先搜索问题,从一个结点u出发,要找离它最近的点进行搜索,即u+1,u-1和与u之间存在捷径的v点,这样子就可以找到到所有点最短的距离。


#include <iostream>
#include <queue>
#include <string.h>
#include <stdio.h>
#include <cmath>
#define MAXN 200000+1
using namespace std;

int vis[MAXN];
int st[MAXN];
int dis[MAXN];
int n;
queue<int> q;

void bfs()
{
    while(!q.empty()) q.pop();
    q.push(1);
    vis[1] = 0;
    dis[1] = 0;
    while(!q.empty())
    {
        int u = q.front();
        dis[u] = vis[u];
        q.pop();
        if(u-1 > 0 && vis[u-1] == -1)
        {
            vis[u-1] = vis[u] + 1;
            q.push(u-1);
        }
        if(u+1 <= n && vis[u+1] == -1)
        {
            vis[u+1] = vis[u] + 1;
            q.push(u+1);
        }
        if(vis[st[u]] == -1)
        {
            q.push(st[u]);
            vis[st[u]] = vis[u] + 1;
        }
    }
}

int main()
{
    while(cin >> n)
    {
        memset(st, 0, sizeof(st));
        memset(dis, 0, sizeof(dis));
        memset(vis, -1, sizeof(vis));
        for(int i = 1; i <= n; i++)
            scanf("%d", &st[i]);
        bfs();
        for(int i = 1; i <= n; i++)
        {
            printf("%d", dis[i]);
            if(i != n)
                printf(" ");
            else
                printf("\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值