【打CF,學演算法——三星級】CodeForces 689B Mike and Shortcuts (最短路+spfa)
題面:
B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard outputRecently, 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
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 only1 unit of energy. There are exactly
n shortcuts in Mike's city, the
ith of them allows walking from intersectioni
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 each1 ≤ i ≤ n Mike is interested in finding minimum possible total energy of some sequencep1 = 1, p2, ..., pk = i.
InputThe 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 intersectioni to intersection ai using only1 unit of energy. Please note that the shortcuts don't allow walking in opposite directions (fromai toi).
OutputIn the only line print n integers m1, m2, ..., mn, wheremi denotes the least amount of total energy required to walk from intersection1 to intersection i.
Examples Input3 2 2 3Output
0 1 2Input
5 1 2 3 4 5Output
0 1 2 3 4Input
7 4 4 4 4 7 7 7Output
0 1 2 1 2 3 3Note
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<=200000),任意兩個節點的距離為這兩點的序號之差絕對值。且每個點都會有一個捷徑,通過該捷徑到達對應點的代價為1,求每個點到點1的最小距離。
解題:
剛開始看到這題肯定是懵逼的,這麼大的圖,沒辦法存。但是這題很有特殊性,任意兩點間距離已知,且為序號差,同時如果不算捷徑的話,除1以外的其他節點直接到節點1的距離和通過他的前繼到達節點1的距離是一樣的,利用這個特殊性,我們就可以省去大部分邊,只保留1->2->3->....->n的邊和捷徑,這樣大致只有40000條邊,採用SPFA求解即可。
注意點:
1.捷徑是單向的,節點間的路是雙向的。
2.部分捷徑是通往自身的,可以直接省略。
程式碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 200010
#define inf 0x3f3f3f3f
using namespace std;
struct edge
{
int fm,to,val;
}store[maxn*3];
queue <int> qe;
int cnt=0,head[maxn],next[maxn*3],vis[maxn];
int dist[maxn];
void addedge(int fm,int to,int val)
{
next[cnt]=head[fm];
head[fm]=cnt;
store[cnt].fm=fm;
store[cnt].to=to;
store[cnt].val=val;
cnt++;
}
void spfa()
{
int cur,tmp;
memset(vis,0,sizeof(vis));
qe.push(1);
vis[1]=1;
while(!qe.empty())
{
cur=qe.front();
qe.pop();
vis[cur]=0;
for(int i=head[cur];~i;i=next[i])
{
tmp=dist[store[i].fm]+store[i].val;
if(tmp<dist[store[i].to])
{
dist[store[i].to]=tmp;
// 佇列中保留一個更新即可
if(!vis[store[i].to])
qe.push(store[i].to);
}
}
}
}
int main()
{
int n,tmp;
memset(head,-1,sizeof(head));
scanf("%d",&n);
memset(dist,inf,sizeof(dist));
dist[1]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&tmp);
if(tmp!=i)
addedge(i,tmp,1);
}
for(int i=2;i<=n;i++)
{
addedge(i-1,i,1);
addedge(i,i-1,1);
}
spfa();
printf("%d",dist[1]);
for(int i=2;i<=n;i++)
printf(" %d",dist[i]);
printf("\n");
return 0;
}