Codeforces Round #361 (Div. 2) B bfs处理
3 seconds
256 megabytes
standard input
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.
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.
3
2 2 3
0 1 2
5
1 2 3 4 5
0 1 2 3 4
7
4 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.
题意: 从位置1出发到其他位置,距离为|i - j| 但是存在近路一说,也就是可以直接从i到ai 距离为1
输出从位置1到其他位置的最少距离
题解:最少距离bfs处理
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct node
{
int pos;
int step;
}N[],now,exm;
int n;
int a[];
int b[];
int ans[];
queue<node>q;
int main()
{
scanf("%d",&n);
memset(b,,sizeof(b));
for(int i=;i<=n;i++)
N[i].pos=i;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
exm.pos=;
exm.step=;
b[]=;
q.push(exm); while(!q.empty())
{
exm=q.front();
// cout<<exm.pos<<" "<<exm.step<<endl;
ans[exm.pos]=exm.step;
q.pop();
for(int i=;i<=;i++)
{
if(i==)
{
if(b[exm.pos+]==&&exm.pos+<=n)
{
now.pos=exm.pos+;
now.step=exm.step+;
q.push(now);
b[now.pos]=;
}
}
if(i==)
{
if(b[exm.pos-]==&&exm.pos->=)
{
now.pos=exm.pos-;
now.step=exm.step+;
q.push(now);
b[now.pos]=;
}
}
if(i==)
{
if(b[a[exm.pos]]==&&a[exm.pos]>=&&a[exm.pos]<=n)
{
now.pos=a[exm.pos];
now.step=exm.step+;
q.push(now);
b[a[exm.pos]]=;
}
}
}
}
cout<<ans[];
for(int i=;i<=n;i++)
printf(" %d",ans[i]);
return ;
}
Codeforces Round #361 (Div. 2) B bfs处理的更多相关文章
- Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs
B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...
- Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)
B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #361 (Div. 2) B
B - Mike and Shortcuts Description Recently, Mike was very busy with studying for exams and contests ...
- Codeforces Round #325 (Div. 2) D bfs
D. Phillip and Trains time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #361 (Div. 2)
A 脑筋急转弯 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream&g ...
- Codeforces Round #361 (Div. 2) C.NP-Hard Problem
题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化 排列组合
E. Mike and Geometry Problem 题目连接: http://www.codeforces.com/contest/689/problem/E Description Mike ...
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...
- Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves 二分
C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...
随机推荐
- 从多个textarea中随机选取一个内容
<div id="IMContentTest"> <textarea name="IMContent" class="IMClass ...
- TO_DATS() AS ABAP_DATE
有的时候一个想不到的小问题,,才是致命的问题!
- JS的四舍五入问题
最近踩了一个坑,mark一下toFixed四舍五入问题,详见代码: var myFixed = function(num, fix) { num = (parseFloat(num) * + ) / ...
- CodeForces 651B
#include <cstdio> #include <algorithm> using namespace std; int a[1005], n, temp, maxk; ...
- A Bug's Life(削弱版食物链)
Description Background Professor Hopper is researching the sexual behavior of a rare species of bug ...
- 20181229(守护进程,互斥锁,IPC,生产者和消费者模型)
一.守护进程 守护进程:一个进程B守护另一个进程A,当被守护的进程A结束,进程B也就结束了.(不一定同生,但会同死) 两个特点: ①守护进程会在主进程代码执行结束后就终止 ②守护进程内无法再开启子进程 ...
- 删除项目开发中的.pyc文件
在实际开发中python会自动生成很多pyc文件,但是这些pyc文件是不需要我们追踪的,删除了对项目也没有影响,下面是删除pyc文件的方法. Linux或Mac系统 find /tmp -name & ...
- 离线安装 Visual Studio Express 而不下载整个镜像文件的方法(转载)
转 visual studio 2010 express 全序列号 phone开发工具YDK44-2WW9W-QV7PM-8P8G8-FTYDF VC# 2010 Express: PQT8W-68Y ...
- Farm Tour POJ - 2135 (最小费用流)
When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= ...
- HDU_6194 后缀数组+RMQ
好绝望的..想了五个多小时,最后还是没A...赛后看了下后缀数组瞬间就有了思路...不过因为太菜,想了将近两个小时才吧这个题干掉. 首先,应当认为,后缀数组的定义是,某字符串S的所有后缀按照字典序有小 ...