CodeForces 689B - Mike and Shortcuts
2016-07-08 10:15
417 查看
题目
B. Mike and Shortcutstime 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 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 se
b0f1
quence
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.
题意
n个点标号为1到n,每个点都有一条有向边连向某个点,边权为1,各个点之间都有一条无向边相连,权值为标号的差值,问1号点到其他点的最短路。解法
虽然边很多,但并不是每条边都会用到,只需保留权值为1的边,怎样就只有40w条边,用spfa或dijkstra都可以。代码
#include<cstdio>#include<cstring>
#include<queue>
using namespace std;
#define N 200005
struct edge
{
int to,next,cost;
}e[N*4];
struct node
{
int id,dis;
friend bool operator <(node a,node b)
{
return a.dis>b.dis;
}
};
priority_queue <node> q;
int head
,cnt,dis
,vis
;
void addedge(int u,int v,int c)
{
e[cnt].cost=c;
e[cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt++;
}
int main()
{
int n,i,x;
scanf("%d",&n);
memset(head,-1,sizeof(head));
cnt=0;
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(x!=i)
addedge(i,x,1);
if(i!=n)
{
addedge(i,i+1,1);
addedge(i+1,i,1);
}
}
dis[1]=0;
node now,tmp;
tmp.id=1;
tmp.dis=0;
q.push(tmp);
int num=0;
while(!q.empty())
{
now=q.top();
q.pop();
if(vis[now.id])
continue;
vis[now.id]=1;
num++;
if(num==n)
break;
for(i=head[now.id];~i;i=e[i].next)
{
int v=e[i].to;
if(vis[v])
continue;
if(dis[v]==0||dis[now.id]+e[i].cost<dis[v])
{
dis[v]=dis[now.id]+e[i].cost;
tmp.id=v;
tmp.dis=dis[v];
q.push(tmp);
}
}
}
for(i=1;i<=n;i++)
printf("%d ",dis[i]);
puts("");
return 0;
}
相关文章推荐
- Apache Shiro 整合Spring 进行权限验证 以及在Freemarker中使用shiro标签
- 1122. Prerequisites?
- Hibernate的简介
- CSS+JS滚动图片功能代码
- 点击itemView选中checkbox
- C语言strchr()函数:查找某字符在字符串中首次出现的位置
- centos安装freetds使php支持sqlserver操作
- Linux是这样泄露内存的:Linux内存泄露过程观察
- Android fragment 重叠问题的解决方法
- 正则表达式小括号的多义性
- Unmarshalling Error: unexpected element 错误的解决
- Android Studio利用异步任务AsyncTask发送post请求获取json数据
- Android 2016新技术
- iOS-最全的App上架教程
- BZOJ3223 Tyvj 1729 文艺平衡树
- MySQL计划任务(事件调度器)(Event Scheduler)
- php 将网页执行的输出写入到本地文件中
- centos7 virt-manager制作windows映像流程
- iOS-获取手机健康步数,去除人为添加的数据
- RTL8723BU WiFi—蓝牙 模块移植