CodeForces 689BMike and Shortcuts (bfs or 最短路)
題目鏈接:
http://acm.hust.edu.cn/vjudge/contest/121333#problem/F
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.
Sample Input
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
Hint
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.
題意:
圖中任意兩點之間距離為abs(i-j),每個點有且僅有一條捷徑a[i]并且距離為1;
求從1開始遍歷所有點的最小代價;
題解:
只考慮相鄰兩點和捷徑這三條長度為1的邊,最短路即可;
或者直接考慮上述三條邊跑一遍bfs.
(以下代碼為bfs法)
代碼:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<queue> #include<set> #include<list> #define LL long long #define maxn 210000 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); #define OUT freopen("out.txt","w",stdout); using namespace std;int n; queue<int> myq; int dis[maxn]; int a[maxn];int main(int argc, char const *argv[]) {//IN;while(scanf("%d",&n) != EOF){for(int i=1; i<=n; i++) scanf("%d", &a[i]);fill(dis, dis+maxn, inf);while(!myq.empty()) myq.pop();myq.push(1); dis[1] = 0;while(!myq.empty()) {int cur = myq.front(); myq.pop();if(cur+1<=n && dis[cur+1] > dis[cur]+1){dis[cur+1] = dis[cur]+1;myq.push(cur+1);}if(cur-1>0 && dis[cur-1] > dis[cur]+1){dis[cur-1] = dis[cur]+1;myq.push(cur-1);}if(dis[a[cur]] > dis[cur]+1){dis[a[cur]] = dis[cur]+1;myq.push(a[cur]);}}for(int i=1; i<=n; i++)printf("%d ", dis[i]);;}return 0; }轉載于:https://www.cnblogs.com/Sunshine-tcf/p/5693308.html
總結
以上是生活随笔為你收集整理的CodeForces 689BMike and Shortcuts (bfs or 最短路)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 17.1.1.3 Creating a
- 下一篇: web标准常见问题整理