Codeforces Round #361 (Div. 2)——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 energyspent 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.
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.
这题基本不能用SPFA,20W个点,边多到存不下,SPFA会WA4,然后试了下BFS,也WA4,发现有个坑,第四组中到第10个点的最短距离是8,是1走捷径到17,再一步一步往回走到10即1+7=8而不是直接从1走到10的直接距离9,显然这里需要往回走。BFS里面多写一个往回走的方向搜索就好了
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=200010; int vis[N];
int cut[N];
int d[N];
queue<pii>Q; void init()
{
MM(vis,0);
MM(cut,-1);
MM(d,0);
while (!Q.empty())
Q.pop();
}
void bfs(const int &s)
{
Q.push(pii(s,0));
while (!Q.empty())
{
pii now=Q.front();
Q.pop();
vis[now.first]=1;
d[now.first]=now.second;
int v;
v=cut[now.first];
if(!vis[v]&&v!=-1)
{
Q.push(pii(v,now.second+1));
vis[v]=1;
}
v=now.first+1;
if(!vis[v])
{
Q.push(pii(v,now.second+1));
vis[v]=1;
}
v=now.first-1;
if(!vis[v])
{
Q.push(pii(v,now.second+1));
vis[now.first-1]=1;
}
}
return ;
}
int main(void)
{
int n,i,j,a,b;
while (~scanf("%d",&n))
{
init();
for (i=1; i<=n; i++)
{
scanf("%d",&a);
cut[i]=a;
}
bfs(1);
for (i=1; i<=n; i++)
printf("%d%s",d[i],i==n?"\n":" ");
}
return 0;
}
Codeforces Round #361 (Div. 2)——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 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) C. Mike and Chocolate Thieves 二分
C. Mike and Chocolate Thieves 题目连接: http://www.codeforces.com/contest/689/problem/C Description Bad ...
- Codeforces Round #361 (Div. 2) A. Mike and Cellphone 水题
A. Mike and Cellphone 题目连接: http://www.codeforces.com/contest/689/problem/A Description While swimmi ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 【逆元求组合数 && 离散化】
任意门:http://codeforces.com/contest/689/problem/E E. Mike and Geometry Problem time limit per test 3 s ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem 离散化+逆元
E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #361 (Div. 2)A. Mike and Cellphone
A. Mike and Cellphone time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem
题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
随机推荐
- 进程间通信,把字符串指针作为参数通过SendMessage传递给另一个进程,不起作用
参数发送进程: CString csCmd=AfxGetApp()->m_lpCmdLine; if (!csCmd.IsEmpty()) { pWndPrev->SendMessage( ...
- 【2016新年版】年度精品 XP,32/64位Win7,32/64位Win8,32/64位Win10系统
本系统是10月5日最新完整版本的Windows10 安装版镜像,win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为win10 Edge浏览器中国默认主页和搜索引擎 ...
- HDU 1964 Pipes (插头DP,变形)
题意:给一个n*m的矩阵,每个格子都是必走的,且无障碍格子,每对格子之间都有一个花费,问哈密顿回路的最小花费. 思路: 这个和Formula1差不多,只是求得是最小花费,这只需要修改一下DP值为花费就 ...
- 洛谷 P1334 瑞瑞的木板==P2664 【题目待添加】
题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每根的长度为整数Li(1≤Li≤50,000).于是,他神奇地买了一根足够长的木板,长度为 ...
- 添加/删除 windows下Git右键菜单
从网上搜索了些方法,总结一下 不显示右键菜单: 方法1: 安装的时候选择不添加右键菜单. 方法2(绝对有效): 运行CMD Windows 64 输入命令(地址为git安装地址) cd "C ...
- sourceTree配置bitbucket
1. 为github增加账号信息 选择添加远程库 选择添加一个账号 输入用户名: 按照提示输入密码 选择bitbuchet为默认 选中搜索克隆
- Java动态代理之InvocationHandler最简单的入门教程
网上关于Java的动态代理,Proxy和InvocationHandler这些概念有讲解得非常高深的文章.其实这些概念没有那么复杂.现在咱们通过一个最简单的例子认识什么是InvocationHandl ...
- Clusterware 和 RAC 中的域名解析的配置校验和检查 (文档 ID 1945838.1)
适用于: Oracle Database - Enterprise Edition - 版本 10.1.0.2 到 12.1.0.1 [发行版 10.1 到 12.1]Oracle Database ...
- 带二级目录的Nginx配置------目前找到的最简单的方法
由于项目不知一个,所以不得不为每一个项目建一个专有的文件夹,这就导致了在配置nginx的时候会出现二级目录 目前找到的最简单的方法 - step1:修改 vue.config.js 添加配 ...
- Linux系统GEDIT编译运行C++
作为NOIP第一年强制使用Linux系统的考生,真的很难受,被迫还要学一波Linux系统. 正常的Windows对于较基础的程序员来说非常方便好用,但是对于高级程序员来说就是一个坑,于是就有了Linu ...