• Codeforces Round #361 (Div. 2) B bfs处理


    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 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.

    题意:  从位置1出发到其他位置,距离为|i - j|    但是存在近路一说,也就是可以直接从i到ai 距离为1

               输出从位置1到其他位置的最少距离

    题解:最少距离bfs处理

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 using namespace std;
     6 struct node
     7 {
     8  int pos;
     9  int step;
    10 }N[200005],now,exm;
    11 int n;
    12 int a[200005];
    13 int b[200005];
    14 int ans[200005];
    15 queue<node>q;
    16 int main()
    17 {
    18     scanf("%d",&n);
    19     memset(b,0,sizeof(b));
    20     for(int i=1;i<=n;i++)
    21     N[i].pos=i;
    22     for(int i=1;i<=n;i++)
    23       scanf("%d",&a[i]);
    24     exm.pos=1;
    25     exm.step=0;
    26     b[1]=1;
    27     q.push(exm);
    28 
    29     while(!q.empty())
    30     {
    31         exm=q.front();
    32        // cout<<exm.pos<<" "<<exm.step<<endl;
    33         ans[exm.pos]=exm.step;
    34         q.pop();
    35         for(int i=1;i<=3;i++)
    36         {
    37             if(i==1)
    38             {
    39                if(b[exm.pos+1]==0&&exm.pos+1<=n)
    40                {
    41                    now.pos=exm.pos+1;
    42                    now.step=exm.step+1;
    43                    q.push(now);
    44                    b[now.pos]=1;
    45                }
    46             }
    47             if(i==2)
    48             {
    49                if(b[exm.pos-1]==0&&exm.pos-1>=1)
    50                {
    51                    now.pos=exm.pos-1;
    52                    now.step=exm.step+1;
    53                    q.push(now);
    54                    b[now.pos]=1;
    55                }
    56             }
    57             if(i==3)
    58             {
    59                 if(b[a[exm.pos]]==0&&a[exm.pos]>=1&&a[exm.pos]<=n)
    60                 {
    61                     now.pos=a[exm.pos];
    62                     now.step=exm.step+1;
    63                     q.push(now);
    64                     b[a[exm.pos]]=1;
    65                 }
    66             }
    67         }
    68     }
    69     cout<<ans[1];
    70     for(int i=2;i<=n;i++)
    71         printf(" %d",ans[i]);
    72     return 0;
    73 }
  • 相关阅读:
    excel表中的一列,如何去除重复项
    Notepad++ 列操作
    mysql 登陆其它服务器
    贪心算法处理背包问题
    分治法处理排序问题
    回溯法_皇后问题
    动态规划算法的java实现
    人事管理系统
    java复习笔记
    核心动画-翻页效果的实现
  • 原文地址:https://www.cnblogs.com/hsd-/p/5657133.html
Copyright © 2020-2023  润新知