CodeForces 689B【最短路】

本文介绍了一种基于最短路径快速算法(SPFA)的应用案例,通过构建特殊性质的图来解决点对点距离问题。文章详细解释了如何通过直接建立边关系并使用SPFA求解两点间最短路径的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:
给你一副图,给出的点两两之间的距离是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;
}
### Codeforces 平台上的短路径算法题目 #### Dijkstra 算法的应用实例 当面对构建短路径树的需求时,可以采用Dijkstra算法来解决。该方法的核心在于优先选择当前节点能够延伸出去的多条候选边中具有小权重的一条作为扩展方向;而在仅存在唯一一条可能的拓展边的情况下,则直接选取这条边继续探索过程[^1]。 ```python import heapq def dijkstra(graph, start): n = len(graph) dist = [float('inf')] * n dist[start] = 0 heap = [(0, start)] while heap: current_dist, u = heapq.heappop(heap) if current_dist != dist[u]: continue for v, weight in graph[u].items(): alt = dist[u] + weight if alt < dist[v]: dist[v] = alt heapq.heappush(heap, (alt, v)) return dist ``` #### Floyd-Warshall 算法处理复杂情况下的优化策略 对于涉及多个源点之间的短路径计算问题,Floyd-Warshall是一个有效的解决方案。然而,在某些特定场景下(比如本题),通过预先给定的信息可以直接定位受影响的部分并加以简化,从而避免不必要的全量遍历操作,达到降低时间复杂度的效果。值得注意的是,在累加过程中应当选用`long long`类型的变量以防止溢出错误的发生[^2]。 #### 单源短路径查询案例解析 考虑到从指定起点出发前往其余各个顶点间的短距离需求,此情形适用于单源短路径类别的算法实现方式。具体而言,即是从某固定位置开始测量至其它任意可达地点的距离长度,并针对不可达的情形输出特殊标记值 `-1` 表明无法访问的状态[^3]。 ```cpp #include<bits/stdc++.h> using namespace std; const int INF=0x3f3f3f; vector<pair<int,int>> adj[100]; int dis[100]; void spfa(int src){ queue<int> q; vector<bool> vis(100,false); fill(dis,dis+100,INF); dis[src]=0;q.push(src); while(!q.empty()){ int cur=q.front();q.pop(); vis[cur]=false; for(auto& edge : adj[cur]){ int next=edge.first,cost=edge.second; if(dis[next]>dis[cur]+cost){ dis[next]=dis[cur]+cost; if(!vis[next]){ vis[next]=true; q.push(next); } } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值