P8802 [蓝桥杯 2022 国 B] 出差——最短路问题(SPFA)

目录

题目

解析

代码


最短路问题

最短路问题---朴素Dijkstra算法,堆优化版Dijkstra算法,Bellman-Ford算法,SPFA算法,Floyd算法(算法思路+例题)_dijkstra+堆优化-CSDN博客

在这里,比较推荐SPFA算法。算法参考以上链接。

题目

解析

        题目很明显,就是经典的最短路问题。只是多加了一个要隔离的时间,每次加上就好了,使用spfa算法

        数组模拟邻接表+队列

时间复杂度:一般O(m) ,最坏O(nm)

伪代码:

Queue q

q  <- 1

while(q不空)

        t  <- 取出q的队头

        更新 t 的所有出边 t -> b

        如果更新成功且 b 点没有在队列中 ,将 b 装入 q 中 

代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;

//P8802 [蓝桥杯 2022 国 B] 出差
// SPFA
public class P8802 {
    static int N = 20010;
    static int n,m,idx;
    // 数组模拟邻接表
    static int[] h = new int[N];
    static int[] e = new int[N];
    static int[] ne = new int[N];

    static int[] weight = new int[N]; // 每个城市隔离时间
    static int[] weight1 = new int[N]; // 城市到城市之间的时间
    static int[] dist = new int[N]; // 从1到每个点的最短距离
    static boolean[] st = new boolean[N];
    static Queue<Integer> q = new PriorityQueue<>();

    public static void main(String[] args) throws IOException{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] s = in.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        // 读取隔离时间
        s = in.readLine().split(" ");
        for (int i = 1; i <= n; i++)
            weight[i] = Integer.parseInt(s[i-1]);
        // 初始化
        Arrays.fill(h,-1);
        Arrays.fill(dist,0x3f3f3f3f);
        dist[1] = 0;
        // 读取 m 个路径
        for (int i = 0; i < m; i++) {
            s = in.readLine().split(" ");
            int a = Integer.parseInt(s[0]);
            int b = Integer.parseInt(s[1]);
            int c = Integer.parseInt(s[2]);
            add(a,b,c);
            add(b,a,c);
        }
        // spfa
        q.add(1);
        while (!q.isEmpty()){
            int t = q.poll();
            st[t] = false;
            for (int i = h[t]; i != -1 ; i = ne[i]) {
                int j = e[i];
                int w = weight1[i];
                int c = weight[j];
                if(dist[j]>dist[t]+c+w){
                    dist[j] = dist[t]+c+w;
                    if(!st[j]){
                        q.add(j);
                        st[j] = true;
                    }
                }
            }
        }
        System.out.println(dist[n]-weight[n]); // n不隔离
    }
    public static void add(int a,int b,int c){
        weight1[idx] = c;
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

  • 16
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SPFA算法(Shortest Path Faster Algorithm)是一种用于解决最短路问题算法。它是一种单源最短路径算法,可以解决带有负权边的图的最短路径问题。 SPFA算法基本思想是使用队列对图中的所有节点进行遍历,对于每一个节点,如果它的邻居节点的最短路径可以通过当前节点更新,则将邻居节点加入队列中进行下一轮遍历,直到所有节点的最短路径都被更新后停止遍历。 在Java中,可以使用邻接矩阵或邻接表来表示图,并使用队列来实现SPFA算法。下面是一个使用邻接矩阵实现SPFA算法的Java代码示例: ```java import java.util.*; public class SPFA { public static void main(String[] args) { int[][] graph = { {0, 2, 5, Integer.MAX_VALUE, Integer.MAX_VALUE}, {Integer.MAX_VALUE, 0, 7, 1, Integer.MAX_VALUE}, {Integer.MAX_VALUE, Integer.MAX_VALUE, 0, 4, Integer.MAX_VALUE}, {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 0, 3}, {Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, 0} }; int[] dist = shortestPath(graph, 0); System.out.println(Arrays.toString(dist)); } public static int[] shortestPath(int[][] graph, int start) { int n = graph.length; int[] dist = new int[n]; Arrays.fill(dist, Integer.MAX_VALUE); dist[start] = 0; Queue<Integer> queue = new LinkedList<>(); queue.offer(start); boolean[] inQueue = new boolean[n]; inQueue[start] = true; while (!queue.isEmpty()) { int u = queue.poll(); inQueue[u] = false; for (int v = 0; v < n; v++) { if (graph[u][v] != Integer.MAX_VALUE && dist[v] > dist[u] + graph[u][v]) { dist[v] = dist[u] + graph[u][v]; if (!inQueue[v]) { queue.offer(v); inQueue[v] = true; } } } } return dist; } } ``` 在上面的代码中,我们使用一个二维数组`graph`来表示图,其中`graph[i][j]`表示从节点`i`到节点`j`的边的权重,如果没有边则为`Integer.MAX_VALUE`。函数`shortestPath`接受一个图和一个起点`start`,返回一个数组`dist`,其中`dist[i]`表示从起点`start`到节点`i`的最短路径。 在函数中,我们首先初始化`dist`数组为`Integer.MAX_VALUE`,表示所有节点到起点的距离都是无限大。然后将起点`start`加入队列中,并标记为已加入队列。进入循环后,每次取出队列中的一个节点`u`,将`u`标记为未加入队列,然后遍历`u`的所有邻居节点`v`,如果从起点到`v`的距离可以通过从起点到`u`再加上`u`到`v`的距离来更新,则更新`dist[v]`的值,并将`v`加入队列中,并标记为已加入队列。当队列为空时,所有节点的最短路径都已被更新,函数返回`dist`数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值