codeforces 689B B. Mike and Shortcuts(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 ito 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 1unit 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.
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 题意: 点i与点j的距离为|i-j|,而且第i个点到a[i]的距离为1,问第1个点到其他所有点的最短距离; 思路: bfs的水题,对于一个点i,距它为1的点有i-1,i+1,a[i],这样bfs就好了; AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio> using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=2e5+;
const int maxn=;
const double eps=1e-; int n,a[N],vis[N],dis[N];
queue<int>qu;
void bfs()
{
qu.push();
vis[]=;
while(!qu.empty())
{
int fr=qu.front();
qu.pop();
if(!vis[fr+])
{
vis[fr+]=;
dis[fr+]=dis[fr]+;
qu.push(fr+);
}
if(fr->&&!vis[fr-])
{ vis[fr-]=;
dis[fr-]=dis[fr]+;
qu.push(fr-); }
if(!vis[a[fr]])
{
vis[a[fr]]=;
dis[a[fr]]=dis[fr]+;
qu.push(a[fr]);
}
}
}
int main()
{
read(n);
For(i,,n)read(a[i]);
bfs();
For(i,,n)printf("%d ",dis[i]); return ;
}
codeforces 689B B. Mike and Shortcuts(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 689B Mike and Shortcuts (BFS or 最短路)
		
题目链接:http://codeforces.com/problemset/problem/689/B 题目大意: 留坑 明天中秋~
 - codeforces 361 B - Mike and Shortcuts
		
原题: Description Recently, Mike was very busy with studying for exams and contests. Now he is going t ...
 - CodeForces 689B	Mike and Shortcuts (bfs or 最短路)
		
Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...
 - Codeforces 689B. Mike and Shortcuts SPFA/搜索
		
B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...
 - 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 689 Mike and Shortcuts(最短路)
		
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
 - Codeforces Round #361 (Div. 2) B bfs处理
		
B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...
 - Codeforces 547C/548E - Mike and Foam 题解
		
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
 
随机推荐
- zoj 2724 Windows Message Queue
			
Windows Message Queue Time Limit: 2 Seconds Memory Limit: 65536 KB Message queue is the basic f ...
 - HDU 4821 字符串hash
			
题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...
 - POJ 2777 Count Color【线段树】
			
题目大意:要求完成以下两个操作:1.将一个区间刷上一种颜色2.询问一段区间上有多少种颜色 思路:这两个操作线段树都可以很迅速的完成,具体做法是:线段树上每个节点存这个线段上的颜色数量,由于颜色数很少, ...
 - hdu  1824  2-sat问题(判断)
			
/* 题意:u,v,w队长,队员,队长留下两个队员可以回家,两个队员留下,队长回家 2-sat问题,把两个队员看成一个整体就变成一个简单2-sat问题了 */ #include<stdio.h& ...
 - 腾讯云CVM使用记录--使用root权限
			
1.su root 指令 ,执行下列命令获取root权限: sudo /bin/su - root 注意:严禁执行password命令,root密码默认不能被修改.
 - 洛谷  P 1018 乘积最大 ==Codevs
			
题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸得 ...
 - java面向对象基础编程题
			
第一题: 设计一个形状类Shape,方法:求周长和求面积.形状类的子类:Rect(矩形),Circle(圆形).Rect类的子类:Square(正方形).不同的子类会有不同的计算周长和面积的方法1.总 ...
 - spring-boot-starter-data-redis与spring-boot-starter-redis两个包的区别
			
spring-boot-starter-data-redis: <?xml version="1.0" encoding="UTF-8"?> < ...
 - 删除,“windows setup 启用EMS”
			
方案1[笔者推荐]:进入Windows后按Windows+R输入msconfig回车进入系统配置,切换到引导,点击你要删除的选项然后点击删除就行[1].
 - Meteor Assets资源
			
静态服务器资源位于应用程序内的 private 子文件夹.在这个例子中,我们将学习如何从简单的JSON文件中使用数据. 第1步 - 创建文件和文件夹 让我们创建一个 private 文件夹并在这个文件 ...