返回介绍

solution / 2600-2699 / 2642.Design Graph With Shortest Path Calculator / README

发布于 2024-06-17 01:03:01 字数 10453 浏览 0 评论 0 收藏 0

2642. 设计可以求最短路径的图类

English Version

题目描述

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

请你实现一个 Graph 类:

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

 

示例 1:

输入:
["Graph", "shortestPath", "shortestPath", "addEdge", "shortestPath"]
[[4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]], [3, 2], [0, 3], [[1, 3, 4]], [0, 3]]
输出:
[null, 6, -1, null, 6]

解释:
Graph g = new Graph(4, [[0, 2, 5], [0, 1, 2], [1, 2, 1], [3, 0, 3]]);
g.shortestPath(3, 2); // 返回 6 。从 3 到 2 的最短路径如第一幅图所示:3 -> 0 -> 1 -> 2 ,总代价为 3 + 2 + 1 = 6 。
g.shortestPath(0, 3); // 返回 -1 。没有从 0 到 3 的路径。
g.addEdge([1, 3, 4]); // 添加一条节点 1 到节点 3 的边,得到第二幅图。
g.shortestPath(0, 3); // 返回 6 。从 0 到 3 的最短路径为 0 -> 1 -> 3 ,总代价为 2 + 4 = 6 。

 

提示:

  • 1 <= n <= 100
  • 0 <= edges.length <= n * (n - 1)
  • edges[i].length == edge.length == 3
  • 0 <= fromi, toi, from, to, node1, node2 <= n - 1
  • 1 <= edgeCosti, edgeCost <= 106
  • 图中任何时候都不会有重边和自环。
  • 调用 addEdge 至多 100 次。
  • 调用 shortestPath 至多 100 次。

解法

方法一:Dijsktra 算法

在初始化函数中,我们先用邻接矩阵 $g$ 存储图的边权,其中 $g_{ij}$ 表示从节点 $i$ 到节点 $j$ 的边权,如果 $i$ 和 $j$ 之间没有边,则 $g_{ij}$ 的值为 $\infty$。

addEdge 函数中,我们更新 $g_{ij}$ 的值为 $edge[2]$。

shortestPath 函数中,我们使用 Dijsktra 算法求从节点 $node1$ 到节点 $node2$ 的最短路径,其中 $dist[i]$ 表示从节点 $node1$ 到节点 $i$ 的最短路径,$vis[i]$ 表示节点 $i$ 是否已经被访问过。我们初始化 $dist[node1]$ 为 $0$,其余的 $dist[i]$ 均为 $\infty$。然后我们遍历 $n$ 次,每次找到当前未被访问过的节点 $t$,使得 $dist[t]$ 最小。然后我们将节点 $t$ 标记为已访问,然后更新 $dist[i]$ 的值为 $min(dist[i], dist[t] + g_{ti})$。最后我们返回 $dist[node2]$,如果 $dist[node2]$ 为 $\infty$,则说明从节点 $node1$ 到节点 $node2$ 不存在路径,返回 $-1$。

时间复杂度 $O(n^2 \times q)$,空间复杂度 $O(n^2)$。其中 $n$ 为节点数,而 $q$ 为 shortestPath 函数的调用次数。

class Graph:
  def __init__(self, n: int, edges: List[List[int]]):
    self.n = n
    self.g = [[inf] * n for _ in range(n)]
    for f, t, c in edges:
      self.g[f][t] = c

  def addEdge(self, edge: List[int]) -> None:
    f, t, c = edge
    self.g[f][t] = c

  def shortestPath(self, node1: int, node2: int) -> int:
    dist = [inf] * self.n
    dist[node1] = 0
    vis = [False] * self.n
    for _ in range(self.n):
      t = -1
      for j in range(self.n):
        if not vis[j] and (t == -1 or dist[t] > dist[j]):
          t = j
      vis[t] = True
      for j in range(self.n):
        dist[j] = min(dist[j], dist[t] + self.g[t][j])
    return -1 if dist[node2] == inf else dist[node2]


# Your Graph object will be instantiated and called as such:
# obj = Graph(n, edges)
# obj.addEdge(edge)
# param_2 = obj.shortestPath(node1,node2)
class Graph {
  private int n;
  private int[][] g;
  private final int inf = 1 << 29;

  public Graph(int n, int[][] edges) {
    this.n = n;
    g = new int[n][n];
    for (var f : g) {
      Arrays.fill(f, inf);
    }
    for (int[] e : edges) {
      int f = e[0], t = e[1], c = e[2];
      g[f][t] = c;
    }
  }

  public void addEdge(int[] edge) {
    int f = edge[0], t = edge[1], c = edge[2];
    g[f][t] = c;
  }

  public int shortestPath(int node1, int node2) {
    int[] dist = new int[n];
    boolean[] vis = new boolean[n];
    Arrays.fill(dist, inf);
    dist[node1] = 0;
    for (int i = 0; i < n; ++i) {
      int t = -1;
      for (int j = 0; j < n; ++j) {
        if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
          t = j;
        }
      }
      vis[t] = true;
      for (int j = 0; j < n; ++j) {
        dist[j] = Math.min(dist[j], dist[t] + g[t][j]);
      }
    }
    return dist[node2] >= inf ? -1 : dist[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);
 */
class Graph {
public:
  Graph(int n, vector<vector<int>>& edges) {
    this->n = n;
    g = vector<vector<int>>(n, vector<int>(n, inf));
    for (auto& e : edges) {
      int f = e[0], t = e[1], c = e[2];
      g[f][t] = c;
    }
  }

