B. Mike and Shortcuts
time limit per test:

3 seconds

memory limit per test:

256 megabytes

input:

standard input

output:

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 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.

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.

题目链接:http://codeforces.com/contest/689/problem/B


题意:任意两点的距离为两点序号差的绝对值,有一些特殊的点,i到ai的距离为1.求1到每个点的最短距离。

思路:SPFA模板题。任意两个编号相邻的点的距离为1构造双向边,再加上n个特殊点构成的边。因为n最大为200000,套用SPFA模板。

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 6e5+, mod = 1e9 + , inf = 0x3f3f3f3f;
struct node
{
int to,d;
} edge[*MAXN];
int head[MAXN],nextt[*MAXN];
int sign[MAXN];
queue<int>Q;
int dist[MAXN];
int n;
void add(int i,int u,int v,int d)
{
edge[i].to=v;
edge[i].d=d;
nextt[i]=head[u];
head[u]=i;
}
void SPFA(int v)
{
int i,u;
for(i=; i<=n; i++)
{
dist[i]=inf;
sign[i]=;
}
dist[v]=;
Q.push(v);
sign[v]=;
while(!Q.empty())
{
u=Q.front();
Q.pop();
sign[u]=;
i=head[u];
while(i!=)
{
if(dist[edge[i].to]>dist[u]+edge[i].d)
{
dist[edge[i].to]=dist[u]+edge[i].d;
if(!sign[edge[i].to])
{
Q.push(edge[i].to);
sign[edge[i].to]=;
}
}
i=nextt[i];
}
}
}
int a[];
int main()
{
int i,j;
scanf("%d",&n);
memset(head,,sizeof(head));
j=;
for(i=; i<=n; i++)
{
scanf("%d",&a[i]);
if(i!=a[i]) add(j++,i,a[i],);
if(i>)
{
add(j++,i-,i,);
add(j++,i,i-,);
}
}
SPFA();
for(i=; i<=n; i++)
cout<<dist[i]<<" ";
cout<<endl;
return ;
}

SPFA

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 6e5+, mod = 1e9 + , inf = 0x3f3f3f3f;
vector<int>V[];
int dist[];
void DFS(int u)
{
int i;
for(i=; i<V[u].size(); i++)
{
if(dist[V[u][i]]>dist[u]+)
{
dist[V[u][i]]=dist[u]+;
DFS(V[u][i]);
}
}
}
int main()
{
int i,n,a;
scanf("%d",&n);
for(i=; i<=n; i++)
{
scanf("%d",&a);
V[i].push_back(a);
if(i+<=n) V[i].push_back(i+);
if(i->=) V[i].push_back(i-);
}
for(i=; i<=n; i++) dist[i]=inf;
dist[]=;
DFS();
for(i=;i<=n;i++)
cout<<dist[i]<<" ";
cout<<endl;
return ;
}

DFS

Codeforces 689B. Mike and Shortcuts SPFA/搜索的更多相关文章

  1. CodeForces 689B Mike and Shortcuts (bfs or 最短路)

    Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...

  2. CodeForces 689B Mike and Shortcuts (BFS or 最短路)

    题目链接:http://codeforces.com/problemset/problem/689/B 题目大意: 留坑 明天中秋~

  3. codeforces 689B Mike and Shortcuts 最短路

    题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1.问1到各个点之间的最短距离. 题目思路:SPFA求最短路 #incl ...

  4. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  5. codeforces 689B B. Mike and Shortcuts(bfs)

    题目链接: B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input ...

  6. 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 ...

  7. 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 ...

  8. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

  9. codeforces 547E Mike and Friends

    codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...

随机推荐

  1. sqlserver操作命令

    启动命令:Net Start MSSqlServer 暂停命令:Net Pause MSSqlServer 重新启动暂停的命令:Net Continue MSSqlServer 停止命令:Net st ...

  2. 逆地址解析协议RARP

    解决的问题 一般系统启动时,从引导磁盘中获取ip 有些机器没有引导磁盘,如X终端或无盘工作站,则需要采用其他方法来获得IP地址 解决的过程 无盘系统依据RARP协议 从接口卡上读取唯一的硬件地址,然后 ...

  3. MySQL优化十大技巧

    转自:https://m.2cto.com/database/201701/557910.html MYSQL优化主要分为以下四大方面: 设计:存储引擎,字段类型,范式与逆范式 功能:索引,缓存,分区 ...

  4. 使用Teleport Ultra批量克隆网站,使用Easy CHM合并生成chm文件

    1.要下载的页面 http://www.howsoftworks.net/javaapi/ 2. 下载Teleport Ultra 3.使用Teleport Ultra批量克隆网站 4.下载Easy ...

  5. Activity服务类-6 ManagementService服务类

    一共含有17个方法 // 获取包含了Activiti数据库模式的{表名.行计数}项的映射.Map<String, Long> getTableCount();//获取诸如任务.执行之类的A ...

  6. Java8函数之旅(四) --四大函数接口

    前言   Java8中函数接口有很多,大概有几十个吧,具体究竟是多少我也数不清,所以一开始看的时候感觉一脸懵逼,不过其实根本没那么复杂,毕竟不应该也没必要把一个东西设计的很复杂. 几个单词   在学习 ...

  7. ios 真机测试与发布详细流程,基于最新的开发者网站,ios7,xcode5(有截图的哦)[[[第一部分真机测试]]]

    转载于:http://blog.csdn.net/lv_ruanruan/article/details/14446597 真机测试及发布详细流程,最新版 第一次一个人搞一个项目,我们老大规定,一个周 ...

  8. Spring 集成Hibernate的三种方式

    首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件 ...

  9. SQL事务日志备份时的问题

    1.在进行事务日志备份的时候,如下图: 3041 消息的疑难解答时的考虑事项:不会只是一个数据库或所有数据库出现问题吗?是备份到本地存储区或远程存储吗?哪种类型的备份 (数据库备份. 日志备份和差异备 ...

  10. Linux下tar.gz 安装

    将安装文件拷贝至你的目录中 如果是以root身份登录上的,就将软件拷贝至/root中. cp xxx.tar.gz /root 解压缩包 tar xvzf xxx.tar.gz 切换到安装目录下 cd ...