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 ...
随机推荐
- Drupal常用的模块
CCK (Content Construction Kit ) : 添加字段模块 Views:生成列表 Tinymce:(Wysiwyg Editor) 常用的编辑器之一 Ajax Form Buil ...
- AJAX Control Toolkit的AsynFileUpload控件资料收集
基于AJAX的文件上传显示进度条实现 http://plkong.iteye.com/blog/238159 asp.net ajax AjaxFileUpload使用 多文件上传 http://bl ...
- 使用Maven开发一个简单的SpringData
1:创建Maven项目 2:添加依赖(修改pom.xml为以下代码) <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
- Centos 6版本Device eth0 does not seem to be present,delaying initialization.故障处理
1.1 故障现象 2019年06月14日晚上,公司项目组说有台业务服务器连接不上,比较着急,我通过vpn拨入的方式远程登录到管理控制台查看发现网卡没有获取到IP地址,我尝试重启来重新启动,重启的时候 ...
- Linux下启动、停止xampp命令
启动xampp: /opt/lampp/./lampp start 停止xampp: /opt/lampp/./lampp stop 卸载xampp: rm -rf /opt/lampp
- 洛谷 P2279 [HNOI2003]消防局的设立
题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...
- 使用JFreeChart生成报表
1.JFreeChart简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications,servlets以及JSP等使用所设计. ...
- vscode添加Astyle
1.安装astyle插件,在应用商城里面一键安装即可.2.下载astyle的bin文件,并添加到系统环境变量.3.打开vscode的settings.json,添加以下代码. { "edit ...
- char* 与char[]区别
[转] 最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区(认为无区别),导致有时出现“难以理解”的错误.一时也不能说得很明白,网上也 ...
- ElasticSearch学习笔记(一)-- 查询索引分词
# 查看所有索引 GET _cat/indices # 创建一个索引 PUT /test_index # 插入一条数据(指定id)PUT /test_index/doc/ { "userna ...