zoukankan      html  css  js  c++  java
  • CodeForces 689B【最短路】

    题意:
    给你一副图,给出的点两两之间的距离是abs(pos1-pos2),然后给你n个数是表示该pos到x的距离是1.
    思路:
    直接建边,跑spfa就好了。虽然说似乎题意说边很多,其实只要建一下相邻的点的边就好了,这样的图的性质还是得到了;

    //                      d*##$.
    // zP"""""$e.           $"    $o
    //4$       '$          $"      $
    //'$        '$        J$       $F
    // 'b        $k       $>       $
    //  $k        $r     J$       d$
    //  '$         $     $"       $~
    //   '$        "$   '$E       $
    //    $         $L   $"      $F ...
    //     $.       4B   $      $$$*"""*b
    //     '$        $.  $$     $$      $F
    //      "$       R$  $F     $"      $
    //       $k      ?$ u*     dF      .$
    //       ^$.      $$"     z$      u$$$$e
    //        #$b             $E.dW@e$"    ?$
    //         #$           .o$$# d$$$$c    ?F
    //          $      .d$$#" . zo$>   #$r .uF
    //          $L .u$*"      $&$$$k   .$$d$$F
    //           $$"            ""^"$$$P"$P9$
    //          JP              .o$$$$u:$P $$
    //          $          ..ue$"      ""  $"
    //         d$          $F              $
    //         $$     ....udE             4B
    //          #$    """"` $r            @$
    //           ^$L        '$            $F
    //             RN        4N           $
    //              *$b                  d$
    //               $$k                 $F
    //               $$b                $F
    //                 $""               $F
    //                 '$                $
    //                  $L               $
    //                  '$               $
    //                   $               $
    #include <bits/stdc++.h>
    using namespace std;
    typedef __int64 LL;
    
    const int N=2e5+10;
    
    struct asd{
        int to;
        LL w;
        int next;
    };
    asd q[N*8];
    int head[N*8],tol;
    int n;
    LL dis[N];
    bool vis[N];
    int num[N];
    
    queue<int>que;
    void spfa()
    {
        while(!que.empty())
            que.pop();
        for(int i=1;i<=n;i++)
        {
            vis[i]=num[i]=0;
            dis[i]=1e15;
        }
        vis[1]=true;
        num[1]++;
        dis[1]=0;
        que.push(1);
        while(!que.empty())
        {
            int u=que.front();
            que.pop();
            vis[u]=0;
            for(int v=head[u];v!=-1;v=q[v].next)
            {
                int i=q[v].to;
                if(dis[i]>dis[u]+q[v].w)
                {
                    dis[i]=dis[u]+q[v].w;
                    if(!vis[i])
                    {
                        vis[i]=1;
                        que.push(i);
                    }
                }
            }
        }
    }
    void add(int a,int b,LL c)
    {
        q[tol].to=b;
        q[tol].w=c;
        q[tol].next=head[a];
        head[a]=tol++;
    }
    
    int main()
    {
        scanf("%d",&n);
        int x;
        memset(head,-1,sizeof(head));
        tol=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            //add(x,i,1);
            add(i,x,1);
        }
        for(int i=2;i<=n;i++)
        {
            add(i,i-1,1);
            add(i-1,i,1);
        }
        spfa();
        for(int i=1;i<=n;i++)
            printf("%I64d ",dis[i]);
        return 0;
    }
  • 相关阅读:
    faster with MyISAM tables than with InnoDB or NDB tables
    w-BIG TABLE 1-toSMALLtable @-toMEMORY
    Indexing and Hashing
    MEMORY Storage Engine MEMORY Tables TEMPORARY TABLE max_heap_table_size
    controlling the variance of request response times and not just worrying about maximizing queries per second
    Variance
    Population Mean
    12.162s 1805.867s
    situations where MyISAM will be faster than InnoDB
    1920.154s 0.309s 30817
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934774.html
Copyright © 2011-2022 走看看