Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
来源:互联网 发布:linux下载mysql的命令 编辑:程序博客网 时间:2024/06/06 10:05
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 sequencep1 = 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.
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).
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.
32 2 3
0 1 2
51 2 3 4 5
0 1 2 3 4
74 4 4 4 7 7 7
0 1 2 1 2 3 3
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,代表有n个城市,每个城市都会与其他城市有一条特殊通道,第二行就是给你每个城市与哪个城市连接,通过特殊通道到达只需1min,相邻的城市耗时也是1min,问你从第一个城市到达其他城市的最短时间。
代码
#include <bits/stdc++.h>using namespace std;int n;int z[200005],flag[200005]={0};queue <int> q;void bfs(){ int i; while(!q.empty()){ i=q.front(); //getchar(); //cout<<"tou:"<<q.front()<<endl; //cout<<"wei:"<<q.back()<<endl; if(flag[i-1]==-1&&i-1>0){ flag[i-1]=flag[i]+1; q.push(i-1); //cout<<"-1rudui:"<<i-1<<endl; } if(flag[i+1]==-1&&i+1<=n){ flag[i+1]=flag[i]+1; q.push(i+1); //cout<<"+1rudui:"<<i+1<<endl; //cout<<"z"<<endl; } if(flag[z[i]]==-1){ flag[z[i]]=flag[i]+1; q.push(z[i]); //cout<<"kuaijierudui:"<<z[i]<<endl; } //getchar(); //cout<<"tou:"<<q.front()<<endl; //cout<<"wei:"<<q.back()<<endl; q.pop(); //getchar(); //cout<<"tou:"<<q.front()<<endl; //cout<<"wei:"<<q.back()<<endl; }}int main(){ //freopen("cin.txt","r",stdin); int a; memset(flag,-1,sizeof(flag)); cin>>n; for(a=1;n>=a;a++){ cin>>z[a]; } q.push(1); flag[1]=0; bfs(); for(a=1;n>=a;a++){ cout<<flag[a]<<" "; } cout<<endl; return 0;}
第一次用队列,纪念一下,这道题应该算是最简单的bfs了吧。
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2) B - Mike and Shortcuts
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)
- Codeforces #361 (Div. 2)B-Mike and Shortcuts(spfa最短路)
- Codeforces Round #305 (Div. 2) B.Mike and Fun
- Codeforces Round #305 (Div. 2) B. Mike and Fun(水题)
- Codeforces Round #305 (Div. 2)B. Mike and Fun
- Codeforces Round #305 (Div. 2) C. Mike and Frog +B. Mike and Fun
- CodeForces 689B - Mike and Shortcuts
- codeforces 689B Mike and Shortcuts (bfs)
- CodeForces 689B BFS-Mike and Shortcuts
- day6 CodeForces 689B Mike and Shortcuts
- CodeForces 689B Mike and Shortcuts
- AndroidManifest.xml中一些常用的属性
- dos执行exp命令和imp命令
- Java入门第三季-1.异常与异常处理
- Linux 各目录的作用
- PHP学习总结(11)——PHP入门篇之WAMPServer多站点配置
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts
- 几种数值积分方法
- NOI 模拟试题(一)
- perl调用命令包含正则表达式,要注意转义符。[经验,还未证实]
- python中的静态类与方法
- 第35个python程序:分支和函数
- (android高仿系列)今日头条 --新闻阅读器 (二)
- 一个聚合的加解密工具类
- mysq数据从一个数据库的表复制到另一个数据库的表