Mike and Shortcuts

探讨了在一个特殊城市中,Mike如何利用捷径和常规路径寻找从家到各处的最低能量消耗路线,通过BFS算法实现最优路径计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门

题面:

Mike and Shortcuts

time limit per test:3 second
memory limit per test:256 megabytes
inputstandard:standard input
outputstandard: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.

Sample Input

3
2 2 3

5
1 2 3 4 5

7
4 4 4 4 7 7 7

Sample Output

0 1 2

0 1 2 3 4

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个点(1<=n<=200000),任意两个点之间的距离为这两点的序号之差绝对值,距离多远就要花费多少代价,并且每个点都会有一条捷径,通过该捷径到达对应点的代价为1,求每个点到第一个点的最小距离。注意捷径是单向的,点之间的路是双向的。

题目分析:

可以用bfs做,因为两点之间的代价是他们距离差的绝对值,就相当于从一个点往左或者往右走都花费1的代价,捷径也是,所以用bfs从第一个点开始往后遍历,每个点考虑往左、往右、走捷径三种情况,再取最小,最后直接输出答案。

代码:

#include<algorithm>
#include<stdio.h> 
#include<stdlib.h>
#include<iostream>
#include<queue>
#include<vector>
#include<string.h>
#include<math.h>
using namespace std;
int n,shcat[200005],ans[200005];
queue<int> q;


void bfs(){
	q.push(1);
	while(!q.empty()){
		int t=q.front();
		q.pop();
		if(t+1<=n&&ans[t]+1<ans[t+1]){
			ans[t+1]=ans[t]+1;
			q.push(t+1);
		}
		if(t-1>=1&&ans[t]+1<ans[t-1]){
			ans[t-1]=ans[t]+1;
			q.push(t-1);
		}
		if(ans[t]+1<ans[shcat[t]]){
			ans[shcat[t]]=ans[t]+1;
			q.push(shcat[t]);
		}
	}
}

int main(){
	cin>>n;
	memset(ans,0x3f3f3f3f,sizeof(ans));
	ans[1]=0;
	for(int i=1;i<=n;i++) cin>>shcat[i];
	bfs();
	for(int i=1;i<=n;i++) cout<<ans[i]<<' ';
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yjonben

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值