图论(最短路)

leetcode743. 网络延迟时间🔐🔐

在这里插入图片描述

class Solution {
    List<ArrayList<int[]>> grid;
    boolean[] isVisited;
    int[] dist;

    public int networkDelayTime(int[][] times, int n, int k) {
        grid = new ArrayList<>(n + 1);
        isVisited = new boolean[n + 1];
        dist = new int[n + 1];
        Arrays.fill(dist, Integer.MAX_VALUE);
        for (int i = 0; i <= n; i++) grid.add(new ArrayList<>());
        for (int[] time : times) grid.get(time[0]).add(new int[]{time[1], time[2]});
        dijkstra(k);
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            ans = Math.max(ans, dist[i]);
        }
        return ans > Integer.MAX_VALUE / 2 ? -1 : ans;
    }

    private void dijkstra(int start) {
        PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(o -> o[1]));
        dist[start] = 0;
        queue.add(new int[]{start, 0});
        while (!queue.isEmpty()) {
            int[] pop = queue.poll();
            int e = pop[0], distance = pop[1];// distance也就是dist[e]
            if (isVisited[e]) continue;
            isVisited[e] = true;
            for (int[] val : grid.get(e)) {
                int j = val[0];
                if (dist[j] > distance + val[1]) {// dijkstra思想
                    dist[j] = distance + val[1];
                    queue.add(new int[]{j, dist[j]});// 添加更新节点
                }
            }
        }
    }
}

leetcode787. K 站中转内最便宜的航班🔐🔐

在这里插入图片描述
在这里插入图片描述

  • 弗洛伊德算法思想松弛
class Solution {
    int INF = 0x3f3f3f3f;
    int[] dist;

    public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
        dist = new int[n + 1];
        Arrays.fill(dist, INF);
        dist[src] = 0;
        // 中间经过节点小于等于k的所有情况
        for (int limit = 0; limit <= k; limit++) {
            int[] clone = dist.clone();
            for (int[] each : flights) {
            	// 松弛顶点
                int x = each[0], y = each[1], w = each[2];
                dist[y] = Math.min(dist[y], clone[x] + w);
            }
        }
        return dist[dst] > INF / 2 ? -1 : dist[dst];
    }
}

leetcode407. 接雨水 II🔐🔐

在这里插入图片描述
在这里插入图片描述

class Solution {
    public int trapRainWater(int[][] heightMap) {
        int m = heightMap.length, n = heightMap[0].length;
        PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(a -> a[2]));
        boolean[][] vis = new boolean[m][n];
        // 将第一行和最后第一行的数据进入优先队列
        for (int i = 0; i < n; i++) {
            queue.add(new int[]{0, i, heightMap[0][i]});
            queue.add(new int[]{m - 1, i, heightMap[m - 1][i]});
            vis[0][i] = vis[m - 1][i] = true;
        }
        // 将第一列和最后第一列的数据进入优先队列
        for (int i = 1; i < m - 1; i++) {
            queue.add(new int[]{i, 0, heightMap[i][0]});
            queue.add(new int[]{i, n - 1, heightMap[i][n - 1]});
            vis[i][0] = vis[i][n - 1] = true;
        }
        int[][] dirs = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        int ans = 0;
        while (!queue.isEmpty()) {
            int[] poll = queue.poll();
            int x = poll[0], y = poll[1], h = poll[2];
            for (int[] d : dirs) {// 向四个方向扩展计算最小高度和扩展的高度heightMap[nx][ny]比较
                int nx = x + d[0], ny = y + d[1];
                if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
                if (vis[nx][ny]) continue;
                if (h > heightMap[nx][ny]) ans += h - heightMap[nx][ny];// 最小值都比它大,定能存水
                queue.add(new int[]{nx, ny, Math.max(heightMap[nx][ny], h)});
                vis[nx][ny] = true;
            }
        }
        return ans;
    }
}

leetcode847. 访问所有节点的最短路径🔐🔐

在这里插入图片描述

在这里插入图片描述

