Floyd,最短路维护,LeetCode 2642. 设计可以求最短路径的图类

目录

一、题目

1、题目描述

2、接口描述

​cpp

python3

3、原题链接

二、解题报告

1、思路分析

2、复杂度

3、代码详解

​cpp

python3


一、题目

1、题目描述

给你一个有 n 个节点的 有向带权 图,节点编号为 0n - 1 。图中的初始边用数组 edges 表示,其中 edges[i] = [fromi, toi, edgeCosti] 表示从 fromitoi 有一条代价为 edgeCosti 的边。

请你实现一个 Graph 类:

  • Graph(int n, int[][] edges) 初始化图有 n 个节点,并输入初始边。
  • addEdge(int[] edge) 向边集中添加一条边,其中edge = [from, to, edgeCost] 。数据保证添加这条边之前对应的两个节点之间没有有向边。
  • int shortestPath(int node1, int node2) 返回从节点 node1node2 的路径最小 代价。如果路径不存在,返回 -1 。一条路径的代价是路径中所有边代价之和。

2、接口描述

​cpp
cpp 复制代码
class Graph {
public:
    Graph(int n, vector<vector<int>>& edges) {

    }
    
    void addEdge(vector<int> edge) {

    }
    
    int shortestPath(int node1, int node2) {

    }
};

/**
 * Your Graph object will be instantiated and called as such:
 * Graph* obj = new Graph(n, edges);
 * obj->addEdge(edge);
 * int param_2 = obj->shortestPath(node1,node2);
 */
python3
cpp 复制代码
class Graph:

    def __init__(self, n: int, edges: List[List[int]]):


    def addEdge(self, edge: List[int]) -> None:


    def shortestPath(self, node1: int, node2: int) -> int:



# Your Graph object will be instantiated and called as such:
# obj = Graph(n, edges)
# obj.addEdge(edge)
# param_2 = obj.shortestPath(node1,node2)

3、原题链接

2642. 设计可以求最短路径的图类 - 力扣(LeetCode)


二、解题报告

1、思路分析

数据规模100个点,可以用floyd

我们初始化就直接floyd即可

然后考虑floyd的原理无非就是枚举路径上的点进行动态规划,那么我们对<x, y>这条边进行修改后,以<x, y>这条边进行一次松弛操作即可

2、复杂度

时间复杂度:初始化:O(n^3) ,最短路维护:O(n^2),最短路获取:O(1)空间复杂度:O(n^2)

3、代码详解

​cpp
cpp 复制代码
int g[105][105];
const int inf = 0x3f3f3f3f;
class Graph {
public:
int _n;
    Graph(int n, vector<vector<int>>& edges):_n(n) {
        memset(g, 0x3f, sizeof g);
        for(int i = 0; i < n; i++) g[i][i] = 0;
        for(auto& e : edges)
            g[e[0]][e[1]] = e[2];
        for(int k = 0; k < n; k++)
            for(int i = 0; i < n; i++){
                if(g[i][k] == inf) continue;
                for(int j = 0; j < n; j++)
                    g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
            }
    }
    
    void addEdge(vector<int> e) {
        if(e[2] >= g[e[0]][e[1]]) return;
        int a = e[0], b = e[1];
        g[a][b] = e[2];
        for(int i = 0; i < _n; i++)
            for(int j = 0; j < _n; j++)
                g[i][j] = min(g[i][j], g[i][a] + g[b][j] + e[2]);
    }
    
    int shortestPath(int x, int y) {
        return g[x][y] < inf ? g[x][y] : -1;
    }
};

/**
 * Your Graph object will be instantiated and called as such:
 * Graph* obj = new Graph(n, edges);
 * obj->addEdge(edge);
 * int param_2 = obj->shortestPath(node1,node2);
 */
python3
python 复制代码
class Graph:

    def __init__(self, n: int, edges: List[List[int]]):
        self.g = [[inf] * n for _ in range(n)]
        g = self.g
        for i in range(n):
            g[i][i] = 0
        for x, y, w in edges:
            g[x][y] = w
        for k in range(n):
            for i in range(n):
                for j in range(n):
                    g[i][j] = min(g[i][j], g[i][k] + g[k][j])

    def addEdge(self, edge: List[int]) -> None:
        x, y, w = edge
        g = self.g
        if w > g[x][y]:
            return
        g[x][y] = w
        n = len(self.g)
        for i in range(n):
            for j in range(n):
                g[i][j] = min(g[i][j], g[i][x] + g[y][j] + g[x][y])

    def shortestPath(self, x: int, y: int) -> int:
        g = self.g
        return g[x][y] if g[x][y] < inf else -1



# Your Graph object will be instantiated and called as such:
# obj = Graph(n, edges)
# obj.addEdge(edge)
# param_2 = obj.shortestPath(node1,node2)
相关推荐
多米Domi01121 分钟前
0x3f 第49天 面向实习的八股背诵第六天 过了一遍JVM的知识点,看了相关视频讲解JVM内存,垃圾清理,买了plus,稍微看了点确定一下方向
jvm·数据结构·python·算法·leetcode
_F_y6 小时前
MySQL用C/C++连接
c语言·c++·mysql
兩尛6 小时前
c++知识点2
开发语言·c++
xiaoye-duck7 小时前
C++ string 底层原理深度解析 + 模拟实现(下)——面试 / 开发都适用
开发语言·c++·stl
Azure_withyou7 小时前
Visual Studio中try catch()还未执行,throw后便报错
c++·visual studio
琉染云月7 小时前
【C++入门练习软件推荐】Visual Studio下载与安装(以Visual Studio2026为例)
c++·visual studio
L_09079 小时前
【C++】高阶数据结构 -- 红黑树
数据结构·c++
A_nanda9 小时前
c# MOdbus rto读写串口,如何不相互影响
算法·c#·多线程
代码雕刻家11 小时前
2.4.蓝桥杯-分巧克力
算法·蓝桥杯
Ulyanov11 小时前
顶层设计——单脉冲雷达仿真器的灵魂蓝图
python·算法·pyside·仿真系统·单脉冲