Codeforces Round #361 (Div. 2) B BFS最短路

CrossDolphin發表於2016-07-07



連結:戳這裡


B. Mike and Shortcuts
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

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-2-3-4-...-n),一個人正在快走,每經過兩個相連的城市消耗的能量為1

接下來給出n個數ai,表示從第i個城市到達第ai個城市消耗能量為1(i<=ai<=n)

問到達每個城市消耗最小的能量是多少,分別輸出


思路:

每個城市都能到達三個城市分別是i-1、i+1、ai 且消耗的能量都是1

直接建圖,然後bfs跑出從1到達所有城市所消耗的最小能量

就是跑最短路的過程


程式碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
int a[200100];
struct edge{
    int v,next;
}e[1000100];
int head[1000100],tot=0;
void Add(int u,int v){
    e[tot].v=v;
    e[tot].next=head[u];
    head[u]=tot++;
}
int dis[200100];
void BFS(int x){
    queue<int> qu;
    dis[x]=0;
    qu.push(x);
    while(!qu.empty()){
        int u=qu.front();
        qu.pop();
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(dis[v]>dis[u]+1){
                dis[v]=dis[u]+1;
                qu.push(v);
            }
        }
    }
}
int main(){
    mst(head,-1);
    mst(dis,127);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++){
        if(i+1<=n) Add(i,i+1);
        if(i-1>=1) Add(i,i-1);
        Add(i,a[i]);
    }
    BFS(1);
    for(int i=1;i<=n;i++) printf("%d ",dis[i]);
    puts("");
    return 0;
}


相關文章