class Solution {
    public int shortestPathLength(int[][] graph) {
        int n = graph.length;

        // 1.初始化队列及标记数组,存入起点
        Queue<int[]> queue = new LinkedList<>(); // 三个属性分别为 idx, mask, dist
        boolean[][] vis = new boolean[n][1 << n]; // 节点编号及当前状态
        for (int i = 0; i < n; i++) {
            //存入起点(指向方向作用),标记(已经走过的节点记录),从起始节点走过标记节点并且到i的距离
            queue.offer(new int[]{i, 1 << i, 0});
            vis[i][1 << i] = true;
        }

        // 开始搜索
        while (!queue.isEmpty()) {
            int[] tuple = queue.poll(); // 弹出队头元素
            int idx = tuple[0], mask = tuple[1], dist = tuple[2];

            // 找到答案,返回结果
            if (mask == (1 << n) - 1) return dist;

            // 扩展
            for (int x : graph[idx]) {
                int next_mask = mask | (1 << x);
                if (!vis[x][next_mask]) {// 若存在,说明定存在有一条其他最优路路,距离不超过dist
                    queue.offer(new int[]{x, next_mask, dist + 1});
                    vis[x][next_mask] = true;
                }
            }
        }
        return 0;
    }
}

leetcode2045. 到达目的地的第二短时间🔐🔐

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
题解

class Solution {
    int INF = 0x3f3f3f3f;
    List<ArrayList<Integer>> grid;
    int[] dist1, dist2;

    public int secondMinimum(int n, int[][] edges, int time, int change) {
        grid = new ArrayList<>(n + 1);
        for (int i = 0; i <= n; i++) grid.add(new ArrayList<>());
        dist1 = new int[n + 1];
        dist2 = new int[n + 1];
        Arrays.fill(dist1, INF);
        Arrays.fill(dist2, INF);
        for (int[] val : edges) {
            grid.get(val[0]).add(val[1]);
            grid.get(val[1]).add(val[0]);
        }
        PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
        queue.add(new int[]{1, 0});
        dist1[1] = 0;
        while (!queue.isEmpty()) {
            int[] poll = queue.poll();
            int u = poll[0], step = poll[1];
            for (int val : grid.get(u)) {
                int e = val;
                int a = step / change, b = step % change;
                int wait = a % 2 == 0 ? 0 : change - b;
                int dist = step + time + wait;
                if (dist1[e] > dist) {
                    dist2[e] = dist1[e];
                    dist1[e] = dist;
                    queue.add(new int[]{e, dist1[e]});
                    queue.add(new int[]{e, dist2[e]});
                } else if (dist1[e] < dist && dist < dist2[e]) {
                    dist2[e] = dist;
                    queue.add(new int[]{e, dist2[e]});
                }
            }
        }
        return dist2[n];
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[1\]提供了使用Python的networkx库绘制网络图和计算最短加权路径的示例代码。该代码使用了一个包含顶点和边的列表,并使用add_nodes_from和add_weighted_edges_from方法将它们添加到图中。然后,使用nx.shortest_path_length方法计算了从顶点v1到顶点v11的最短加权路径长度为13。\[1\] 引用\[2\]提供了一个计算最短路径的Python程序示例。该程序使用了numpy和networkx库。首先,定义了一个包含顶点和边的列表,并使用add_nodes_from和add_weighted_edges_from方法将它们添加到图中。然后,使用nx.shortest_path_length方法计算了最短路径长度,并将结果存储在一个字典中。接下来,使用numpy创建了一个6x6的零矩阵,并使用两个嵌套的for循环将最短路径长度填充到矩阵中。最后,使用矩阵乘法计算了运力,并找到了最小运力和对应的位置。\[2\] 引用\[3\]提供了关于Dijkstra算法的一些背景信息。Dijkstra算法是一种寻找最短路径的算法,适用于所有权重大于等于0的情况。它可以用于解决从一个起始点到任意一个点的最短路径问题。\[3\] 综上所述,如果你想在Python中计算图论中的最短路径,可以使用networkx库和Dijkstra算法。你可以根据引用\[1\]和引用\[2\]中的示例代码进行操作。 #### 引用[.reference_title] - *1* *3* [运筹学——图论与最短距离(Python实现)](https://blog.csdn.net/weixin_46039719/article/details/122521276)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [数学建模:图论模型 — 最短路模型示例 (Python 求解)](https://blog.csdn.net/qq_55851911/article/details/124776487)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「 25' h 」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值