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 ...
随机推荐
- OneNote- 面经
- java连接MySQL数据库操作步骤
package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connecti ...
- SunmmerVocation_Learning--Java数组的创建
一维数组声明方式: type var[] 或 type[] var; 如int a[], int[] a; Java中声明数组不能指定其长度,如int a[5]是非法的. 一维数组对象的创建: Jav ...
- 1452: [JSOI2009]Count
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 3135 Solved: 1828[Submit][Status][Discuss] Descripti ...
- django+xadmin在线教育平台(三)
通过留言版功能回顾django基础知识 将对于django目录结构,使用Django快速搭建可以提交的表单页面,models.py , urls.py, views.py. 从数据库中取出数据展示到h ...
- nuxt.js服务端缓存lru-cache
对于部分网页进行服务端的缓存,可以获得更好的渲染性能,但是缓存又涉及到一个数据的及时性的问题,所以在及时性和性能之间要有平衡和取舍. 官方文档里面写的使用方法 按照这个配置,试过了没什么用,但是从文档 ...
- 面试前赶紧看了5道Python Web面试题,Python面试题No17
目录 本面试题题库,由公号:非本科程序员 整理发布 第1题: Flask中的请求上下文和应用上下文是什么? 第2题:django中间件的使用? 第3题: django开发中数据做过什么优化? 第4题: ...
- python3 练习题100例 (十三)
题目十三:将一个正整数分解质因数.例如:输入60,打印出60=2*2*3*5. #!/usr/bin/env python3 # -*- coding: utf-8 -*- ""& ...
- Linux磁盘与文件管理系统
基本上Linux的正统文件系统为Ext2,该文件系统内的信息主要有: superblock:记录此filesystem的整体信息,包括inode/block的总量,使用量,剩余量,以及文件系统的格式与 ...
- 微信公众开发api接口
简介 微信公众平台消息接口为开发者提供了一种新的消息处理方式.微信公众平台消息接口为开发者提供与用户进行消息交互的能力.对于成功接入消息接口的微信公众账号,当用户发消息给公众号,微信公众平台服务器 ...