  void addEdge(vector<int> edge) {
    int f = edge[0], t = edge[1], c = edge[2];
    g[f][t] = c;
  }

  int shortestPath(int node1, int node2) {
    vector<bool> vis(n);
    vector<int> dist(n, inf);
    dist[node1] = 0;
    for (int i = 0; i < n; ++i) {
      int t = -1;
      for (int j = 0; j < n; ++j) {
        if (!vis[j] && (t == -1 || dist[t] > dist[j])) {
          t = j;
        }
      }
      vis[t] = true;
      for (int j = 0; j < n; ++j) {
        dist[j] = min(dist[j], dist[t] + g[t][j]);
      }
    }
    return dist[node2] >= inf ? -1 : dist[node2];
  }

private:
  vector<vector<int>> g;
  int n;
  const int inf = 1 << 29;
};

/**
 * 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);
 */
const inf = 1 << 29

type Graph struct {
  g [][]int
}

func Constructor(n int, edges [][]int) Graph {
  g := make([][]int, n)
  for i := range g {
    g[i] = make([]int, n)
    for j := range g[i] {
      g[i][j] = inf
    }
  }
  for _, e := range edges {
    f, t, c := e[0], e[1], e[2]
    g[f][t] = c
  }
  return Graph{g}
}

func (this *Graph) AddEdge(edge []int) {
  f, t, c := edge[0], edge[1], edge[2]
  this.g[f][t] = c
}

func (this *Graph) ShortestPath(node1 int, node2 int) int {
  n := len(this.g)
  dist := make([]int, n)
  for i := range dist {
    dist[i] = inf
  }
  vis := make([]bool, n)
  dist[node1] = 0
  for i := 0; i < n; i++ {
    t := -1
    for j := 0; j < n; j++ {
      if !vis[j] && (t == -1 || dist[t] > dist[j]) {
        t = j
      }
    }
    vis[t] = true
    for j := 0; j < n; j++ {
      dist[j] = min(dist[j], dist[t]+this.g[t][j])
    }
  }
  if dist[node2] >= inf {
    return -1
  }
  return dist[node2]
}

/**
 * Your Graph object will be instantiated and called as such:
 * obj := Constructor(n, edges);
 * obj.AddEdge(edge);
 * param_2 := obj.ShortestPath(node1,node2);
 */
class Graph {
  private g: number[][] = [];
  private inf: number = 1 << 29;

  constructor(n: number, edges: number[][]) {
    this.g = Array.from({ length: n }, () => Array(n).fill(this.inf));
    for (const [f, t, c] of edges) {
      this.g[f][t] = c;
    }
  }

  addEdge(edge: number[]): void {
    const [f, t, c] = edge;
    this.g[f][t] = c;
  }

  shortestPath(node1: number, node2: number): number {
    const n = this.g.length;
    const dist: number[] = new Array(n).fill(this.inf);
    dist[node1] = 0;
    const vis: boolean[] = new Array(n).fill(false);
    for (let i = 0; i < n; ++i) {
      let t = -1;
      for (let j = 0; j < n; ++j) {
        if (!vis[j] && (t === -1 || dist[j] < dist[t])) {
          t = j;
        }
      }
      vis[t] = true;
      for (let j = 0; j < n; ++j) {
        dist[j] = Math.min(dist[j], dist[t] + this.g[t][j]);
      }
    }
    return dist[node2] >= this.inf ? -1 : dist[node2];
  }
}

/**
 * Your Graph object will be instantiated and called as such:
 * var obj = new Graph(n, edges)
 * obj.addEdge(edge)
 * var param_2 = obj.shortestPath(node1,node2)
 */
public class Graph {
  private int n;
  private int[][] g;
  private readonly int inf = 1 << 29;

  public Graph(int n, int[][] edges) {
    this.n = n;
    g = new int[n][];
    for (int i = 0; i < n; i++)
    {
      g[i] = new int[n];
      for (int j = 0; j < n; j++)
      {
        g[i][j] = inf;
      }
    }
    foreach (int[] e in edges)
    {
      g[e[0]][e[1]] = e[2];
    }
  }

  public void AddEdge(int[] edge) {
    g[edge[0]][edge[1]] = edge[2];
  }

  public int ShortestPath(int node1, int node2) {
    int[] dist = new int[n];
    bool[] vis = new bool[n];
    Array.Fill(dist, inf);
    dist[node1] = 0;

    for (int i = 0; i < n; i++)
    {
      int t = -1;
      for (int j = 0; j < n; j++)
      {
        if (!vis[j] && (t == -1 || dist[t] > dist[j]))
          t = j;
      }
      vis[t] = true;
      for (int j = 0; j < n; j++)
      {
        dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
      }
    }
    return dist[node2] >= inf ? -1 : dist[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);
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文