日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa)

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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 energyinstead 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個點,主人公在1號點,現在假設相鄰點之間的距離為1(表述為),并且每個點都有一條到其他點的距離為1的捷徑(可以是自己,也可以是相鄰點,也可以是遠處的點,但是這條邊是單向邊)。定義了一下兩點之間最短路的定義,求1號點到其他點的最短距離是多少?

解題報告:

? ? 話說啊作為cf div2的B題,考最短路過分了啊雖然是裸題,,但是作為B題,,代碼超50行了過分了啊。。

AC代碼:

#include<bits/stdc++.h> #define ll long long #define pb push_back using namespace std; const int MAX = 2e5 + 5; ll dis[MAX]; bool vis[MAX]; int n,cnt; vector<ll> vv[MAX]; struct Point {int pos;ll c;Point(){}Point(int pos,ll c):pos(pos),c(c){}bool operator < (const Point & b) const{return c > b.c;} } ; void Dijkstra() {for(int i = 1; i<=n; i++) dis[i] = 0x3f3f3f3f3f3f;memset(vis,0,sizeof vis);dis[1] = 0;priority_queue<Point> pq;pq.push(Point(1,0));while(!pq.empty()) {Point cur = pq.top();pq.pop();if(vis[cur.pos]) continue;vis[cur.pos] = 1;int up = vv[cur.pos].size();for(int i = 0; i<up; i++) {int v = vv[cur.pos][i];if(vis[v]) continue;if(dis[v] > dis[cur.pos] + 1) {dis[v] = dis[cur.pos] + 1;pq.push(Point(v,dis[v]));}}} } int main() {cin>>n;int tmp;memset(head,-1,sizeof head);for(int i = 1; i<=n; i++) {scanf("%d",&tmp);vv[i].pb(tmp);}for(int i = 1; i<=n; i++) {if(i != 1) vv[i].pb(i-1);if(i != n) vv[i].pb(i+1);}Dijkstra();for(int i = 1; i<=n; i++) {printf("%lld%c",dis[i],i == n ? '\n' : ' ');}return 0 ;}

?

總結

以上是生活随笔為你收集整理的【CodeForces - 689B】Mike and Shortcuts(Dijkstra最短路,或者bfs跑状态类似spfa)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。