您的位置:首页 > 其它

CodeForces 689B - Mike and Shortcuts

2016-07-08 10:15 417 查看

题目

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 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: